Skip to content

Commit 87cd4a0

Browse files
fix(telemetry): histogram buckets from OTel standard to MCP standards (googleapis#2729)
## Description > Currently the Histograms for Telemetry Metrics are defaulted to OTel Standard ExplicitBucketBoundaries. These metrics SHOULD be specified with [ExplicitBucketBoundaries](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.50.0/specification/metrics/api.md#instrument-advisory-parameters) of [ 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 30, 60, 120, 300 ]. ## PR Checklist - [x] Make sure you reviewed [CONTRIBUTING.md](https://github.com/googleapis/genai-toolbox/blob/main/CONTRIBUTING.md) - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/genai-toolbox/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) - [ ] Make sure to add `!` if this involve a breaking change 🛠️ Fixes googleapis#2222 --------- Co-authored-by: Yuan Teoh <45984206+Yuan325@users.noreply.github.com>
1 parent dc984ba commit 87cd4a0

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

internal/telemetry/telemetry.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,30 @@ func newMeterProvider(ctx context.Context, r *resource.Resource, telemetryOTLP s
154154
metricOpts = append(metricOpts, metric.WithReader(metric.NewPeriodicReader(gcpExporter)))
155155
}
156156

157+
// Configure custom histogram bucket boundaries for duration metrics as per MCP semantic conventions.
158+
// Source: https://opentelemetry.io/docs/specs/semconv/gen-ai/mcp/#metric-mcpserversessionduration
159+
durationBuckets := []float64{0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 30, 60, 120, 300}
160+
161+
// Create views for duration histograms
162+
durationHistogramNames := []string{
163+
mcpOperationDurationName,
164+
mcpSessionDurationName,
165+
toolExecutionDurationName,
166+
}
167+
168+
views := make([]metric.View, 0, len(durationHistogramNames))
169+
for _, name := range durationHistogramNames {
170+
views = append(views, metric.NewView(
171+
metric.Instrument{Name: name},
172+
metric.Stream{
173+
Aggregation: metric.AggregationExplicitBucketHistogram{
174+
Boundaries: durationBuckets,
175+
},
176+
},
177+
))
178+
}
179+
metricOpts = append(metricOpts, metric.WithView(views...))
180+
157181
meterProvider := metric.NewMeterProvider(metricOpts...)
158182
return meterProvider, nil
159183
}

0 commit comments

Comments
 (0)