Files
ai/object/prometheus_handler_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

48 lines
1.8 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.
package object
import (
"net/http/httptest"
"strings"
"testing"
metric "github.com/luxfi/metric"
)
// TestMetricsHandlerContract locks the invariant that GET /v1/metrics answers
// 200 in the Prometheus text-exposition format and never 500s, in ANY build
// (metrics compiled in or no-op). Regression guard for the live incident where
// the endpoint returned HTTP 500 with a zero-length body.
func TestMetricsHandlerContract(t *testing.T) {
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/v1/metrics", nil)
MetricsHandler().ServeHTTP(rr, req)
if rr.Code != 200 {
t.Fatalf("status = %d, want 200 (body=%q)", rr.Code, rr.Body.String())
}
if ct := rr.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/plain; version=0.0.4") {
t.Fatalf("content-type = %q, want prometheus text exposition", ct)
}
// The handler must be bound to the same registry the app records into, and
// that registry must gather cleanly — a gather error is exactly what the
// upstream promhttp handler turned into the empty-body 500.
if _, err := metric.DefaultRegistry.Gather(); err != nil {
t.Fatalf("DefaultRegistry.Gather() error = %v, want nil", err)
}
}