Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🛞 fix: Roll Overflow Tenant Labels Into Aggregate Metrics (#15474)
Danny Avilaf72a5b33c3e5
diff --git a/otel/langfuse-fanout/README.md b/otel/langfuse-fanout/README.mdindex 6da4a6fd5d..0f2e2e158a 100644--- a/otel/langfuse-fanout/README.md+++ b/otel/langfuse-fanout/README.md@@ -272,10 +272,10 @@ LibreChat stamps `librechat.tenant.id`, `librechat.langfuse.export_plan`, and`librechat.langfuse.export_reason` on Langfuse run spans. The gateway reads thetenant ID from each OTLP batch for the trace export counter. Batches without atenant ID use `<unknown>`; batches containing more than one tenant use `<multiple>`.-Angle brackets keep these synthetic values outside LibreChat's accepted tenant-ID grammar.-The `tenant_id` label is intentionally high-cardinality and must only receive-traffic from trusted LibreChat deployments. Each distinct tenant creates a-Prometheus time series for every destination and result combination.+Invalid tenant IDs use `<invalid>`. The gateway retains up to 1,000 valid tenant+labels and aggregates additional IDs under `<overflow>`, bounding the trace+counter's cardinality. Angle brackets keep these synthetic values outside+LibreChat's accepted tenant-ID grammar.Successful admin connection updates emit the structured log event`librechat.langfuse.connection.changed`. It includes the tenant, configurationdiff --git a/otel/langfuse-fanout/cmd/langfuse-fanout/main.go b/otel/langfuse-fanout/cmd/langfuse-fanout/main.goindex 6fa069c711..25fb7464dc 100644--- a/otel/langfuse-fanout/cmd/langfuse-fanout/main.go+++ b/otel/langfuse-fanout/cmd/langfuse-fanout/main.go@@ -748,7 +748,11 @@ func (g *gateway) recordTraceExport(route route, result string, tenantID string)if g.metrics == nil {return}- g.metrics.recordTraceExport(routeDestinationLabel(route), result, tenantID)+ destination := centralName+ if _, ok := g.cfg.tenants[route.destination]; ok {+ destination = routeDestinationLabel(route)+ }+ g.metrics.recordTraceExport(destination, result, tenantID)}func (g *gateway) recordMediaDivergence(kind string, destination string) {diff --git a/otel/langfuse-fanout/cmd/langfuse-fanout/main_test.go b/otel/langfuse-fanout/cmd/langfuse-fanout/main_test.goindex 9893d665dc..a540993d71 100644--- a/otel/langfuse-fanout/cmd/langfuse-fanout/main_test.go+++ b/otel/langfuse-fanout/cmd/langfuse-fanout/main_test.go@@ -6,6 +6,7 @@ import ("context""encoding/json""errors"+ "fmt""io""log/slog""net/http"@@ -1068,6 +1069,56 @@ func TestTraceProxyRecordsPrometheusMetrics(t *testing.T) {}}+func TestTraceProxyBoundsTenantMetricLabels(t *testing.T) {+ t.Parallel()++ metrics := newGatewayMetrics()+ for i := 0; i < maxTenantMetricLabels+10; i++ {+ metrics.recordTraceExport(centralName, "success", fmt.Sprintf("tenant-%d", i))+ }+ metrics.recordTraceExport(centralName, "success", strings.Repeat("x", 129))++ metricFamilies, err := metrics.registry.Gather()+ if err != nil {+ t.Fatal(err)+ }+ for _, family := range metricFamilies {+ if family.GetName() != "langfuse_fanout_trace_exports_total" {+ continue+ }+ if got, want := len(family.Metric), maxTenantMetricLabels+2; got != want {+ t.Fatalf("trace metric children = %d, want %d", got, want)+ }+ labels := make(map[string]struct{}, len(family.Metric))+ for _, metric := range family.Metric {+ for _, label := range metric.Label {+ if label.GetName() == "tenant_id" {+ labels[label.GetValue()] = struct{}{}+ }+ }+ }+ for _, label := range []string{overflowTenantID, invalidTenantID} {+ if _, ok := labels[label]; !ok {+ t.Fatalf("missing bounded tenant label %q", label)+ }+ }+ return+ }+ t.Fatal("missing trace export metric family")+}++func TestTraceProxyBoundsUnknownDestinationLabel(t *testing.T) {+ t.Parallel()++ gw := newTestGatewayWithCollector("http://collector.invalid")+ gw.recordTraceExport(route{destination: "attacker-controlled"}, "error", "tenant-123")++ metrics := scrapeMetrics(t, gw)+ if !strings.Contains(metrics, `langfuse_fanout_trace_exports_total{destination="central",result="error",tenant_id="tenant-123"} 1`) {+ t.Fatalf("missing bounded trace destination metric:\n%s", metrics)+ }+}+func TestExtractTraceTenantID(t *testing.T) {t.Parallel()diff --git a/otel/langfuse-fanout/cmd/langfuse-fanout/metrics.go b/otel/langfuse-fanout/cmd/langfuse-fanout/metrics.goindex d3bb05abbf..184c9e20d5 100644--- a/otel/langfuse-fanout/cmd/langfuse-fanout/metrics.go+++ b/otel/langfuse-fanout/cmd/langfuse-fanout/metrics.go@@ -4,12 +4,19 @@ import ("fmt""net/http""strings"+ "sync""time""github.com/prometheus/client_golang/prometheus""github.com/prometheus/client_golang/prometheus/collectors")+const (+ maxTenantMetricLabels = 1000+ invalidTenantID = "<invalid>"+ overflowTenantID = "<overflow>"+)+type gatewayMetrics struct {registry *prometheus.RegistryhttpRequests *prometheus.CounterVec@@ -23,6 +30,8 @@ type gatewayMetrics struct {uploadPlanMisses prometheus.CounteruploadPlanStoreErrors *prometheus.CounterVecuploadBytes prometheus.Histogram+ tenantLabelsMu sync.Mutex+ tenantLabels map[string]struct{}}func newGatewayMetrics() *gatewayMetrics {@@ -33,7 +42,8 @@ func newGatewayMetrics() *gatewayMetrics {)metrics := &gatewayMetrics{- registry: registry,+ registry: registry,+ tenantLabels: make(map[string]struct{}, maxTenantMetricLabels),httpRequests: prometheus.NewCounterVec(prometheus.CounterOpts{Name: "langfuse_fanout_http_requests_total",Help: "Total HTTP requests handled by the Langfuse fanout gateway.",@@ -129,17 +139,50 @@ func (m *gatewayMetrics) recordTraceExport(destination string, result string, teif m == nil {return}- m.traceExports.WithLabelValues(normalizeMetricLabel(destination), result, tenantMetricLabel(tenantID)).Inc()+ m.traceExports.WithLabelValues(normalizeMetricLabel(destination), result, m.tenantMetricLabel(tenantID)).Inc()}-func tenantMetricLabel(tenantID string) string {+func (m *gatewayMetrics) tenantMetricLabel(tenantID string) string {tenantID = strings.TrimSpace(tenantID)if tenantID == "" {- return "unknown"+ return unknownTenantID+ }+ if tenantID == unknownTenantID || tenantID == multipleTenantIDs {+ return tenantID+ }+ if !validTenantMetricID(tenantID) {+ return invalidTenantID}++ m.tenantLabelsMu.Lock()+ defer m.tenantLabelsMu.Unlock()+ if _, ok := m.tenantLabels[tenantID]; ok {+ return tenantID+ }+ if len(m.tenantLabels) >= maxTenantMetricLabels {+ return overflowTenantID+ }+ m.tenantLabels[tenantID] = struct{}{}return tenantID}+func validTenantMetricID(tenantID string) bool {+ if len(tenantID) > 128 {+ return false+ }+ for _, r := range tenantID {+ switch {+ case r >= 'a' && r <= 'z':+ case r >= 'A' && r <= 'Z':+ case r >= '0' && r <= '9':+ case r == '-' || r == '_' || r == '.':+ default:+ return false+ }+ }+ return true+}+func (m *gatewayMetrics) recordMediaDivergence(kind string, destination string) {if m == nil {return