summaryrefslogtreecommitdiff
path: root/internal/telemetry/metrics.go
diff options
context:
space:
mode:
authorChia <Chia@93.nz>2026-08-04 19:58:52 +1200
committerChia <Chia@93.nz>2026-08-04 20:43:23 +1200
commit5b651488b081b65fda8a323f228e139adb79a35d (patch)
tree08baf40efb8fe103b32721cd991ff712323e3173 /internal/telemetry/metrics.go
Build AI gateway control plane and admin UI
Diffstat (limited to '')
-rw-r--r--internal/telemetry/metrics.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/internal/telemetry/metrics.go b/internal/telemetry/metrics.go
new file mode 100644
index 0000000..4942d8d
--- /dev/null
+++ b/internal/telemetry/metrics.go
@@ -0,0 +1,44 @@
+package telemetry
+
+import (
+ "fmt"
+ "net/http"
+ "sync/atomic"
+)
+
+type Metrics struct {
+ requests atomic.Uint64
+ failed atomic.Uint64
+ inFlight atomic.Int64
+ attempts atomic.Uint64
+ droppedUsage atomic.Uint64
+}
+
+func (m *Metrics) RequestStarted() {
+ m.requests.Add(1)
+ m.inFlight.Add(1)
+}
+
+func (m *Metrics) RequestFinished(success bool) {
+ m.inFlight.Add(-1)
+ if !success {
+ m.failed.Add(1)
+ }
+}
+
+func (m *Metrics) UpstreamAttempt() {
+ m.attempts.Add(1)
+}
+
+func (m *Metrics) UsageDropped() {
+ m.droppedUsage.Add(1)
+}
+
+func (m *Metrics) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
+ w.Header().Set("Content-Type", "text/plain; version=0.0.4")
+ fmt.Fprintf(w, "# TYPE aigw_requests_total counter\naigw_requests_total %d\n", m.requests.Load())
+ fmt.Fprintf(w, "# TYPE aigw_requests_failed_total counter\naigw_requests_failed_total %d\n", m.failed.Load())
+ fmt.Fprintf(w, "# TYPE aigw_requests_in_flight gauge\naigw_requests_in_flight %d\n", m.inFlight.Load())
+ fmt.Fprintf(w, "# TYPE aigw_upstream_attempts_total counter\naigw_upstream_attempts_total %d\n", m.attempts.Load())
+ fmt.Fprintf(w, "# TYPE aigw_usage_events_dropped_total counter\naigw_usage_events_dropped_total %d\n", m.droppedUsage.Load())
+}