-
Notifications
You must be signed in to change notification settings - Fork 4.6k
feat: add built-in Prometheus metrics for HTTP and LLM observability #3439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RichardoMrMu
wants to merge
3
commits into
QuantumNous:main
Choose a base branch
from
RichardoMrMu:feat/prometheus-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+349
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/QuantumNous/new-api/dto" | ||
| relaycommon "github.com/QuantumNous/new-api/relay/common" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| ) | ||
|
|
||
| // region is read once from MAAS_REGION env var at startup. | ||
| var region string | ||
|
|
||
| func init() { | ||
| region = os.Getenv("MAAS_REGION") | ||
| if region == "" { | ||
| region = "unknown" | ||
| } | ||
|
|
||
| prometheus.MustRegister( | ||
| llmInputTokenTotal, | ||
| llmOutputTokenTotal, | ||
| llmRequestTotal, | ||
| llmServiceDuration, | ||
| llmFirstTokenDuration, | ||
| llmTimePerOutputToken, | ||
| rateLimitTotal, | ||
| circuitBreakerState, | ||
| llmGatewayDuration, | ||
| ) | ||
| } | ||
|
|
||
| // GetRegion returns the configured MAAS_REGION value. | ||
| func GetRegion() string { | ||
| return region | ||
| } | ||
|
|
||
| // ---- LLM Metrics (6) ---- | ||
|
|
||
| var llmRequestLabelNames = []string{ | ||
| "model", "channel", "upstream_model", "status", "error_type", | ||
| "region", "is_stream", "token_name", | ||
| } | ||
|
|
||
| var llmTokenLabelNames = []string{ | ||
| "model", "channel", "upstream_model", "region", "token_name", | ||
| } | ||
|
|
||
| var llmLatencyLabelNames = []string{ | ||
| "model", "channel", "region", | ||
| } | ||
|
|
||
| var ( | ||
| llmRequestTotal = prometheus.NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Namespace: "newapi", | ||
| Name: "llm_request_total", | ||
| Help: "Total number of LLM requests", | ||
| }, | ||
| llmRequestLabelNames, | ||
| ) | ||
|
|
||
| llmInputTokenTotal = prometheus.NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Namespace: "newapi", | ||
| Name: "llm_input_token_total", | ||
| Help: "Total number of LLM input (prompt) tokens", | ||
| }, | ||
| llmTokenLabelNames, | ||
| ) | ||
|
|
||
| llmOutputTokenTotal = prometheus.NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Namespace: "newapi", | ||
| Name: "llm_output_token_total", | ||
| Help: "Total number of LLM output (completion) tokens", | ||
| }, | ||
| llmTokenLabelNames, | ||
| ) | ||
|
|
||
| llmFirstTokenDuration = prometheus.NewHistogramVec( | ||
| prometheus.HistogramOpts{ | ||
| Namespace: "newapi", | ||
| Name: "llm_first_token_duration_seconds", | ||
| Help: "LLM time-to-first-token (TTFT) in seconds", | ||
| Buckets: []float64{0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30}, | ||
| }, | ||
| llmLatencyLabelNames, | ||
| ) | ||
|
|
||
| llmTimePerOutputToken = prometheus.NewHistogramVec( | ||
| prometheus.HistogramOpts{ | ||
| Namespace: "newapi", | ||
| Name: "llm_time_per_output_token_seconds", | ||
| Help: "LLM time per output token (TPOT) in seconds", | ||
| Buckets: []float64{0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1}, | ||
| }, | ||
| llmLatencyLabelNames, | ||
| ) | ||
|
|
||
| llmServiceDuration = prometheus.NewHistogramVec( | ||
| prometheus.HistogramOpts{ | ||
| Namespace: "newapi", | ||
| Name: "llm_service_duration_seconds", | ||
| Help: "LLM upstream service duration in seconds (from request start to response complete)", | ||
| Buckets: []float64{0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60, 120, 300}, | ||
| }, | ||
| llmLatencyLabelNames, | ||
| ) | ||
| ) | ||
|
|
||
| // ---- Rate Limit / Circuit Breaker / Gateway Metrics (3) ---- | ||
|
|
||
| var ( | ||
| rateLimitTotal = prometheus.NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Namespace: "newapi", | ||
| Name: "rate_limit_total", | ||
| Help: "Total number of rate limit triggers", | ||
| }, | ||
| []string{"model", "channel", "type", "token_name"}, | ||
| ) | ||
|
|
||
| circuitBreakerState = prometheus.NewGaugeVec( | ||
| prometheus.GaugeOpts{ | ||
| Namespace: "newapi", | ||
| Name: "circuit_breaker_state", | ||
| Help: "Circuit breaker state (0=Closed, 1=HalfOpen, 2=Open)", | ||
| }, | ||
| []string{"channel", "model"}, | ||
| ) | ||
|
|
||
| llmGatewayDuration = prometheus.NewHistogramVec( | ||
| prometheus.HistogramOpts{ | ||
| Namespace: "newapi", | ||
| Name: "llm_gateway_duration_seconds", | ||
| Help: "Gateway processing duration in seconds (excluding upstream)", | ||
| Buckets: []float64{0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5}, | ||
| }, | ||
| []string{"model", "channel"}, | ||
| ) | ||
| ) | ||
|
|
||
| // RecordAIMetrics should be called after a relay request completes. | ||
| // It records token counts, request count, service duration, TTFT, and TPOT. | ||
| func RecordAIMetrics(relayInfo *relaycommon.RelayInfo, usage *dto.Usage) { | ||
| RecordAIMetricsWithStatus(relayInfo, usage, "success", "") | ||
| } | ||
|
|
||
| // RecordAIMetricsWithStatus records LLM metrics with explicit status and error type. | ||
| func RecordAIMetricsWithStatus(relayInfo *relaycommon.RelayInfo, usage *dto.Usage, status string, errorType string) { | ||
| if relayInfo == nil || relayInfo.ChannelMeta == nil { | ||
| return | ||
| } | ||
|
|
||
| model := relayInfo.OriginModelName | ||
| channel := fmt.Sprintf("%d", relayInfo.ChannelMeta.ChannelId) | ||
| upstreamModel := relayInfo.ChannelMeta.UpstreamModelName | ||
| tokenName := "" | ||
| if relayInfo.TokenKey != "" { | ||
| tokenName = relayInfo.TokenKey | ||
| } | ||
| isStream := strconv.FormatBool(relayInfo.IsStream) | ||
|
|
||
| // Request count (with status labels) | ||
| llmRequestTotal.WithLabelValues( | ||
| model, channel, upstreamModel, status, errorType, | ||
| region, isStream, tokenName, | ||
| ).Inc() | ||
|
|
||
| // Token counts | ||
| if usage != nil { | ||
| tokenLabels := []string{model, channel, upstreamModel, region, tokenName} | ||
| llmInputTokenTotal.WithLabelValues(tokenLabels...).Add(float64(usage.PromptTokens)) | ||
| llmOutputTokenTotal.WithLabelValues(tokenLabels...).Add(float64(usage.CompletionTokens)) | ||
| } | ||
|
|
||
| latencyLabels := []string{model, channel, region} | ||
|
|
||
| // Service duration (total time from request start to now) | ||
| serviceDuration := time.Since(relayInfo.StartTime).Seconds() | ||
| llmServiceDuration.WithLabelValues(latencyLabels...).Observe(serviceDuration) | ||
|
|
||
| // Time-to-first-token (only meaningful when FirstResponseTime was recorded) | ||
| if !relayInfo.FirstResponseTime.IsZero() { | ||
| ttft := relayInfo.FirstResponseTime.Sub(relayInfo.StartTime).Seconds() | ||
| if ttft > 0 { | ||
| llmFirstTokenDuration.WithLabelValues(latencyLabels...).Observe(ttft) | ||
| } | ||
|
Comment on lines
+185
to
+193
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard zero Line 185 computes Suggested fix latencyLabels := []string{model, channel, region}
+ if relayInfo.StartTime.IsZero() {
+ return
+ }
+
// Service duration (total time from request start to now)
serviceDuration := time.Since(relayInfo.StartTime).Seconds()
llmServiceDuration.WithLabelValues(latencyLabels...).Observe(serviceDuration)
// Time-to-first-token (only meaningful when FirstResponseTime was recorded)
- if !relayInfo.FirstResponseTime.IsZero() {
+ if !relayInfo.FirstResponseTime.IsZero() && relayInfo.FirstResponseTime.After(relayInfo.StartTime) {
ttft := relayInfo.FirstResponseTime.Sub(relayInfo.StartTime).Seconds()🤖 Prompt for AI Agents |
||
|
|
||
| // Time per output token (TPOT): (total_duration - ttft) / output_tokens | ||
| if usage != nil && usage.CompletionTokens > 0 { | ||
| generationDuration := serviceDuration - ttft | ||
| if generationDuration > 0 { | ||
| tpot := generationDuration / float64(usage.CompletionTokens) | ||
| llmTimePerOutputToken.WithLabelValues(latencyLabels...).Observe(tpot) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // RecordRateLimit records a rate limit trigger event. | ||
| func RecordRateLimit(model string, channel string, limitType string, tokenName string) { | ||
| rateLimitTotal.WithLabelValues(model, channel, limitType, tokenName).Inc() | ||
| } | ||
|
|
||
| // RecordCircuitBreakerState updates the circuit breaker state gauge. | ||
| func RecordCircuitBreakerState(channel string, model string, state float64) { | ||
| circuitBreakerState.WithLabelValues(channel, model).Set(state) | ||
| } | ||
|
|
||
| // RecordGatewayDuration records the gateway processing duration (excluding upstream). | ||
| func RecordGatewayDuration(model string, channel string, durationSeconds float64) { | ||
| llmGatewayDuration.WithLabelValues(model, channel).Observe(durationSeconds) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/QuantumNous/new-api/metrics" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promhttp" | ||
| ) | ||
|
|
||
| var httpLabelNames = []string{"method", "path", "status", "region"} | ||
|
|
||
| var ( | ||
| httpRequestsTotal = prometheus.NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Namespace: "newapi", | ||
| Name: "http_requests_total", | ||
| Help: "Total number of HTTP requests", | ||
| }, | ||
| httpLabelNames, | ||
| ) | ||
|
|
||
| httpRequestDuration = prometheus.NewHistogramVec( | ||
| prometheus.HistogramOpts{ | ||
| Namespace: "newapi", | ||
| Name: "http_request_duration_seconds", | ||
| Help: "HTTP request duration in seconds", | ||
| Buckets: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60}, | ||
| }, | ||
| httpLabelNames, | ||
| ) | ||
|
|
||
| httpRequestsInFlight = prometheus.NewGauge( | ||
| prometheus.GaugeOpts{ | ||
| Namespace: "newapi", | ||
| Name: "http_requests_in_flight", | ||
| Help: "Number of HTTP requests currently being processed", | ||
| }, | ||
| ) | ||
|
|
||
| httpResponseSizeBytes = prometheus.NewHistogramVec( | ||
| prometheus.HistogramOpts{ | ||
| Namespace: "newapi", | ||
| Name: "http_response_size_bytes", | ||
| Help: "HTTP response size in bytes", | ||
| Buckets: prometheus.ExponentialBuckets(100, 10, 8), | ||
| }, | ||
| httpLabelNames, | ||
| ) | ||
| ) | ||
|
|
||
| func init() { | ||
| prometheus.MustRegister( | ||
| httpRequestsTotal, | ||
| httpRequestDuration, | ||
| httpRequestsInFlight, | ||
| httpResponseSizeBytes, | ||
| ) | ||
| } | ||
|
|
||
| // PrometheusMiddleware collects HTTP golden metrics for each request. | ||
| // It records request count, latency, in-flight requests, and response size. | ||
| func PrometheusMiddleware() gin.HandlerFunc { | ||
| return func(c *gin.Context) { | ||
| if c.Request.URL.Path == "/metrics" { | ||
| c.Next() | ||
| return | ||
| } | ||
|
|
||
| startTime := time.Now() | ||
| httpRequestsInFlight.Inc() | ||
|
|
||
| c.Next() | ||
|
|
||
| httpRequestsInFlight.Dec() | ||
|
|
||
| statusCode := strconv.Itoa(c.Writer.Status()) | ||
| routePath := normalizeRoutePath(c) | ||
| method := c.Request.Method | ||
| duration := time.Since(startTime).Seconds() | ||
| responseSize := float64(c.Writer.Size()) | ||
| regionLabel := metrics.GetRegion() | ||
|
|
||
| httpRequestsTotal.WithLabelValues(method, routePath, statusCode, regionLabel).Inc() | ||
| httpRequestDuration.WithLabelValues(method, routePath, statusCode, regionLabel).Observe(duration) | ||
| httpResponseSizeBytes.WithLabelValues(method, routePath, statusCode, regionLabel).Observe(responseSize) | ||
| } | ||
| } | ||
|
|
||
| // MetricsHandler returns the Prometheus metrics HTTP handler for the /metrics endpoint. | ||
| func MetricsHandler() gin.HandlerFunc { | ||
| handler := promhttp.Handler() | ||
| return func(c *gin.Context) { | ||
| handler.ServeHTTP(c.Writer, c.Request) | ||
| } | ||
| } | ||
|
|
||
| // normalizeRoutePath extracts the matched route template to avoid high-cardinality labels. | ||
| // Falls back to a generic label if no route template is available. | ||
| func normalizeRoutePath(c *gin.Context) string { | ||
| routePath := c.FullPath() | ||
| if routePath != "" { | ||
| return routePath | ||
| } | ||
|
|
||
| routeTag, exists := c.Get(RouteTagKey) | ||
| if exists { | ||
| return routeTag.(string) | ||
| } | ||
|
|
||
| return "unmatched" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove raw token keys from metric labels (secret leakage + cardinality blow-up).
Line 164-Line 166 maps
relayInfo.TokenKeyintotoken_name, and that label is used in counters on Line 170-Line 179. This can expose credentials in/metricsand create unbounded label cardinality.Suggested fix
var llmRequestLabelNames = []string{ "model", "channel", "upstream_model", "status", "error_type", - "region", "is_stream", "token_name", + "region", "is_stream", } var llmTokenLabelNames = []string{ - "model", "channel", "upstream_model", "region", "token_name", + "model", "channel", "upstream_model", "region", } @@ - tokenName := "" - if relayInfo.TokenKey != "" { - tokenName = relayInfo.TokenKey - } isStream := strconv.FormatBool(relayInfo.IsStream) @@ llmRequestTotal.WithLabelValues( model, channel, upstreamModel, status, errorType, - region, isStream, tokenName, + region, isStream, ).Inc() @@ - tokenLabels := []string{model, channel, upstreamModel, region, tokenName} + tokenLabels := []string{model, channel, upstreamModel, region} llmInputTokenTotal.WithLabelValues(tokenLabels...).Add(float64(usage.PromptTokens)) llmOutputTokenTotal.WithLabelValues(tokenLabels...).Add(float64(usage.CompletionTokens)) }Also applies to: 163-166, 170-179
🤖 Prompt for AI Agents