open-telemetry / opentelemetry-python

OpenTelemetry Python API and SDK
https://opentelemetry.io
Apache License 2.0
1.67k stars 569 forks source link

Is it possible to set the histogram boundaries? #3926

Closed fnmut closed 1 month ago

fnmut commented 1 month ago

version 1.23.0 of opentelemetry-sdk python 3.12

I see OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION, which I think defaults to ExplicitBucketHistogramAggregation? How can I set the boundaries on this? During setup of the MeterProvider?

Apologies if this is the wrong place to ask this question.

edit: I found this issue and it seems like I should add it as a View in the MeterProvider?

fnmut commented 1 month ago

Figured it out, the solution is to set a MeterProvider with a View that handles the histogram, eg:

meter_provider = MeterProvider(
    metric_readers=[reader],
    views=[
        View(
            instrument_type=metrics.Histogram,
            instrument_name=histogram_name,
            aggregation=ExplicitBucketHistogramAggregation(
                boundaries=[
                    0,
                    10,
                    50,
                    100,
                    200,
                    400,
                    600,
                    800,
                    1000,
                    2000,
                    10000,
                ]
            ),
        )
    ],
)
metrics.set_meter_provider(meter_provider)