Skip to content

feat: support custom metric views in MetricsConfig - #427

Draft
adubovik wants to merge 2 commits into
developmentfrom
feat/metrics-views
Draft

adubovik wants to merge 2 commits into
developmentfrom
feat/metrics-views

Conversation

@adubovik

@adubovik adubovik commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

MetricsConfig accepts a new optional meter_provider_views field — a list of opentelemetry.sdk.metrics.view.View — passed straight to the MeterProvider. Previously the only way to customize aggregation was to set the global MeterProvider before init_telemetry ran, which made the SDK's own provider silently lose (Overriding of current MeterProvider is not allowed).

Example — keep the default buckets of http.server.duration but extend them past 10s, up to 5 minutes:

from fastapi import FastAPI
from opentelemetry.sdk.metrics.view import (
    ExplicitBucketHistogramAggregation,
    View,
)

from aidial_sdk.telemetry.init import init_telemetry
from aidial_sdk.telemetry.types import MetricsConfig, TelemetryConfig

app = FastAPI()

init_telemetry(
    app,
    TelemetryConfig(
        metrics=MetricsConfig(
            prometheus_export=True,
            meter_provider_views=[
                View(
                    # NOTE: `http.server.duration` is reported in MILLISECONDS
                    instrument_name="http.server.duration",
                    aggregation=ExplicitBucketHistogramAggregation(
                        # the SDK default boundaries stop at 10s...
                        (
                            0, 5, 10, 25, 50, 75, 100, 250, 500, 750,
                            1000, 2500, 5000, 7500, 10000,
                            # ...these are the extra ones
                            15000, 20000, 30000, 60000, 120000, 300000,
                        )
                    ),
                )
            ],
        )
    ),
)

Scraping :9464/metrics then reports http_server_duration_milliseconds_bucket{...,le="300000"} and friends.

meter_provider_views defaults to None, so existing applications are unaffected.

Applications can now pass `MetricsConfig.views` to override the default
aggregation of any instrument (e.g. the histogram bucket boundaries of
`http.server.duration`) without racing `init_telemetry` for the global
MeterProvider.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@adubovik adubovik self-assigned this Aug 3, 2026
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant