package httpapi import ( "context" "net/http" "strconv" "strings" "time" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" ) var ( requestDurationBuckets = []float64{0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 20, 30} uploadDurationBuckets = []float64{0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 20, 30, 60} ) type metrics struct { requests metric.Int64Counter duration metric.Float64Histogram inFlight metric.Int64UpDownCounter replays metric.Int64Counter attachmentUploads metric.Int64Counter attachmentDuration metric.Float64Histogram attachmentBytes metric.Int64Counter handler http.Handler } func newMetrics(meter metric.Meter, handler http.Handler) *metrics { if meter == nil { meter = otel.Meter("messenger-api") } if handler == nil { handler = http.NotFoundHandler() } requests, _ := meter.Int64Counter("messenger_http_requests", metric.WithDescription("Completed HTTP requests.")) duration, _ := meter.Float64Histogram("messenger_http_request_duration", metric.WithDescription("HTTP request duration."), metric.WithUnit("s"), metric.WithExplicitBucketBoundaries(requestDurationBuckets...), ) inFlight, _ := meter.Int64UpDownCounter("messenger_http_requests_in_flight", metric.WithDescription("HTTP requests currently being served.")) replays, _ := meter.Int64Counter("messenger_http_idempotency_replays", metric.WithDescription("Message requests served from an idempotency record.")) attachmentUploads, _ := meter.Int64Counter("messenger_attachment_uploads", metric.WithDescription("Completed attachment uploads by result.")) attachmentDuration, _ := meter.Float64Histogram("messenger_attachment_upload_duration", metric.WithDescription("Attachment upload duration."), metric.WithUnit("s"), metric.WithExplicitBucketBoundaries(uploadDurationBuckets...), ) attachmentBytes, _ := meter.Int64Counter("messenger_attachment_upload_bytes", metric.WithDescription("Bytes accepted by successful attachment uploads."), metric.WithUnit("By")) return &metrics{ requests: requests, duration: duration, inFlight: inFlight, replays: replays, attachmentUploads: attachmentUploads, attachmentDuration: attachmentDuration, attachmentBytes: attachmentBytes, handler: handler, } } func (m *metrics) observe(ctx context.Context, r *http.Request, status int, started time.Time) { attributes := metric.WithAttributes( attribute.String("method", r.Method), attribute.String("route", routeTemplate(r.URL.Path)), attribute.String("status_class", strconv.Itoa(status/100)+"xx"), ) m.requests.Add(ctx, 1, attributes) m.duration.Record(ctx, time.Since(started).Seconds(), attributes) } func routeTemplate(path string) string { switch path { case "/health/live", "/health/ready", "/metrics", "/api/v1/users", "/api/v1/users/me", "/api/v1/auth/sessions", "/api/v1/auth/sessions/current", "/api/v1/chats": return path } parts := strings.Split(strings.Trim(path, "/"), "/") if len(parts) == 4 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "chats" { return "/api/v1/chats/{chat_id}" } if len(parts) == 5 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "chats" && parts[4] == "messages" { return "/api/v1/chats/{chat_id}/messages" } if len(parts) == 5 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "chats" && parts[4] == "attachments" { return "/api/v1/chats/{chat_id}/attachments" } if len(parts) == 4 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "attachments" { return "/api/v1/attachments/{attachment_id}" } if len(parts) == 5 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "attachments" && parts[4] == "content" { return "/api/v1/attachments/{attachment_id}/content" } return "unknown" }