Files
ai/object/prometheus_handler_metrics_test.go
zeekay 7c5a2985bc fix(metrics): serve DefaultRegistry at /v1/metrics (was empty-body 500)
GET api.hanzo.ai/v1/metrics 500'd with an empty body and Prometheus
content-type. The gateway proxies /v1/metrics to this service (Casdoor
fork); the 500 originated in ApiController.GetMetrics, not the gateway's
OpenCensus exporter.

Two defects made the endpoint wrong:

1. GetMetrics served metric.Handler(), which luxfi/metric implements as
   HandlerFor(NewRegistry()) — a fresh, throwaway registry. It NEVER sees
   the DefaultRegistry that object/prometheus.go records into, so the
   scrape is always empty. GetPrometheusInfo already reads DefaultRegistry;
   the two disagreed. Bind both to the one registry via a new
   object.MetricsHandler() = metric.HandlerFor(metric.DefaultRegistry).

2. luxfi/metric is no-op unless built with `-tags metrics` (its documented
   design). The Dockerfile built ./cmd/aid without the tag, so every metric
   was a no-op and DefaultRegistry gathered nothing. Add -tags metrics.

Verified end-to-end: curl 127.0.0.1/v1/metrics -> 200,
Content-Type text/plain; version=0.0.4; charset=utf-8, valid exposition
(cloud_cpu_usage / cloud_memory_usage / cloud_api_throughput). Regression
tests: TestMetricsHandlerContract (never-500 in any build) and, under
-tags metrics, TestMetricsHandlerServesDefaultRegistry (non-empty output).
2026-07-04 19:22:38 -07:00

51 lines
1.7 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build metrics
package object
import (
"net/http/httptest"
"strings"
"testing"
)
// TestMetricsHandlerServesDefaultRegistry proves the fix under the shipping
// build (`-tags metrics`, as set in the Dockerfile): GET /v1/metrics exposes the
// SAME DefaultRegistry the app records into, so a recorded series shows up as
// non-empty, valid Prometheus output — instead of the throwaway empty registry
// metric.Handler() used to bind, which answered 200 with a zero-length body.
func TestMetricsHandlerServesDefaultRegistry(t *testing.T) {
ApiThroughput.WithLabelValues("/v1/chat", "POST").Set(42)
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/v1/metrics", nil)
MetricsHandler().ServeHTTP(rr, req)
if rr.Code != 200 {
t.Fatalf("status = %d, want 200", rr.Code)
}
body := rr.Body.String()
if body == "" {
t.Fatal("empty body: handler is not bound to the DefaultRegistry the app records into")
}
for _, want := range []string{"# HELP ", "# TYPE ", "cloud_api_throughput"} {
if !strings.Contains(body, want) {
t.Fatalf("body missing %q\n----- body -----\n%s", want, body)
}
}
}