feat(api): Prometheus metrics for operators
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 10m23s
ci / test (pull_request) Successful in 12m59s
ci / build (pull_request) Canceled after 7m17s
ci / image (pull_request) Canceled after 0s

The API serves Prometheus metrics on a listener of its own
(UPTIME_API_METRICS_LISTEN, default :9090, "off" disables it), so the
public ingress never exposes them:

- requests by route and status, with durations except for long polls
  and agent streams
- dispatched checks, stored and rejected results per region, how late
  results arrive, live checkers and overdue checks per platform region
- notification and subscriber mail deliveries, incidents opened and
  resolved
- runs, duration and last success of the background jobs
- platform totals (orgs, users, monitors by type, status pages,
  subscribers, open incidents, pending notifications), read at most
  every 15 seconds when scraped
- the Postgres and Redis connection pools, Go runtime and build info

The Helm chart exposes the port on the API service and can add a
ServiceMonitor for the Prometheus Operator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-29 11:56:46 +01:00
1 parent ab25c1f218
commit e9984f8271
30 files changed
+1005 -20

No files matched your search

+28 -1
View File
@@ -6,6 +6,7 @@ import (
"context"
"errors"
"log/slog"
"net/http"
"os"
"time"
@@ -15,6 +16,7 @@ import (
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
"git.zb-server.de/ZB-Server/uptimesebi/internal/datapolicy"
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
"git.zb-server.de/ZB-Server/uptimesebi/internal/metrics"
"git.zb-server.de/ZB-Server/uptimesebi/internal/migrations"
"git.zb-server.de/ZB-Server/uptimesebi/internal/notify"
"git.zb-server.de/ZB-Server/uptimesebi/internal/platform"
@@ -80,12 +82,15 @@ func run(log *slog.Logger, level *slog.LevelVar, args []string) error {
if err != nil {
return err
}
srv := api.New(cfg, api.Deps{Log: log, DB: tenant.New(deps.DB), Redis: deps.Redis, Keys: keys, Catalog: catalog})
m := metrics.New(string(cfg.Edition))
m.Register(metrics.DBPool(deps.DB), metrics.RedisPool(deps.Redis))
srv := api.New(cfg, api.Deps{Log: log, DB: tenant.New(deps.DB), Redis: deps.Redis, Keys: keys, Catalog: catalog, Metrics: m})
health := platform.NewHealth(deps.Probes())
r := platform.NewRouter(log)
if err := platform.TrustProxies(r, cfg.TrustedProxies); err != nil {
return err
}
r.Use(m.HTTP(api.StreamingRoutes()...))
health.Register(r)
r.StaticFS("/static", web.Static())
srv.Mount(r)
@@ -97,9 +102,31 @@ func run(log *slog.Logger, level *slog.LevelVar, args []string) error {
go srv.RunDomainWatch(ctx)
go srv.RunHeartbeats(ctx)
if cfg.MetricsListen != config.MetricsOff {
go serveMetrics(ctx, log, cfg.MetricsListen, m)
}
return platform.Serve(ctx, log, platform.NewHTTPServer(cfg.Listen, r), health, 5*time.Second, cfg.ShutdownTimeout)
}
// serveMetrics serves /metrics on addr, apart from the public listener,
// until ctx ends. Failing to listen is logged but does not stop the API.
func serveMetrics(ctx context.Context, log *slog.Logger, addr string, m *metrics.Metrics) {
mux := http.NewServeMux()
mux.Handle("GET /metrics", m.Handler())
srv := platform.NewHTTPServer(addr, mux)
go func() {
<-ctx.Done()
sctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = srv.Shutdown(sctx)
}()
log.Info("metrics listening", "addr", addr)
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Error("serving metrics failed", "addr", addr, "error", err)
}
}
func migrate(ctx context.Context, log *slog.Logger, cfg config.API, args []string) error {
dsn := cfg.DatabaseURL
if dsn == "" {