From 43ecbbae5f7a2e0dd18e69bb574eac940f511688 Mon Sep 17 00:00:00 2001 From: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> Date: Tue, 29 Sep 2026 11:44:37 +0000 Subject: [PATCH] server: export the not-found and already-set API metrics The server counts not-found and already-set API results in countCallNotFound and countCallAlreadySet, but Metrics did not include either map, so they were never published. Signed-off-by: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> --- server/server.go | 2 ++ server/server_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/server/server.go b/server/server.go index 153f29c..04f7905 100644 --- a/server/server.go +++ b/server/server.go @@ -183,6 +183,8 @@ func (s *Server) Metrics() expvar.Var { m.Set("counter_api_bad_request", s.countCallBadRequest) m.Set("counter_api_forbidden", s.countCallForbidden) m.Set("counter_api_internal_error", s.countCallInternalError) + m.Set("counter_api_not_found", s.countCallNotFound) + m.Set("counter_api_already_set", s.countCallAlreadySet) return m } diff --git a/server/server_test.go b/server/server_test.go index 2d9c558..f236b72 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -163,3 +163,36 @@ func TestServerStatus(t *testing.T) { t.Errorf("DeleteVersion %v: unexpected error %v", ov2, err) } } + +func TestServerMetrics(t *testing.T) { + d := setectest.NewDB(t, nil) + v1 := d.MustPut(d.Superuser, "test", "v1") + + ss := setectest.NewServer(t, d, nil) + hs := httptest.NewServer(ss.Mux) + defer hs.Close() + + ctx := t.Context() + cli := setec.Client{Server: hs.URL, DoHTTP: hs.Client().Do} + + if sv, err := cli.GetVersion(ctx, "test", v1+1); !errors.Is(err, api.ErrNotFound) { + t.Fatalf("GetVersion %v: got (%v, %v), want error %v", v1+1, sv, err, api.ErrNotFound) + } + if err := cli.CreateVersion(ctx, "test", v1, []byte("again")); err == nil { + t.Fatalf("CreateVersion %v: unexpected success", v1) + } + + var got map[string]map[string]int + if err := json.Unmarshal([]byte(ss.Actual.Metrics().String()), &got); err != nil { + t.Fatalf("Decode metrics: %v", err) + } + for _, tc := range []struct{ metric, method string }{ + {"counter_api_calls", "/api/get"}, + {"counter_api_not_found", "/api/get"}, + {"counter_api_already_set", "/api/create-version"}, + } { + if n := got[tc.metric][tc.method]; n != 1 { + t.Errorf("Metric %s[%s]: got %d, want 1", tc.metric, tc.method, n) + } + } +}