Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OTLPMetricExporter ignores preferred_aggregation property #3603

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
704b4d8
fix #3522 OTLPMetricExporter ignores preferred_aggregation
Dudssource Dec 27, 2023
1718871
make OTLPMetricExporter pass the preferred_aggregation argument to th…
Dudssource Dec 27, 2023
cdded4a
Remove unnecessary import from metrics encoder
Dudssource Dec 27, 2023
be1cfc1
docs: added changelog entry for pr #3603
Dudssource Jan 8, 2024
14af46f
Added unit test to make sure preferred_aggregation override works
Dudssource Jan 8, 2024
718ea2d
chore: break aggregation and temporality config in two functions
Dudssource Jan 12, 2024
c59c7c9
chore: removed instrument_class_aggregation variable external declara…
Dudssource Jan 19, 2024
057273e
chore: removed unnecessary variable declaration
Dudssource Jan 22, 2024
77fde48
docs: moved changelog entry to unreleased
Dudssource Jan 22, 2024
491482d
chore: added preferred_aggregation argument to _common_configuration …
Dudssource Jan 22, 2024
489b4c5
chore: added unit test for grpc otlp metric exporter
Dudssource Jan 22, 2024
6b5cf7a
test: moved preferred_aggregation test to class
Dudssource Jan 22, 2024
4f805cc
chore: fix linter findings on metrics_encoder/__init__.py
Dudssource Jan 23, 2024
a6343f6
chore: fix linter findings on grpc/metrics_exporter/__init__.py
Dudssource Jan 23, 2024
f8b03e3
chore: fix linter findings on http/metrics_exporter/__init__.py
Dudssource Jan 23, 2024
b5c95cc
fix: removed code duplicate
Dudssource Jan 24, 2024
77869df
chore: fixed linter errors
Dudssource Jan 27, 2024
cb825d9
Ignoring pylint for protected access
Dudssource Jan 29, 2024
479e28e
Fix Aggregation import
ocelotl Feb 13, 2024
ce20dd0
Rename getter methods to private
ocelotl Feb 13, 2024
File filter

Filter by extension

Filter by extension

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

## Unreleased

- Fix `OTLPMetricExporter` ignores `preferred_aggregation` property
([#3603](https://github.com/open-telemetry/opentelemetry-python/pull/3603))
- Logs: set `observed_timestamp` field
([#3565](https://github.com/open-telemetry/opentelemetry-python/pull/3565))
- Add missing Resource SchemaURL in OTLP exporters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from opentelemetry.sdk.metrics.export import (
MetricExporter,
)
from opentelemetry.sdk.metrics.view import Aggregation
from os import environ
from opentelemetry.sdk.metrics import (
Counter,
Expand Down Expand Up @@ -65,9 +66,18 @@ class OTLPMetricExporterMixin:
def _common_configuration(
self,
preferred_temporality: Dict[type, AggregationTemporality] = None,
preferred_aggregation: Dict[type, Aggregation] = None,
) -> None:

instrument_class_temporality = {}
MetricExporter.__init__(
self,
preferred_temporality=self._get_temporality(preferred_temporality),
preferred_aggregation=self._get_aggregation(preferred_aggregation),
)

def _get_temporality(
self, preferred_temporality: Dict[type, AggregationTemporality]
) -> Dict[type, AggregationTemporality]:

otel_exporter_otlp_metrics_temporality_preference = (
environ.get(
Expand Down Expand Up @@ -119,6 +129,13 @@ def _common_configuration(

instrument_class_temporality.update(preferred_temporality or {})

return instrument_class_temporality

def _get_aggregation(
self,
preferred_aggregation: Dict[type, Aggregation],
) -> Dict[type, Aggregation]:

otel_exporter_otlp_metrics_default_histogram_aggregation = environ.get(
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION,
"explicit_bucket_histogram",
Expand All @@ -128,7 +145,9 @@ def _common_configuration(
"base2_exponential_bucket_histogram"
):

histogram_aggregation_type = ExponentialBucketHistogramAggregation
instrument_class_aggregation = {
Histogram: ExponentialBucketHistogramAggregation(),
}

else:

Expand All @@ -145,13 +164,13 @@ def _common_configuration(
otel_exporter_otlp_metrics_default_histogram_aggregation,
)

histogram_aggregation_type = ExplicitBucketHistogramAggregation
instrument_class_aggregation = {
Histogram: ExplicitBucketHistogramAggregation(),
}

MetricExporter.__init__(
self,
preferred_temporality=instrument_class_temporality,
preferred_aggregation={Histogram: histogram_aggregation_type()},
)
instrument_class_aggregation.update(preferred_aggregation or {})

return instrument_class_aggregation


def encode_metrics(data: MetricsData) -> ExportMetricsServiceRequest:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ def __init__(
else compression
)

self._common_configuration(preferred_temporality)
self._common_configuration(
preferred_temporality, preferred_aggregation
)

OTLPExporterMixin.__init__(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,24 @@ def test_exponential_explicit_bucket_histogram(self):
ExplicitBucketHistogramAggregation,
)

def test_preferred_aggregation_override(self):

histogram_aggregation = ExplicitBucketHistogramAggregation(
boundaries=[0.05, 0.1, 0.5, 1, 5, 10],
)

exporter = OTLPMetricExporter(
preferred_aggregation={
Histogram: histogram_aggregation,
},
)

self.assertEqual(
# pylint: disable=protected-access
exporter._preferred_aggregation[Histogram],
histogram_aggregation,
)


def _resource_metrics(
index: int, scope_metrics: List[ScopeMetrics]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ def __init__(
{"Content-Encoding": self._compression.value}
)

self._common_configuration(preferred_temporality)
self._common_configuration(
preferred_temporality, preferred_aggregation
)

def _export(self, serialized_data: str):
data = serialized_data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,19 @@ def test_2xx_status_code(self, mock_otlp_metric_exporter):
OTLPMetricExporter().export(MagicMock()),
MetricExportResult.SUCCESS,
)

def test_preferred_aggregation_override(self):

histogram_aggregation = ExplicitBucketHistogramAggregation(
boundaries=[0.05, 0.1, 0.5, 1, 5, 10],
)

exporter = OTLPMetricExporter(
preferred_aggregation={
Histogram: histogram_aggregation,
},
)

self.assertEqual(
exporter._preferred_aggregation[Histogram], histogram_aggregation
)