Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- `opentelemetry-exporter-prometheus`: Add `default_aggregation` parameter to `PrometheusMetricReader` to allow configuring default aggregation per instrument kind
([#5109](https://github.com/open-telemetry/opentelemetry-python/issues/5109))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
([#5109](https://github.com/open-telemetry/opentelemetry-python/issues/5109))
([#5117](https://github.com/open-telemetry/opentelemetry-python/issues/5117))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think #5109 is the correct issue number here, #5117 is the PR number. Please correct me if I'm wrong.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We prefer the changelog to use the PR number, not the associated issue number. You're welcome to add the issue number to the PR description if it's not there already 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying. Updated the changelog to use the PR number and also renamed default_aggregation to preferred_aggregation in the changelog entry.

- `opentelemetry-sdk`: fix YAML structure injection via environment variable substitution in declarative file configuration; values containing newlines are now emitted as quoted YAML scalars per spec requirement
([#5091](https://github.com/open-telemetry/opentelemetry-python/pull/5091))
- `opentelemetry-sdk`: Add `create_logger_provider`/`configure_logger_provider` to declarative file configuration, enabling LoggerProvider instantiation from config files without reading env vars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
from json import dumps
from logging import getLogger
from os import environ
from typing import Deque, Dict, Iterable, Sequence, Tuple, Union
from typing import Deque, Dict, Iterable, Optional, Sequence, Tuple, Union

from prometheus_client import start_http_server
from prometheus_client.core import (
Expand Down Expand Up @@ -105,6 +105,9 @@
MetricsData,
Sum,
)
from opentelemetry.sdk.metrics.view import (
Aggregation,
)
from opentelemetry.semconv._incubating.attributes.otel_attributes import (
OtelComponentTypeValues,
)
Expand Down Expand Up @@ -135,7 +138,10 @@ class PrometheusMetricReader(MetricReader):
"""Prometheus metric exporter for OpenTelemetry."""

def __init__(
self, disable_target_info: bool = False, prefix: str = ""
self,
disable_target_info: bool = False,
prefix: str = "",
default_aggregation: Optional[Dict[type, Aggregation]] = None,
Comment thread
MikeGoldsmith marked this conversation as resolved.
Outdated
) -> None:
super().__init__(
preferred_temporality={
Expand All @@ -146,6 +152,7 @@ def __init__(
ObservableUpDownCounter: AggregationTemporality.CUMULATIVE,
ObservableGauge: AggregationTemporality.CUMULATIVE,
},
preferred_aggregation=default_aggregation,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why call this default_aggregation here? Would be nice to keep consistent as preferred_aggregation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to preferred_aggregation for consistency.

otel_component_type=OtelComponentTypeValues.PROMETHEUS_HTTP_TEXT_METRIC_EXPORTER,
)
self._collector = _CustomCollector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
_CustomCollector,
)
from opentelemetry.metrics import NoOpMeterProvider
from opentelemetry.sdk.metrics import Histogram as HistogramInstrument
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import (
AggregationTemporality,
Expand All @@ -38,6 +39,7 @@
ResourceMetrics,
ScopeMetrics,
)
from opentelemetry.sdk.metrics.view import ExplicitBucketHistogramAggregation
from opentelemetry.sdk.resources import Resource
from opentelemetry.test.metrictestutil import (
_generate_gauge,
Expand Down Expand Up @@ -719,3 +721,21 @@ def test_multiple_data_points_with_different_label_sets(self):
"""
),
)

def test_default_aggregation(self):
"""Test that default_aggregation parameter is passed to MetricReader."""
custom_aggregation = {
HistogramInstrument: ExplicitBucketHistogramAggregation(
boundaries=[1.0, 5.0, 10.0]
)
}
reader = PrometheusMetricReader(
default_aggregation=custom_aggregation
)
self.assertEqual(
reader._instrument_class_aggregation[
HistogramInstrument
].__class__,
ExplicitBucketHistogramAggregation,
)
reader.shutdown()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is wrong, this won't get run.

Suggested change
def test_default_aggregation(self):
"""Test that default_aggregation parameter is passed to MetricReader."""
custom_aggregation = {
HistogramInstrument: ExplicitBucketHistogramAggregation(
boundaries=[1.0, 5.0, 10.0]
)
}
reader = PrometheusMetricReader(
default_aggregation=custom_aggregation
)
self.assertEqual(
reader._instrument_class_aggregation[
HistogramInstrument
].__class__,
ExplicitBucketHistogramAggregation,
)
reader.shutdown()
def test_default_aggregation(self):
"""Test that default_aggregation parameter is passed to MetricReader."""
custom_aggregation = {
HistogramInstrument: ExplicitBucketHistogramAggregation(
boundaries=[1.0, 5.0, 10.0]
)
reader = PrometheusMetricReader(
default_aggregation=custom_aggregation
)
self.assertEqual(
reader._instrument_class_aggregation[
HistogramInstrument
].__class__,
ExplicitBucketHistogramAggregation,
)
reader.shutdown()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the indentation and updated the test to use public API instead of internal attributes

Loading