diff --git a/cmd/server/internal/api/api.go b/cmd/server/internal/api/api.go index 17207c2..fdf4c0e 100644 --- a/cmd/server/internal/api/api.go +++ b/cmd/server/internal/api/api.go @@ -107,7 +107,7 @@ func Unavailable() http.Handler { err := httpx.Fail(503, "console_store_unavailable", "The console database is not available") if strings.HasPrefix(r.URL.Path, "/api/console/v1/build-sessions") { w.Header().Set("Cache-Control", "no-store") - writeBuildError(w, err) + writeBuildError(w, r, err) return } httpx.WriteError(w, err, "console") @@ -118,7 +118,7 @@ func (a *API) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "no-store") if a.links == nil && strings.HasPrefix(r.URL.Path, "/api/console/") && r.URL.Path != "/api/console/openapi.json" { if strings.HasPrefix(r.URL.Path, "/api/console/v1/build-sessions") { - writeBuildError(w, directoryError(link.ErrUnavailable)) + writeBuildError(w, r, directoryError(link.ErrUnavailable)) return } httpx.WriteError(w, directoryError(link.ErrUnavailable), "console") @@ -155,7 +155,7 @@ func (a *API) routeHandlerMode(mutation, needsRoles, transactional, fresh bool, if errors.Is(err, identity.ErrUnavailable) { status = http.StatusServiceUnavailable } - writeBuildError(w, httpx.Fail(status, "unauthorized", "Authentication is required")) + writeBuildError(w, r, httpx.Fail(status, "unauthorized", "Authentication is required")) return } if errors.Is(err, identity.ErrUnavailable) { @@ -192,11 +192,11 @@ func (a *API) routeHandlerMode(mutation, needsRoles, transactional, fresh bool, if err != nil { var rejection *domain.BuildRejection if errors.As(err, &rejection) { - writeBuildError(w, err) + writeBuildError(w, r, err) return } if !transactional { - writeBuildError(w, err) + writeBuildError(w, r, err) return } httpx.WriteError(w, directoryError(err), "console") diff --git a/cmd/server/internal/api/build_request_test.go b/cmd/server/internal/api/build_request_test.go index f11358d..14a7841 100644 --- a/cmd/server/internal/api/build_request_test.go +++ b/cmd/server/internal/api/build_request_test.go @@ -40,7 +40,7 @@ func TestBuildRequestScalarAndCanonicalNumberContract(t *testing.T) { func TestBuildPackedPromptLimitReturns413(t *testing.T) { w := httptest.NewRecorder() - writeBuildError(w, prompts.ErrInputTooLarge) + writeBuildError(w, httptest.NewRequest("POST", "/", nil), prompts.ErrInputTooLarge) require.Equal(t, 413, w.Code) require.Equal(t, "no-store", w.Header().Get("Cache-Control")) var body BuildError diff --git a/cmd/server/internal/api/build_sessions.go b/cmd/server/internal/api/build_sessions.go index 0fb4f7f..ab81555 100644 --- a/cmd/server/internal/api/build_sessions.go +++ b/cmd/server/internal/api/build_sessions.go @@ -18,6 +18,7 @@ import ( "unicode/utf8" "github.com/appthrust/appserver-go/appserver/httpx" + "github.com/appthrust/appserver-go/appserver/requestlog" "github.com/appthrust/builder-console/cmd/server/internal/buildsession" "github.com/appthrust/builder-console/cmd/server/internal/conversation/prompts" "github.com/appthrust/builder-console/cmd/server/internal/domain" @@ -107,7 +108,7 @@ func (a *API) registerBuildSessions() { a.mux.HandleFunc(route.pattern, a.routeHandlerMode(false, false, false, true, route.handler)) } } -func writeBuildError(w http.ResponseWriter, err error) { +func writeBuildError(w http.ResponseWriter, r *http.Request, err error) { status := 503 body := BuildError{Error: "service_unavailable", Message: "The conversation service is temporarily unavailable"} var upstream *buildsession.Failure @@ -119,6 +120,8 @@ func writeBuildError(w http.ResponseWriter, err error) { body.Error = "input_too_large" body.Message = "The complete conversation inputs exceed the request limit" case errors.As(err, &upstream): + // Failure.Code is normalized by buildsession; never record upstream details. + requestlog.SetError(r, &httpx.Error{Status: upstream.Status, Code: upstream.Code}) status = upstream.Status body.Error = upstream.Code body.Message = upstream.Message @@ -153,6 +156,7 @@ func writeBuildError(w http.ResponseWriter, err error) { } } case errors.As(err, &shared): + requestlog.SetError(r, shared) status = shared.Status body.Message = shared.Message switch { @@ -169,6 +173,9 @@ func writeBuildError(w http.ResponseWriter, err error) { default: body.Error = "service_unavailable" } + default: + // Unknown errors can contain request data or upstream diagnostics. + requestlog.SetOutcome(r, "failed", "build_session_unavailable") } if status == http.StatusTooManyRequests || status == http.StatusServiceUnavailable { retryAfter := "" diff --git a/cmd/server/internal/api/build_sessions_test.go b/cmd/server/internal/api/build_sessions_test.go index 69d96cd..8ed4349 100644 --- a/cmd/server/internal/api/build_sessions_test.go +++ b/cmd/server/internal/api/build_sessions_test.go @@ -409,7 +409,7 @@ func TestBuildErrorsHaveBoundedRetryAfter(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { w := httptest.NewRecorder() - writeBuildError(w, tc.err) + writeBuildError(w, httptest.NewRequest("GET", "/", nil), tc.err) require.Equal(t, tc.want, w.Header().Get("Retry-After")) if tc.name == "permanent failure" { var body BuildError diff --git a/cmd/server/internal/api/directory_proxy.go b/cmd/server/internal/api/directory_proxy.go index 6a39c76..9f45d31 100644 --- a/cmd/server/internal/api/directory_proxy.go +++ b/cmd/server/internal/api/directory_proxy.go @@ -2,6 +2,7 @@ package api import ( "context" + "errors" "net/http" "net/http/httputil" "net/url" @@ -10,10 +11,11 @@ import ( "github.com/appthrust/appserver-go/appserver/httpx" "github.com/appthrust/appserver-go/appserver/link" + "github.com/appthrust/appserver-go/appserver/requestlog" ) func (a *API) directoryProxy() http.Handler { - proxy := &httputil.ReverseProxy{ + proxy := WithProxyOutcomes(&httputil.ReverseProxy{ Rewrite: func(r *httputil.ProxyRequest) { r.Out.Host = r.Out.URL.Host r.Out.URL.Path = "/api/directory/v1/" + strings.TrimPrefix(r.In.URL.Path, "/api/console/directory/") @@ -32,10 +34,13 @@ func (a *API) directoryProxy() http.Handler { } return nil }, - ErrorHandler: func(w http.ResponseWriter, _ *http.Request, _ error) { + ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) { + if !errors.Is(err, link.ErrUnavailable) { + requestlog.SetOutcome(r, "upstream_error", "directory_proxy_failed") + } httpx.WriteError(w, directoryError(link.ErrUnavailable), "console") }, - } + }) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("Authorization") == "" { httpx.WriteError(w, httpx.Fail(401, "unauthenticated", "A valid bearer token is required"), "console") @@ -65,3 +70,35 @@ func (a *API) directoryProxy() http.Handler { proxy.ServeHTTP(w, r) }) } + +type upstreamStatusTransport struct{ next http.RoundTripper } + +func (t upstreamStatusTransport) RoundTrip(r *http.Request) (*http.Response, error) { + next := t.next + if next == nil { + next = http.DefaultTransport + } + resp, err := next.RoundTrip(r) + if err == nil && resp.StatusCode >= 400 { + requestlog.SetOutcome(r, "upstream_error", "upstream_http_status") + } + return resp, err +} + +// WithProxyOutcomes records upstream rejections without inspecting their bodies. +// Configure the proxy before serving requests. +func WithProxyOutcomes(proxy *httputil.ReverseProxy) http.Handler { + proxy.Transport = upstreamStatusTransport{next: proxy.Transport} + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // ReverseProxy aborts a partial stream by panicking with http.ErrAbortHandler + // instead of calling ErrorHandler. Do not intercept or change the panic. + complete := false + defer func() { + if !complete { + requestlog.SetOutcome(r, "aborted", "proxy_response_aborted") + } + }() + proxy.ServeHTTP(w, r) + complete = true + }) +} diff --git a/cmd/server/internal/api/draft_reviewers.go b/cmd/server/internal/api/draft_reviewers.go index 8ae8913..d0d0c4d 100644 --- a/cmd/server/internal/api/draft_reviewers.go +++ b/cmd/server/internal/api/draft_reviewers.go @@ -12,6 +12,7 @@ import ( "github.com/appthrust/appserver-go/appserver/httpx" "github.com/appthrust/appserver-go/appserver/identity" + "github.com/appthrust/appserver-go/appserver/requestlog" "github.com/appthrust/builder-console/cmd/server/internal/domain" ) @@ -25,7 +26,7 @@ func WithDraftReviewers(next http.Handler, projectID string, current func() *API } payload, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 1<<20)) if err != nil { - writeDraftError(w, httpx.Invalid("Request body exceeds the size limit")) + writeDraftError(w, r, httpx.Invalid("Request body exceeds the size limit")) return } var body map[string]json.RawMessage @@ -37,7 +38,7 @@ func WithDraftReviewers(next http.Handler, projectID string, current func() *API if kind == "improvement" { a := current() if a == nil { - writeDraftError(w, httpx.Fail(503, "console_store_unavailable", "The console database is not available")) + writeDraftError(w, r, httpx.Fail(503, "console_store_unavailable", "The console database is not available")) return } ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second) @@ -45,14 +46,14 @@ func WithDraftReviewers(next http.Handler, projectID string, current func() *API claims, bearer, err := a.identity.Authenticate(ctx, r.Header.Get("Authorization")) if err != nil { if errors.Is(err, identity.ErrUnavailable) { - writeDraftError(w, httpx.Fail(503, "identity_unavailable", "The identity provider is temporarily unavailable")) + writeDraftError(w, r, httpx.Fail(503, "identity_unavailable", "The identity provider is temporarily unavailable")) return } if errors.Is(err, identity.ErrRawKeyNotAccepted) { - writeDraftError(w, httpx.Fail(401, "raw_key_not_accepted", "Exchange the access key for an assertion before calling this API")) + writeDraftError(w, r, httpx.Fail(401, "raw_key_not_accepted", "Exchange the access key for an assertion before calling this API")) return } - writeDraftError(w, httpx.Fail(401, "unauthenticated", "A valid bearer token is required")) + writeDraftError(w, r, httpx.Fail(401, "unauthenticated", "A valid bearer token is required")) return } err = a.withRequest(ctx, claims, bearer, false, true, false, false, func(s *request) error { @@ -74,12 +75,12 @@ func WithDraftReviewers(next http.Handler, projectID string, current func() *API return httpx.NotFound() }) if err != nil { - writeDraftError(w, directoryError(err)) + writeDraftError(w, r, directoryError(err)) return } payload, err = json.Marshal(body) if err != nil { - writeDraftError(w, httpx.Invalid("Invalid draft request")) + writeDraftError(w, r, httpx.Invalid("Invalid draft request")) return } } @@ -104,12 +105,15 @@ func ownerSubjects(owners []domain.Person) []string { // This middleware is on /api/v1, so errors use the Platform API envelope, not // the flat console envelope. The SPA must be able to distinguish prerequisites. -func writeDraftError(w http.ResponseWriter, err error) { +func writeDraftError(w http.ResponseWriter, r *http.Request, err error) { var detail *httpx.Error if !errors.As(err, &detail) { - log.Printf("console draft request failed: %v", err) + if requestlog.ID(r.Context()) == "" { + log.Printf("console draft request failed: %v", err) + } detail = &httpx.Error{Status: 500, Code: "internal_error", Message: "The console could not complete this request"} } + requestlog.SetError(r, detail) type errorDetail struct { Code string `json:"code"` Message string `json:"message"` diff --git a/cmd/server/main.go b/cmd/server/main.go index 9fe1d87..376a61f 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -12,6 +12,7 @@ import ( "time" "github.com/appthrust/appserver-go/appserver/platformenv" + "github.com/appthrust/appserver-go/appserver/requestlog" ) func main() { @@ -43,7 +44,7 @@ func main() { if err != nil { log.Fatal(err) } - server := &http.Server{Addr: *listen, Handler: handler, ReadHeaderTimeout: 10 * time.Second} + server := newServer(*listen, handler) ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() go func() { @@ -59,3 +60,11 @@ func main() { log.Fatal(err) } } + +func newServer(addr string, handler http.Handler) *http.Server { + return &http.Server{ + Addr: addr, + Handler: requestlog.Wrap(handler, requestlog.Options{Service: "builder-console"}), + ReadHeaderTimeout: 10 * time.Second, + } +} diff --git a/cmd/server/server.go b/cmd/server/server.go index 53c2e92..c5deb49 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -9,6 +9,7 @@ import ( "github.com/appthrust/appserver-go/appserver/platformclient" "github.com/appthrust/appserver-go/appserver/platformenv" "github.com/appthrust/appserver-go/appserver/releaseidentity" + "github.com/appthrust/appserver-go/appserver/requestlog" "github.com/appthrust/builder-console/cmd/server/internal/api" "net/http" "net/http/httputil" @@ -40,7 +41,7 @@ func newHandler(staticDir, upstreamURL, projectID string, console http.Handler, return nil, err } catalog := launcher.NewCatalog(platformclient.New(upstreamURL, projectID, platformclient.WithHTTPClient(authenticator.HTTPClient())), "builder-console") - proxy := &httputil.ReverseProxy{ + proxy := api.WithProxyOutcomes(&httputil.ReverseProxy{ Rewrite: func(r *httputil.ProxyRequest) { // Only replace the origin: /api paths and query strings stay intact. r.Out.URL.Scheme = upstream.Scheme @@ -55,10 +56,11 @@ func newHandler(staticDir, upstreamURL, projectID string, console http.Handler, }, // Flush every AI runtime NDJSON event, not only event-stream responses. FlushInterval: -1, - ErrorHandler: func(w http.ResponseWriter, _ *http.Request, _ error) { + ErrorHandler: func(w http.ResponseWriter, r *http.Request, _ error) { + requestlog.SetOutcome(r, "upstream_error", "platform_proxy_failed") writeJSON(w, http.StatusBadGateway, map[string]string{"error": "upstream_unavailable"}) }, - } + }) // The Dashboard shares the Platform API origin; its // Team settings page is where the データ tab sends membership gaps. config := struct { @@ -82,7 +84,8 @@ func newHandler(staticDir, upstreamURL, projectID string, console http.Handler, writeJSON(w, http.StatusOK, response) }) mux.Handle("GET /launcher", launcher.NewHandler(catalog, authenticator.Authenticate)) - mux.Handle("GET /releasez", releaseidentity.NewHandler("BUILDER_CONSOLE", func(w http.ResponseWriter, _ *http.Request) { + mux.Handle("GET /releasez", releaseidentity.NewHandler("BUILDER_CONSOLE", func(w http.ResponseWriter, r *http.Request) { + requestlog.SetOutcome(r, "unavailable", "release_identity_unavailable") w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.WriteHeader(http.StatusServiceUnavailable) _, _ = w.Write([]byte("UI release status unavailable\n")) diff --git a/cmd/server/server_test.go b/cmd/server/server_test.go index 5d220d9..82df5d6 100644 --- a/cmd/server/server_test.go +++ b/cmd/server/server_test.go @@ -181,13 +181,38 @@ func TestUnauthenticated(t *testing.T) { func TestUpstreamUnavailable(t *testing.T) { upstream := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})) upstream.Close() + h := testHandler(t, upstream.URL) + output, err := os.CreateTemp(t.TempDir(), "request-log") + if err != nil { + t.Fatal(err) + } + defer output.Close() + // Wrap binds stderr when the production server is constructed. + stderr := os.Stderr + os.Stderr = output + server := newServer(":0", h) + os.Stderr = stderr w := httptest.NewRecorder() - r := httptest.NewRequest(http.MethodGet, "/api/v1/projects", nil) + r := httptest.NewRequest(http.MethodGet, "/api/v1/projects?private=query-canary", nil) r.Header.Set("Authorization", "Bearer x") - testHandler(t, upstream.URL).ServeHTTP(w, r) + server.Handler.ServeHTTP(w, r) if w.Code != 502 || strings.TrimSpace(w.Body.String()) != `{"error":"upstream_unavailable"}` { t.Fatalf("status=%d body=%s", w.Code, w.Body) } + line, err := os.ReadFile(output.Name()) + if err != nil { + t.Fatal(err) + } + var entry struct { + Service, Outcome, Reason string + Status int + } + if err := json.Unmarshal(line, &entry); err != nil || bytes.Count(line, []byte{'\n'}) != 1 { + t.Fatalf("expected one JSON log line: %q", line) + } + if entry.Service != "builder-console" || entry.Status != 502 || entry.Outcome != "upstream_error" || entry.Reason != "platform_proxy_failed" || bytes.Contains(line, []byte("query-canary")) { + t.Fatalf("unexpected request log: %s", line) + } } func TestSSEIncremental(t *testing.T) { diff --git a/go.mod b/go.mod index 1bcc9c3..f866288 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/appthrust/builder-console go 1.26.0 require ( - github.com/appthrust/appserver-go v0.5.0 + github.com/appthrust/appserver-go v0.7.0 github.com/google/uuid v1.6.0 github.com/lib/pq v1.12.3 github.com/santhosh-tekuri/jsonschema/v6 v6.0.2 diff --git a/go.sum b/go.sum index 239ad82..a8b6ecc 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/appthrust/appserver-go v0.5.0 h1:MrQBL5rMvBEOsGc9vGiGV3cVOEZuSjIf2CkismPO2mo= -github.com/appthrust/appserver-go v0.5.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/coreos/go-oidc/v3 v3.20.0 h1:EtE0WIBHk03N+DqGkY4+UONzzZHk7amKt6IyNd7OsZE= github.com/coreos/go-oidc/v3 v3.20.0/go.mod h1:DYCf24+ncYi+XkIH97GY1+dqoRlbaSI26KVTCI9SrY4= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=