Files
ai-disclosure-standard/internal/app/metrics.go
T
2026-07-20 21:03:05 +02:00

43 lines
2.0 KiB
Go

package app
import (
"fmt"
"net/http"
"sync/atomic"
"time"
)
type metrics struct {
startedAt time.Time
requests atomic.Uint64
badgeRenders atomic.Uint64
validationRequests atomic.Uint64
validationFailures atomic.Uint64
panics atomic.Uint64
}
func newMetrics() *metrics { return &metrics{startedAt: time.Now()} }
func (m *metrics) serveHTTP(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
fmt.Fprintf(w, "# HELP ai_disclosure_uptime_seconds Process uptime in seconds.\n")
fmt.Fprintf(w, "# TYPE ai_disclosure_uptime_seconds gauge\n")
fmt.Fprintf(w, "ai_disclosure_uptime_seconds %.0f\n", time.Since(m.startedAt).Seconds())
fmt.Fprintf(w, "# HELP ai_disclosure_http_requests_total Total HTTP requests.\n")
fmt.Fprintf(w, "# TYPE ai_disclosure_http_requests_total counter\n")
fmt.Fprintf(w, "ai_disclosure_http_requests_total %d\n", m.requests.Load())
fmt.Fprintf(w, "# HELP ai_disclosure_badge_renders_total Total rendered SVG badges.\n")
fmt.Fprintf(w, "# TYPE ai_disclosure_badge_renders_total counter\n")
fmt.Fprintf(w, "ai_disclosure_badge_renders_total %d\n", m.badgeRenders.Load())
fmt.Fprintf(w, "# HELP ai_disclosure_validation_requests_total Total validation requests.\n")
fmt.Fprintf(w, "# TYPE ai_disclosure_validation_requests_total counter\n")
fmt.Fprintf(w, "ai_disclosure_validation_requests_total %d\n", m.validationRequests.Load())
fmt.Fprintf(w, "# HELP ai_disclosure_validation_failures_total Failed validation requests.\n")
fmt.Fprintf(w, "# TYPE ai_disclosure_validation_failures_total counter\n")
fmt.Fprintf(w, "ai_disclosure_validation_failures_total %d\n", m.validationFailures.Load())
fmt.Fprintf(w, "# HELP ai_disclosure_panics_total Recovered handler panics.\n")
fmt.Fprintf(w, "# TYPE ai_disclosure_panics_total counter\n")
fmt.Fprintf(w, "ai_disclosure_panics_total %d\n", m.panics.Load())
}