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 preferred_aggregation + preferred_temporality example #2911

Merged
merged 8 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2870](https://github.com/open-telemetry/opentelemetry-python/pull/2870))
- Fix: Remove `LogEmitter.flush()` to align with OTel Log spec
([#2863](https://github.com/open-telemetry/opentelemetry-python/pull/2863))
- Fix metric reader examples + added `preferred_temporality` and `preferred_aggregation`
lzchen marked this conversation as resolved.
Show resolved Hide resolved
for `ConsoleMetricExporter`
([#2911](https://github.com/open-telemetry/opentelemetry-python/pull/2911))
- Add support for setting OTLP export protocol with env vars, as defined in the
[specifications](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#specify-protocol)
([#2893](https://github.com/open-telemetry/opentelemetry-python/pull/2893))
Expand Down
12 changes: 7 additions & 5 deletions docs/examples/metrics/reader/preferred_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@
)
from opentelemetry.sdk.metrics.view import LastValueAggregation

# Use console exporter for the example
exporter = ConsoleMetricExporter()

aggregation_last_value = {Counter: LastValueAggregation()}

# Create a metric reader with custom preferred aggregation
# Use console exporter for the example
exporter = ConsoleMetricExporter(
preferred_aggregation=aggregation_last_value,
)

# The PeriodicExportingMetricReader takes the preferred aggregation
# from the passed in exporter
reader = PeriodicExportingMetricReader(
exporter,
preferred_aggregation=aggregation_last_value,
export_interval_millis=5_000,
)

Expand Down
40 changes: 25 additions & 15 deletions docs/examples/metrics/reader/preferred_temporality.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,35 @@
PeriodicExportingMetricReader,
)

# Use console exporter for the example
exporter = ConsoleMetricExporter()

temporality_cumulative = {Counter: AggregationTemporality.CUMULATIVE}
temporality_delta = {Counter: AggregationTemporality.DELTA}
# Create a metric reader with cumulative preferred temporality
# The metrics that are exported using this reader will represent a cumulative value

# Use console exporters for the example

# The metrics that are exported using this exporter will represent a cumulative value
exporter = ConsoleMetricExporter(
preferred_temporality=temporality_cumulative,
)

# The metrics that are exported using this exporter will represent a delta value
exporter2 = ConsoleMetricExporter(
preferred_temporality=temporality_delta,
)

# The PeriodicExportingMetricReader takes the preferred aggregation
# from the passed in exporter
reader = PeriodicExportingMetricReader(
exporter,
preferred_temporality=temporality_cumulative,
export_interval_millis=5_000,
)
# Create a metric reader with delta preferred temporality
# The metrics that are exported using this reader will represent a delta value

# The PeriodicExportingMetricReader takes the preferred aggregation
# from the passed in exporter
reader2 = PeriodicExportingMetricReader(
exporter,
preferred_temporality=temporality_delta,
exporter2,
export_interval_millis=5_000,
)

provider = MeterProvider(metric_readers=[reader, reader2])
set_meter_provider(provider)

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

# Two metrics are expected to be printed to the console per export interval.
# The metric originating from the metric reader with a preferred temporality
# The metric originating from the metric exporter with a preferred temporality
# of cumulative will keep a running sum of all values added.
# The metric originating from the metric reader with a preferred temporality
# The metric originating from the metric exporter with a preferred temporality
# of delta will have the sum value reset each export interval.
for x in range(10):
counter.add(x)
time.sleep(2.0)
counter.add(5)
time.sleep(10)
counter.add(20)
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,15 @@ def __init__(
["opentelemetry.sdk.metrics.export.MetricsData"], str
] = lambda metrics_data: metrics_data.to_json()
+ linesep,
preferred_temporality: Dict[type, AggregationTemporality] = None,
preferred_aggregation: Dict[
type, "opentelemetry.sdk.metrics.view.Aggregation"
] = None,
):
super().__init__()
super().__init__(
preferred_temporality=preferred_temporality,
preferred_aggregation=preferred_aggregation,
)
self.out = out
self.formatter = formatter

Expand Down