Skip to content

Commit d655eb8

Browse files
authored
Add disable default views to common view scenarios (#2983)
1 parent 4055dcf commit d655eb8

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 MeterProvider
20+
from opentelemetry.sdk.metrics.export import (
21+
ConsoleMetricExporter,
22+
PeriodicExportingMetricReader,
23+
)
24+
from opentelemetry.sdk.metrics.view import (
25+
DropAggregation,
26+
SumAggregation,
27+
View,
28+
)
29+
30+
# disable_default_aggregation.
31+
disable_default_aggregation = View(
32+
instrument_name="*", aggregation=DropAggregation()
33+
)
34+
35+
exporter = ConsoleMetricExporter()
36+
37+
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=1_000)
38+
provider = MeterProvider(
39+
metric_readers=[
40+
reader,
41+
],
42+
views=[
43+
disable_default_aggregation,
44+
View(instrument_name="mycounter", aggregation=SumAggregation()),
45+
],
46+
)
47+
set_meter_provider(provider)
48+
49+
meter = get_meter_provider().get_meter(
50+
"view-disable-default-aggregation", "0.1.2"
51+
)
52+
# Create a view to configure aggregation specific for this counter.
53+
my_counter = meter.create_counter("mycounter")
54+
55+
while 1:
56+
my_counter.add(random.randint(1, 10))
57+
time.sleep(random.random())

0 commit comments

Comments
 (0)