open-telemetry / open-telemetry/opentelemetry-python

Creating Exponential Histograms

Open
#4,383 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
2.6k
Forks
1k
Avg merge
4d 15h
Merged PRs (30d)
19

Description

Describe your environment

OS: macOS Sonoma
Python version: Python 3.13.1
Versions:
opentelemetry-api==1.29.0
opentelemetry-distro==0.50b0
opentelemetry-exporter-otlp==1.29.0
opentelemetry-exporter-otlp-proto-common==1.29.0
opentelemetry-exporter-otlp-proto-grpc==1.29.0
opentelemetry-exporter-otlp-proto-http==1.29.0
opentelemetry-instrumentation==0.50b0
opentelemetry-proto==1.29.0
opentelemetry-sdk==1.29.0
opentelemetry-semantic-conventions==0.50b0

What happened?

When I want to have exponential Histograms I can use the environment variable to make them the default like this:

OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION=base2_exponential_bucket_histogram python ./init_metrics.py

But if I want to make exponential histograms the default in code it looks like the correct way is to make a View that matches the histogram instrument to set the aggregation policy. I've tried something like this:

def init_metrics(exporter: Optional[MetricExporter]=OTLPMetricExporter()) -> None:
    metric_reader = PeriodicExportingMetricReader(exporter)
    provider = MeterProvider(
        metric_readers=[metric_reader],

        # Use Exponential Histograms by default
        views=[
            View(
                instrument_type=Histogram,
                instrument_name="*",
                aggregation=ExponentialBucketHistogramAggregation(),
                #aggregation=ExplicitBucketHistogramAggregation(
                #   boundaries=(0.0, 1.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0), record_min_max=True
                #)
            )
        ]
    )

    # Sets the global default meter provider
    metrics.set_meter_provider(provider)

However, the last approach produces an ExplicitBounds histogram using the default bucket boundaries every time. I've tried using this to set an ExplicitBucketHistogramAggregation with custom buckets....but my custom buckets are ignored.

Looking at get_meter() I'm concerned that it doesn't give the instruments access to the defined views and therefor just ignores any global views I try to configure.

https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py#L555

Am I setting this up incorrectly, or is this a bug?

Steps to Reproduce

Uploading init_metrics.py.txt…

Expected Result

I should see my local OTEL Collector in debug mode dump out exponential buckets,

Actual Result

Unless I use the environment variable I always get Explicit Buckets with the default bucketing.

Additional context

I'm attempting to build some examples and boiler plate for custom metrics for my teams.

Would you like to implement a fix?

None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by inspecting get_meter() in opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/init.py around line 555, then reproduce the provided View configuration with the Python snippet. Trace how instruments receive configured views and verify the collector output; done means exponential or custom explicit bucket aggregation is honored without relying on the environment variable.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.