diff --git a/cmd/server/main.go b/cmd/server/main.go index ef192a9..4ae42aa 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -5,6 +5,7 @@ import ( "errors" "flag" "fmt" + "io" "log" "net" "net/http" @@ -13,6 +14,8 @@ import ( "os/signal" "syscall" "time" + + "github.com/appthrust/appserver-go/appserver/requestlog" ) const ( @@ -65,6 +68,14 @@ func main() { } } +func newHTTPServer(upstream *url.URL, ready func() bool, logOutput io.Writer) *http.Server { + return &http.Server{ + Handler: requestlog.Wrap(newHandler(upstream, ready), requestlog.Options{Service: "inference", Output: logOutput}), + ReadHeaderTimeout: 10 * time.Second, ReadTimeout: requestTimeout, + WriteTimeout: requestTimeout + 5*time.Second, IdleTimeout: 60 * time.Second, + } +} + func serve(ctx context.Context, listen string, cfg config) error { listener, err := net.Listen("tcp", listen) if err != nil { @@ -78,11 +89,7 @@ func serve(ctx context.Context, listen string, cfg config) error { defer close(childDone) s.Run(childCtx) }() - server := &http.Server{ - Handler: newHandler(cfg.upstream, s.Ready), - ReadHeaderTimeout: 10 * time.Second, ReadTimeout: requestTimeout, - WriteTimeout: requestTimeout + 5*time.Second, IdleTimeout: 60 * time.Second, - } + server := newHTTPServer(cfg.upstream, s.Ready, os.Stderr) served := make(chan error, 1) go func() { served <- server.Serve(listener) }() log.Printf("inference-server listening on %s (project %s)", listener.Addr(), cfg.projectID) diff --git a/cmd/server/server.go b/cmd/server/server.go index a8ed35c..6afbe5e 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -14,6 +14,7 @@ import ( "github.com/appthrust/appserver-go/appserver/httpx" "github.com/appthrust/appserver-go/appserver/releaseidentity" + "github.com/appthrust/appserver-go/appserver/requestlog" ) const ( @@ -46,6 +47,9 @@ func newHandler(upstream *url.URL, ready func() bool) http.Handler { FlushInterval: -1, // Forward SSE chunks immediately, without buffering a completion. ModifyResponse: func(r *http.Response) error { r.Header.Set("Cache-Control", "no-store") + if r.StatusCode >= http.StatusBadRequest { + requestlog.SetOutcome(r.Request, "failed", "upstream_http_error") + } if r.Request.Method == http.MethodGet && r.Request.URL.Path == "/v1/models" && r.StatusCode == http.StatusOK { return advertisePromptBudget(r) } @@ -59,6 +63,18 @@ func newHandler(upstream *url.URL, ready func() bool) http.Handler { } }, } + forward := func(w http.ResponseWriter, r *http.Request) { + // ReverseProxy aborts on a copy failure after committing the status. + // Record the interruption without recovering or changing the panic. + completed := false + defer func() { + if !completed { + requestlog.SetOutcome(r, "interrupted", "proxy_aborted") + } + }() + proxy.ServeHTTP(w, r) + completed = true + } slots := make(chan struct{}, maxInFlight) modelJSON, _ := json.Marshal(servedModel) mux := http.NewServeMux() @@ -114,13 +130,13 @@ func newHandler(upstream *url.URL, ready func() bool) http.Handler { r = r.WithContext(ctx) r.Body = io.NopCloser(bytes.NewReader(payload)) r.ContentLength = int64(len(payload)) - proxy.ServeHTTP(w, r) + forward(w, r) }) for _, path := range []string{"/v1/models", "/metrics"} { mux.HandleFunc("GET "+path, func(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithTimeout(r.Context(), requestTimeout) defer cancel() - proxy.ServeHTTP(w, r.WithContext(ctx)) + forward(w, r.WithContext(ctx)) }) } mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, _ *http.Request) { diff --git a/cmd/server/server_test.go b/cmd/server/server_test.go index 05ff948..a098a76 100644 --- a/cmd/server/server_test.go +++ b/cmd/server/server_test.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "bytes" "context" "encoding/json" "io" @@ -33,6 +34,36 @@ func requestForTest(h http.Handler, method, path, body string) *httptest.Respons return w } +func TestRequestLogRecordsInterruptedProxyStream(t *testing.T) { + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Content-Length", "1024") + _, _ = io.WriteString(w, "data: private-stream-content\n\n") + })) + defer upstream.Close() + u, err := url.Parse(upstream.URL) + require.NoError(t, err) + var output bytes.Buffer + server := newHTTPServer(u, func() bool { return true }, &output) + r := httptest.NewRequest(http.MethodPost, "/v1/chat/completions?private-query", strings.NewReader(`{"stream":true}`)) + // ReverseProxy only aborts an interrupted copy when running in a server context. + r = r.WithContext(context.WithValue(r.Context(), http.ServerContextKey, server)) + w := httptest.NewRecorder() + require.PanicsWithValue(t, http.ErrAbortHandler, func() { server.Handler.ServeHTTP(w, r) }) + require.Equal(t, http.StatusOK, w.Code) + + var entry struct { + Service, Outcome, Reason string + Status int + } + require.NoError(t, json.Unmarshal(output.Bytes(), &entry)) + require.Equal(t, "inference", entry.Service) + require.Equal(t, http.StatusOK, entry.Status) + require.Equal(t, "interrupted", entry.Outcome) + require.Equal(t, "proxy_aborted", entry.Reason) + require.NotContains(t, output.String(), "private-") +} + func TestProxyForcesBakedModelAndOnlyForwardsPublicRoutes(t *testing.T) { requests := make(chan *http.Request, 8) bodies := make(chan map[string]any, 8) diff --git a/go.mod b/go.mod index 760650a..6f0f719 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/appthrust/inference 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=