diff --git a/cmd/server/main.go b/cmd/server/main.go index abc1bb2..ccf72ef 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -5,6 +5,7 @@ import ( "errors" "flag" "fmt" + "io" "log" "net" "net/http" @@ -18,6 +19,7 @@ import ( "github.com/appthrust/appserver-go/apphealth" "github.com/appthrust/appserver-go/appserver/health" "github.com/appthrust/appserver-go/appserver/ollamasupervisor" + "github.com/appthrust/appserver-go/appserver/requestlog" ) type config struct { @@ -91,11 +93,8 @@ func serve(ctx context.Context, listen string, cfg config) error { defer close(childDone) s.Run(childCtx) }() - server := &http.Server{ - Handler: newHandler(cfg.model, cfg.upstream, func() bool { return !draining.Load() && s.Ready() }, &outcomes), - ReadHeaderTimeout: 10 * time.Second, ReadTimeout: requestTimeout, - WriteTimeout: requestTimeout + 5*time.Second, IdleTimeout: 60 * time.Second, - } + handler := newHandler(cfg.model, cfg.upstream, func() bool { return !draining.Load() && s.Ready() }, &outcomes) + server := newServer(handler, os.Stderr) served := make(chan error, 1) go func() { served <- server.Serve(listener) }() log.Printf("embedding-server listening on %s (project %s)", listener.Addr(), cfg.projectID) @@ -117,3 +116,11 @@ func serve(ctx context.Context, listen string, cfg config) error { } return err } + +func newServer(handler http.Handler, logOutput io.Writer) *http.Server { + return &http.Server{ + Handler: requestlog.Wrap(handler, requestlog.Options{Service: "embedding", Output: logOutput}), + ReadHeaderTimeout: 10 * time.Second, ReadTimeout: requestTimeout, + WriteTimeout: requestTimeout + 5*time.Second, IdleTimeout: 60 * time.Second, + } +} diff --git a/cmd/server/server.go b/cmd/server/server.go index 7e94bb4..38bd813 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -14,6 +14,7 @@ import ( "github.com/appthrust/appserver-go/appserver/health" "github.com/appthrust/appserver-go/appserver/httpx" "github.com/appthrust/appserver-go/appserver/releaseidentity" + "github.com/appthrust/appserver-go/appserver/requestlog" ) const ( @@ -130,7 +131,14 @@ func newHandler(model string, upstream *url.URL, ready func() bool, outcomes *he if response.StatusCode == http.StatusTooManyRequests { class = apphealth.ReasonResourceExhausted } - outcomes.Record(copyErr == nil && response.StatusCode >= 200 && response.StatusCode < 300, class) + upstreamOK := response.StatusCode >= 200 && response.StatusCode < 300 + switch { + case copyErr != nil: + requestlog.SetOutcome(r, "stream_interrupted", "response_copy_failed") + case !upstreamOK: + requestlog.SetOutcome(r, "upstream_error", "upstream_non_2xx") + } + outcomes.Record(copyErr == nil && upstreamOK, class) }) mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, _ *http.Request) { httpx.WriteJSON(w, http.StatusOK, map[string]string{"status": "ok"}) diff --git a/cmd/server/server_test.go b/cmd/server/server_test.go index c5778e0..7b20190 100644 --- a/cmd/server/server_test.go +++ b/cmd/server/server_test.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "context" "encoding/json" "io" @@ -142,6 +143,31 @@ func TestProxyRejectsFifthInFlightRequest(t *testing.T) { require.Equal(t, http.StatusOK, requestForTest(h, http.MethodPost, "/v1/embeddings", `{"input":"after release"}`).Code) } +func TestRequestLogForInterruptedEmbeddingResponse(t *testing.T) { + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Length", "1024") + _, _ = io.WriteString(w, "private-partial-body") + })) + defer upstream.Close() + var logs bytes.Buffer + h := newServer(handlerForTest(t, upstream.URL, func() bool { return true }), &logs).Handler + w := requestForTest(h, http.MethodPost, "/v1/embeddings?private-query", `{"input":"private-input"}`) + require.Equal(t, http.StatusOK, w.Code) + require.Equal(t, "private-partial-body", w.Body.String()) + + require.Equal(t, 1, bytes.Count(logs.Bytes(), []byte("\n"))) + require.NotContains(t, logs.String(), "private-") + var entry struct { + Service, Outcome, Reason string + Status int + } + require.NoError(t, json.Unmarshal(logs.Bytes(), &entry)) + require.Equal(t, "embedding", entry.Service) + require.Equal(t, http.StatusOK, entry.Status) + require.Equal(t, "stream_interrupted", entry.Outcome) + require.Equal(t, "response_copy_failed", entry.Reason) +} + func TestProxyDoesNotFollowRedirectsAndPreservesUpstreamErrors(t *testing.T) { var calls atomic.Int32 upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/go.mod b/go.mod index a32e4ed..a2ca050 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/appthrust/embedding go 1.26.0 require ( - github.com/appthrust/appserver-go v0.3.0 + github.com/appthrust/appserver-go v0.7.0 github.com/stretchr/testify v1.11.1 ) diff --git a/go.sum b/go.sum index d059bbb..851a385 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/appthrust/appserver-go v0.3.0 h1:zxOz0hkspZSXjLq3KWGDLkZIz42Jag6wqRujZ8lTru0= -github.com/appthrust/appserver-go v0.3.0/go.mod h1:0KSf9RQZ6OxBcE0lxYzzjKF37nfwbKps3wXUGlE0oFo= +github.com/appthrust/appserver-go v0.7.0 h1:Fd1G+l/Ys0aAAiI2ZmMKqAz4qY6Dv7WwsF/rZtUgPUA= +github.com/appthrust/appserver-go v0.7.0/go.mod h1:0KSf9RQZ6OxBcE0lxYzzjKF37nfwbKps3wXUGlE0oFo= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=