Skip to content

Commit 30a4d45

Browse files
authored
docs: add example to show how to drop measurements from an instrument (#2826)
1 parent 9b8646d commit 30a4d45

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

docs/examples/views/README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ These examples show how to customize the metrics that are output by the SDK usin
66
* change_aggregation: Shows how to configure to change the default aggregation for an instrument.
77
* change_name: Shows how to change the name of a metric.
88
* 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.
910

1011
The source files of these examples are available :scm_web:`here <docs/examples/views/>`.
1112

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 random
16+
import time
17+
18+
from opentelemetry.metrics import get_meter_provider, set_meter_provider
19+
from opentelemetry.sdk.metrics import Counter, MeterProvider
20+
from opentelemetry.sdk.metrics.export import (
21+
ConsoleMetricExporter,
22+
PeriodicExportingMetricReader,
23+
)
24+
from opentelemetry.sdk.metrics.view import DropAggregation, View
25+
26+
# Create a view matching the counter instrument `my.counter`
27+
# and configure the view to drop the aggregation.
28+
drop_aggregation_view = View(
29+
instrument_type=Counter,
30+
instrument_name="my.counter",
31+
aggregation=DropAggregation(),
32+
)
33+
34+
exporter = ConsoleMetricExporter()
35+
36+
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=1_000)
37+
provider = MeterProvider(
38+
metric_readers=[
39+
reader,
40+
],
41+
views=[
42+
drop_aggregation_view,
43+
],
44+
)
45+
set_meter_provider(provider)
46+
47+
meter = get_meter_provider().get_meter("view-drop-aggregation", "0.1.2")
48+
49+
my_counter = meter.create_counter("my.counter")
50+
51+
while 1:
52+
my_counter.add(random.randint(1, 10))
53+
time.sleep(random.random())

0 commit comments

Comments
 (0)