From 429ddd3407661a86b48c643c5a5dc6437bafddbf Mon Sep 17 00:00:00 2001 From: reoring Date: Thu, 19 Nov 2026 07:38:14 +0900 Subject: [PATCH 1/2] Log one structured line per HTTP request Wrap the server handler with appserver-go requestlog and record the failures that only wrote a response. (appserver-go pinned to the PR commit until v0.7.0 is tagged.) --- cmd/server/internal/api/api.go | 8 +++++- cmd/server/internal/api/avatars.go | 5 +++- cmd/server/internal/api/issues.go | 5 ++-- cmd/server/internal/api/people.go | 2 ++ cmd/server/main.go | 11 +++++++- cmd/server/server.go | 4 ++- cmd/server/server_test.go | 40 ++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 4 +-- 9 files changed, 72 insertions(+), 9 deletions(-) diff --git a/cmd/server/internal/api/api.go b/cmd/server/internal/api/api.go index c980e83..d93edbb 100644 --- a/cmd/server/internal/api/api.go +++ b/cmd/server/internal/api/api.go @@ -15,6 +15,7 @@ import ( "github.com/appthrust/appserver-go/appserver/httpx" "github.com/appthrust/appserver-go/appserver/identity" "github.com/appthrust/appserver-go/appserver/platformclient" + "github.com/appthrust/appserver-go/appserver/requestlog" "github.com/appthrust/tracker/cmd/server/internal/directory" "github.com/appthrust/tracker/cmd/server/internal/domain" "github.com/appthrust/tracker/cmd/server/internal/store" @@ -156,7 +157,9 @@ func (a *API) route(pattern string, mutation, roles, unregistered bool, fn endpo return } if download, ok := body.(attachmentDownload); ok { - download.write(w) + if err := download.write(w); err != nil { + requestlog.SetOutcome(r, "incomplete", "attachment_write_failed") + } return } if text, ok := body.(markdown); ok { @@ -166,6 +169,9 @@ func (a *API) route(pattern string, mutation, roles, unregistered bool, fn endpo return } if replay, ok := body.(store.Replay); ok { + if replay.Status >= http.StatusInternalServerError { + requestlog.SetOutcome(r, "error", "idempotent_response_failed") + } w.Header().Set("Content-Type", "application/json") w.WriteHeader(replay.Status) _, _ = w.Write(replay.Body) diff --git a/cmd/server/internal/api/avatars.go b/cmd/server/internal/api/avatars.go index d7074e1..0c746fe 100644 --- a/cmd/server/internal/api/avatars.go +++ b/cmd/server/internal/api/avatars.go @@ -13,6 +13,7 @@ import ( "github.com/appthrust/appserver-go/appserver/httpx" "github.com/appthrust/appserver-go/appserver/link" + "github.com/appthrust/appserver-go/appserver/requestlog" ) // Avatars use Directory's data and authority, not a Tracker transaction. @@ -73,5 +74,7 @@ func (a *API) avatar(w http.ResponseWriter, r *http.Request) { } w.Header().Set("Content-Type", contentType) w.Header().Set("Content-Length", strconv.Itoa(len(data))) - _, _ = w.Write(data) + if _, err := w.Write(data); err != nil { + requestlog.SetOutcome(r, "incomplete", "avatar_write_failed") + } } diff --git a/cmd/server/internal/api/issues.go b/cmd/server/internal/api/issues.go index 892c6d7..22a56a8 100644 --- a/cmd/server/internal/api/issues.go +++ b/cmd/server/internal/api/issues.go @@ -156,13 +156,14 @@ type attachmentDownload struct { Data []byte } -func (d attachmentDownload) write(w http.ResponseWriter) { +func (d attachmentDownload) write(w http.ResponseWriter) error { w.Header().Set("Content-Type", d.Attachment.MediaType) w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": d.Attachment.Filename})) w.Header().Set("Cache-Control", "private, max-age=31536000, immutable") w.Header().Set("X-Content-Type-Options", "nosniff") w.Header().Set("Content-Length", strconv.Itoa(len(d.Data))) - _, _ = io.Copy(w, bytes.NewReader(d.Data)) + _, err := io.Copy(w, bytes.NewReader(d.Data)) + return err } func (a *API) attachmentGet(s *request) (any, int, error) { item, data, err := s.tx.Attachment(s.r.PathValue("id"), true) diff --git a/cmd/server/internal/api/people.go b/cmd/server/internal/api/people.go index d0d171d..22bb564 100644 --- a/cmd/server/internal/api/people.go +++ b/cmd/server/internal/api/people.go @@ -1,6 +1,7 @@ package api import ( + "github.com/appthrust/appserver-go/appserver/requestlog" "github.com/appthrust/tracker/cmd/server/internal/domain" ) @@ -16,6 +17,7 @@ func (a *API) people(s *request) (any, int, error) { } list, err := a.directory.Search(s.r.Context(), s.bearer, q) if err != nil { + requestlog.SetOutcome(s.r, "degraded", "directory_search_unavailable") return peopleResponse{Items: []domain.Person{}, Source: "unavailable"}, 200, nil } return peopleResponse{Items: list, Source: "directory"}, 200, nil diff --git a/cmd/server/main.go b/cmd/server/main.go index 18fe38d..7b2a3d0 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: "tracker"}), + ReadHeaderTimeout: 10 * time.Second, + } +} diff --git a/cmd/server/server.go b/cmd/server/server.go index 732af86..710051c 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/tracker/cmd/server/internal/api" "net/http" "net/url" @@ -44,7 +45,8 @@ func newHandler(staticDir, upstreamURL, projectID string, tracker http.Handler, writeJSON(w, http.StatusOK, map[string]string{"projectId": projectID, "publicUrl": os.Getenv("TRACKER_PUBLIC_URL"), "directoryUrl": os.Getenv("TRACKER_DIRECTORY_PUBLIC_URL")}) }) mux.Handle("GET /launcher", launcher.NewHandler(catalog, authenticator.Authenticate)) - mux.Handle("GET /releasez", releaseidentity.NewHandler("TRACKER", func(w http.ResponseWriter, _ *http.Request) { + mux.Handle("GET /releasez", releaseidentity.NewHandler("TRACKER", 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 08dfda8..8b5501e 100644 --- a/cmd/server/server_test.go +++ b/cmd/server/server_test.go @@ -1,9 +1,11 @@ package main import ( + "bytes" "encoding/json" "net/http" "net/http/httptest" + "os" "testing" "github.com/stretchr/testify/require" @@ -88,3 +90,41 @@ func TestPendingSurfaceAndRelease(t *testing.T) { require.Equal(t, status, w.Code, path) } } + +func TestRequestLogForUnavailableRelease(t *testing.T) { + for _, key := range []string{"TRACKER_RELEASE_VERSION", "TRACKER_SOURCE_REVISION", "TRACKER_ARTIFACT_DIGEST", "PLATFORM_OIDC_ISSUER", "PLATFORM_OIDC_ISSUER_CA_PEM"} { + t.Setenv(key, "") + } + mux, err := newHandler(t.TempDir(), "http://127.0.0.1:1", "pj-test", nil, nil) + require.NoError(t, err) + + output, err := os.CreateTemp(t.TempDir(), "stderr") + require.NoError(t, err) + defer output.Close() + // Wrap captures stderr at construction; restore it before serving the request. + stderr := os.Stderr + os.Stderr = output + server := newServer(":0", mux) + os.Stderr = stderr + + w := httptest.NewRecorder() + server.Handler.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/releasez?private=query-marker", nil)) + require.Equal(t, http.StatusServiceUnavailable, w.Code) + require.Equal(t, "UI release status unavailable\n", w.Body.String()) + + line, err := os.ReadFile(output.Name()) + require.NoError(t, err) + require.Equal(t, 1, bytes.Count(line, []byte{'\n'})) + require.NotContains(t, string(line), "query-marker") + var entry struct { + Msg, Service, Path, Outcome, Reason string + Status int + } + require.NoError(t, json.Unmarshal(line, &entry)) + require.Equal(t, "request", entry.Msg) + require.Equal(t, "tracker", entry.Service) + require.Equal(t, "/releasez", entry.Path) + require.Equal(t, w.Code, entry.Status) + require.Equal(t, "unavailable", entry.Outcome) + require.Equal(t, "release_identity_unavailable", entry.Reason) +} diff --git a/go.mod b/go.mod index 6e11e91..2b94cdd 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/appthrust/tracker go 1.26.0 require ( - github.com/appthrust/appserver-go v0.5.1 + github.com/appthrust/appserver-go v0.6.1-0.20261118223418-5f8bf82f7a56 github.com/lib/pq v1.12.3 github.com/oklog/ulid/v2 v2.1.1 github.com/stretchr/testify v1.11.1 diff --git a/go.sum b/go.sum index 1cbbb1a..c350d72 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/appthrust/appserver-go v0.5.1 h1:L9IfLUZjR2GxQmEK+d7rjo8P1bItbbb+kqKotRRoey4= -github.com/appthrust/appserver-go v0.5.1/go.mod h1:0KSf9RQZ6OxBcE0lxYzzjKF37nfwbKps3wXUGlE0oFo= +github.com/appthrust/appserver-go v0.6.1-0.20261118223418-5f8bf82f7a56 h1:JKydZpovuh/gTTwMH63iDRKtPa2gjnDEUAbCF7VMa54= +github.com/appthrust/appserver-go v0.6.1-0.20261118223418-5f8bf82f7a56/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= From 23daddfe66122752526648c045ecfac9395bc3e6 Mon Sep 17 00:00:00 2001 From: reoring Date: Thu, 19 Nov 2026 07:51:32 +0900 Subject: [PATCH 2/2] Pin appserver-go v0.7.0 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 2b94cdd..f175e9f 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/appthrust/tracker go 1.26.0 require ( - github.com/appthrust/appserver-go v0.6.1-0.20261118223418-5f8bf82f7a56 + github.com/appthrust/appserver-go v0.7.0 github.com/lib/pq v1.12.3 github.com/oklog/ulid/v2 v2.1.1 github.com/stretchr/testify v1.11.1 diff --git a/go.sum b/go.sum index c350d72..e5c75ef 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/appthrust/appserver-go v0.6.1-0.20261118223418-5f8bf82f7a56 h1:JKydZpovuh/gTTwMH63iDRKtPa2gjnDEUAbCF7VMa54= -github.com/appthrust/appserver-go v0.6.1-0.20261118223418-5f8bf82f7a56/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=