Skip to content

Commit 2ad9515

Browse files
authored
docs: Add an example for preferred temporality and aggregation for metric reader (#2832)
* example * remove test * comments * docs * docs * seconds
1 parent f14d936 commit 2ad9515

13 files changed

+154
-6
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
MetricReader configuration scenarios
2+
====================================
3+
4+
These examples show how to customize the metrics that are output by the SDK using configuration on metric readers. There are multiple examples:
5+
6+
* preferred_aggregation.py: Shows how to configure the preferred aggregation for metric instrument types.
7+
* preferred_temporality.py: Shows how to configure the preferred temporality for metric instrument types.
8+
9+
The source files of these examples are available :scm_web:`here <docs/examples/metrics/reader/>`.
10+
11+
12+
Installation
13+
------------
14+
15+
.. code-block:: sh
16+
17+
pip install -r requirements.txt
18+
19+
Run the Example
20+
---------------
21+
22+
.. code-block:: sh
23+
24+
python <example_name>.py
25+
26+
The output will be shown in the console.
27+
28+
Useful links
29+
------------
30+
31+
- OpenTelemetry_
32+
- :doc:`../../../api/metrics`
33+
34+
.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import time
16+
17+
from opentelemetry.metrics import get_meter_provider, set_meter_provider
18+
from opentelemetry.sdk.metrics import Counter, MeterProvider
19+
from opentelemetry.sdk.metrics.export import (
20+
ConsoleMetricExporter,
21+
PeriodicExportingMetricReader,
22+
)
23+
from opentelemetry.sdk.metrics.view import LastValueAggregation
24+
25+
# Use console exporter for the example
26+
exporter = ConsoleMetricExporter()
27+
28+
aggregation_last_value = {Counter: LastValueAggregation()}
29+
30+
# Create a metric reader with custom preferred aggregation
31+
reader = PeriodicExportingMetricReader(
32+
exporter,
33+
preferred_aggregation=aggregation_last_value,
34+
export_interval_millis=5_000,
35+
)
36+
37+
provider = MeterProvider(metric_readers=[reader])
38+
set_meter_provider(provider)
39+
40+
meter = get_meter_provider().get_meter("preferred-aggregation", "0.1.2")
41+
42+
counter = meter.create_counter("my-counter")
43+
44+
# A counter normally would have an aggregation type of SumAggregation,
45+
# in which it's value would be determined by a cumulative sum.
46+
# In this example, the counter is configured with the LastValueAggregation,
47+
# which will only hold the most recent value.
48+
for x in range(10):
49+
counter.add(x)
50+
time.sleep(2.0)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import time
16+
17+
from opentelemetry.metrics import get_meter_provider, set_meter_provider
18+
from opentelemetry.sdk.metrics import Counter, MeterProvider
19+
from opentelemetry.sdk.metrics.export import (
20+
AggregationTemporality,
21+
ConsoleMetricExporter,
22+
PeriodicExportingMetricReader,
23+
)
24+
25+
# Use console exporter for the example
26+
exporter = ConsoleMetricExporter()
27+
28+
temporality_cumulative = {Counter: AggregationTemporality.CUMULATIVE}
29+
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
32+
reader = PeriodicExportingMetricReader(
33+
exporter,
34+
preferred_temporality=temporality_cumulative,
35+
export_interval_millis=5_000,
36+
)
37+
# Create a metric reader with delta preferred temporality
38+
# The metrics that are exported using this reader will represent a delta value
39+
reader2 = PeriodicExportingMetricReader(
40+
exporter,
41+
preferred_temporality=temporality_delta,
42+
export_interval_millis=5_000,
43+
)
44+
provider = MeterProvider(metric_readers=[reader, reader2])
45+
set_meter_provider(provider)
46+
47+
meter = get_meter_provider().get_meter("preferred-temporality", "0.1.2")
48+
49+
counter = meter.create_counter("my-counter")
50+
51+
# 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
53+
# of cumulative will keep a running sum of all values added.
54+
# The metric originating from the metric reader with a preferred temporality
55+
# 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)
File renamed without changes.

docs/examples/views/README.rst renamed to docs/examples/metrics/views/README.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ View common scenarios
33

44
These examples show how to customize the metrics that are output by the SDK using Views. There are multiple examples:
55

6-
* change_aggregation: Shows how to configure to change the default aggregation for an instrument.
7-
* change_name: Shows how to change the name of a metric.
8-
* limit_num_of_attrs: Shows how to limit the number of attributes that are output for a metric.
9-
* drop_metrics_from_instrument: Shows how to drop measurements from an instrument.
6+
* change_aggregation.py: Shows how to configure to change the default aggregation for an instrument.
7+
* change_name.py: Shows how to change the name of a metric.
8+
* limit_num_of_attrs.py: Shows how to limit the number of attributes that are output for a metric.
9+
* drop_metrics_from_instrument.py: Shows how to drop measurements from an instrument.
1010

11-
The source files of these examples are available :scm_web:`here <docs/examples/views/>`.
11+
The source files of these examples are available :scm_web:`here <docs/examples/metrics/views/>`.
1212

1313

1414
Installation
@@ -31,6 +31,6 @@ Useful links
3131
------------
3232

3333
- OpenTelemetry_
34-
- :doc:`../../api/metrics`
34+
- :doc:`../../../api/metrics`
3535

3636
.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)