Files
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

55 lines
1.5 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
// Portions Copyright 2025 The OpenAgent Authors. 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 controllers
import (
"github.com/hanzoai/ai/object"
)
// GetPrometheusInfo
// @Title GetPrometheusInfo
// @Tag System API
// @Description get Prometheus Info
// @Success 200 {object} object.PrometheusInfo The Response object
// @router /get-prometheus-info [get]
func (c *ApiController) GetPrometheusInfo() {
if !c.RequireAdmin() {
return
}
prometheusInfo, err := object.GetPrometheusInfo()
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(prometheusInfo)
}
// GetMetrics
// @Title GetMetrics
// @Tag System API
// @Description get Prometheus metrics
// @Success 200 {string} string The Response metrics in Prometheus format
// @router /metrics [get]
func (c *ApiController) GetMetrics() {
if !c.RequireAdmin() {
return
}
object.MetricsHandler().ServeHTTP(c.Ctx.ResponseWriter, c.Ctx.Request)
}