Skip to content

Commit 12f4490

Browse files
Dudssourceocelotl
andauthored
Fix OTLPMetricExporter ignores preferred_aggregation property (#3603)
* fix #3522 OTLPMetricExporter ignores preferred_aggregation * make OTLPMetricExporter pass the preferred_aggregation argument to the _common_configuration function * Remove unnecessary import from metrics encoder * docs: added changelog entry for pr #3603 * Added unit test to make sure preferred_aggregation override works * chore: break aggregation and temporality config in two functions * chore: removed instrument_class_aggregation variable external declaration, to avoid possible problems with linters * chore: removed unnecessary variable declaration * docs: moved changelog entry to unreleased * chore: added preferred_aggregation argument to _common_configuration call * chore: added unit test for grpc otlp metric exporter * test: moved preferred_aggregation test to class * chore: fix linter findings on metrics_encoder/__init__.py * chore: fix linter findings on grpc/metrics_exporter/__init__.py * chore: fix linter findings on http/metrics_exporter/__init__.py * fix: removed code duplicate * chore: fixed linter errors * Ignoring pylint for protected access * Fix Aggregation import * Rename getter methods to private --------- Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
1 parent 6e6590c commit 12f4490

6 files changed

Lines changed: 69 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Fix `OTLPMetricExporter` ignores `preferred_aggregation` property
11+
([#3603](https://github.com/open-telemetry/opentelemetry-python/pull/3603))
1012
- Logs: set `observed_timestamp` field
1113
([#3565](https://github.com/open-telemetry/opentelemetry-python/pull/3565))
1214
- Add missing Resource SchemaURL in OTLP exporters

exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/metrics_encoder/__init__.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from opentelemetry.sdk.metrics.export import (
1717
MetricExporter,
1818
)
19+
from opentelemetry.sdk.metrics.view import Aggregation
1920
from os import environ
2021
from opentelemetry.sdk.metrics import (
2122
Counter,
@@ -65,9 +66,18 @@ class OTLPMetricExporterMixin:
6566
def _common_configuration(
6667
self,
6768
preferred_temporality: Dict[type, AggregationTemporality] = None,
69+
preferred_aggregation: Dict[type, Aggregation] = None,
6870
) -> None:
6971

70-
instrument_class_temporality = {}
72+
MetricExporter.__init__(
73+
self,
74+
preferred_temporality=self._get_temporality(preferred_temporality),
75+
preferred_aggregation=self._get_aggregation(preferred_aggregation),
76+
)
77+
78+
def _get_temporality(
79+
self, preferred_temporality: Dict[type, AggregationTemporality]
80+
) -> Dict[type, AggregationTemporality]:
7181

7282
otel_exporter_otlp_metrics_temporality_preference = (
7383
environ.get(
@@ -119,6 +129,13 @@ def _common_configuration(
119129

120130
instrument_class_temporality.update(preferred_temporality or {})
121131

132+
return instrument_class_temporality
133+
134+
def _get_aggregation(
135+
self,
136+
preferred_aggregation: Dict[type, Aggregation],
137+
) -> Dict[type, Aggregation]:
138+
122139
otel_exporter_otlp_metrics_default_histogram_aggregation = environ.get(
123140
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION,
124141
"explicit_bucket_histogram",
@@ -128,7 +145,9 @@ def _common_configuration(
128145
"base2_exponential_bucket_histogram"
129146
):
130147

131-
histogram_aggregation_type = ExponentialBucketHistogramAggregation
148+
instrument_class_aggregation = {
149+
Histogram: ExponentialBucketHistogramAggregation(),
150+
}
132151

133152
else:
134153

@@ -145,13 +164,13 @@ def _common_configuration(
145164
otel_exporter_otlp_metrics_default_histogram_aggregation,
146165
)
147166

148-
histogram_aggregation_type = ExplicitBucketHistogramAggregation
167+
instrument_class_aggregation = {
168+
Histogram: ExplicitBucketHistogramAggregation(),
169+
}
149170

150-
MetricExporter.__init__(
151-
self,
152-
preferred_temporality=instrument_class_temporality,
153-
preferred_aggregation={Histogram: histogram_aggregation_type()},
154-
)
171+
instrument_class_aggregation.update(preferred_aggregation or {})
172+
173+
return instrument_class_aggregation
155174

156175

157176
def encode_metrics(data: MetricsData) -> ExportMetricsServiceRequest:

exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/metric_exporter/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ def __init__(
127127
else compression
128128
)
129129

130-
self._common_configuration(preferred_temporality)
130+
self._common_configuration(
131+
preferred_temporality, preferred_aggregation
132+
)
131133

132134
OTLPExporterMixin.__init__(
133135
self,

exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_metrics_exporter.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,24 @@ def test_exponential_explicit_bucket_histogram(self):
968968
ExplicitBucketHistogramAggregation,
969969
)
970970

971+
def test_preferred_aggregation_override(self):
972+
973+
histogram_aggregation = ExplicitBucketHistogramAggregation(
974+
boundaries=[0.05, 0.1, 0.5, 1, 5, 10],
975+
)
976+
977+
exporter = OTLPMetricExporter(
978+
preferred_aggregation={
979+
Histogram: histogram_aggregation,
980+
},
981+
)
982+
983+
self.assertEqual(
984+
# pylint: disable=protected-access
985+
exporter._preferred_aggregation[Histogram],
986+
histogram_aggregation,
987+
)
988+
971989

972990
def _resource_metrics(
973991
index: int, scope_metrics: List[ScopeMetrics]

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/metric_exporter/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ def __init__(
135135
{"Content-Encoding": self._compression.value}
136136
)
137137

138-
self._common_configuration(preferred_temporality)
138+
self._common_configuration(
139+
preferred_temporality, preferred_aggregation
140+
)
139141

140142
def _export(self, serialized_data: str):
141143
data = serialized_data

exporter/opentelemetry-exporter-otlp-proto-http/tests/metrics/test_otlp_metrics_exporter.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,19 @@ def test_2xx_status_code(self, mock_otlp_metric_exporter):
479479
OTLPMetricExporter().export(MagicMock()),
480480
MetricExportResult.SUCCESS,
481481
)
482+
483+
def test_preferred_aggregation_override(self):
484+
485+
histogram_aggregation = ExplicitBucketHistogramAggregation(
486+
boundaries=[0.05, 0.1, 0.5, 1, 5, 10],
487+
)
488+
489+
exporter = OTLPMetricExporter(
490+
preferred_aggregation={
491+
Histogram: histogram_aggregation,
492+
},
493+
)
494+
495+
self.assertEqual(
496+
exporter._preferred_aggregation[Histogram], histogram_aggregation
497+
)

0 commit comments

Comments
 (0)