Skip to content

Commit 05fd6f3

Browse files
authored
Fix preferred_aggregation + preferred_temporality example (#2911)
1 parent 2d591e4 commit 05fd6f3

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
([#2870](https://github.com/open-telemetry/opentelemetry-python/pull/2870))
1616
- Fix: Remove `LogEmitter.flush()` to align with OTel Log spec
1717
([#2863](https://github.com/open-telemetry/opentelemetry-python/pull/2863))
18+
- Fix metric reader examples + added `preferred_temporality` and `preferred_aggregation`
19+
for `ConsoleMetricExporter`
20+
([#2911](https://github.com/open-telemetry/opentelemetry-python/pull/2911))
1821
- Add support for setting OTLP export protocol with env vars, as defined in the
1922
[specifications](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#specify-protocol)
2023
([#2893](https://github.com/open-telemetry/opentelemetry-python/pull/2893))

docs/examples/metrics/reader/preferred_aggregation.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@
2222
)
2323
from opentelemetry.sdk.metrics.view import LastValueAggregation
2424

25-
# Use console exporter for the example
26-
exporter = ConsoleMetricExporter()
27-
2825
aggregation_last_value = {Counter: LastValueAggregation()}
2926

30-
# Create a metric reader with custom preferred aggregation
27+
# Use console exporter for the example
28+
exporter = ConsoleMetricExporter(
29+
preferred_aggregation=aggregation_last_value,
30+
)
31+
32+
# The PeriodicExportingMetricReader takes the preferred aggregation
33+
# from the passed in exporter
3134
reader = PeriodicExportingMetricReader(
3235
exporter,
33-
preferred_aggregation=aggregation_last_value,
3436
export_interval_millis=5_000,
3537
)
3638

docs/examples/metrics/reader/preferred_temporality.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,35 @@
2222
PeriodicExportingMetricReader,
2323
)
2424

25-
# Use console exporter for the example
26-
exporter = ConsoleMetricExporter()
27-
2825
temporality_cumulative = {Counter: AggregationTemporality.CUMULATIVE}
2926
temporality_delta = {Counter: AggregationTemporality.DELTA}
30-
# Create a metric reader with cumulative preferred temporality
31-
# The metrics that are exported using this reader will represent a cumulative value
27+
28+
# Use console exporters for the example
29+
30+
# The metrics that are exported using this exporter will represent a cumulative value
31+
exporter = ConsoleMetricExporter(
32+
preferred_temporality=temporality_cumulative,
33+
)
34+
35+
# The metrics that are exported using this exporter will represent a delta value
36+
exporter2 = ConsoleMetricExporter(
37+
preferred_temporality=temporality_delta,
38+
)
39+
40+
# The PeriodicExportingMetricReader takes the preferred aggregation
41+
# from the passed in exporter
3242
reader = PeriodicExportingMetricReader(
3343
exporter,
34-
preferred_temporality=temporality_cumulative,
3544
export_interval_millis=5_000,
3645
)
37-
# Create a metric reader with delta preferred temporality
38-
# The metrics that are exported using this reader will represent a delta value
46+
47+
# The PeriodicExportingMetricReader takes the preferred aggregation
48+
# from the passed in exporter
3949
reader2 = PeriodicExportingMetricReader(
40-
exporter,
41-
preferred_temporality=temporality_delta,
50+
exporter2,
4251
export_interval_millis=5_000,
4352
)
53+
4454
provider = MeterProvider(metric_readers=[reader, reader2])
4555
set_meter_provider(provider)
4656

@@ -49,10 +59,10 @@
4959
counter = meter.create_counter("my-counter")
5060

5161
# Two metrics are expected to be printed to the console per export interval.
52-
# The metric originating from the metric reader with a preferred temporality
62+
# The metric originating from the metric exporter with a preferred temporality
5363
# of cumulative will keep a running sum of all values added.
54-
# The metric originating from the metric reader with a preferred temporality
64+
# The metric originating from the metric exporter with a preferred temporality
5565
# of delta will have the sum value reset each export interval.
56-
for x in range(10):
57-
counter.add(x)
58-
time.sleep(2.0)
66+
counter.add(5)
67+
time.sleep(10)
68+
counter.add(20)

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,15 @@ def __init__(
136136
["opentelemetry.sdk.metrics.export.MetricsData"], str
137137
] = lambda metrics_data: metrics_data.to_json()
138138
+ linesep,
139+
preferred_temporality: Dict[type, AggregationTemporality] = None,
140+
preferred_aggregation: Dict[
141+
type, "opentelemetry.sdk.metrics.view.Aggregation"
142+
] = None,
139143
):
140-
super().__init__()
144+
super().__init__(
145+
preferred_temporality=preferred_temporality,
146+
preferred_aggregation=preferred_aggregation,
147+
)
141148
self.out = out
142149
self.formatter = formatter
143150

0 commit comments

Comments
 (0)