forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace_exemplars.py
More file actions
84 lines (76 loc) · 2.71 KB
/
trace_exemplars.py
File metadata and controls
84 lines (76 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
This example shows how to generate trace exemplars for a histogram, and how to export them to Google Cloud Monitoring.
"""
import random
import time
from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider, ValueRecorder
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.aggregate import HistogramAggregator
from opentelemetry.sdk.metrics.view import View, ViewConfig
# Set up OpenTelemetry metrics
metrics.set_meter_provider(MeterProvider(stateful=False))
meter = metrics.get_meter(__name__)
# Use the Google Cloud Monitoring Metrics Exporter since its the only one that currently supports exemplars
metrics.get_meter_provider().start_pipeline(
meter, ConsoleMetricsExporter(), 10
)
# Create our duration metric
request_duration = meter.create_metric(
name="request_duration",
description="duration (ms) of incoming requests",
unit="ms",
value_type=int,
metric_type=ValueRecorder,
)
# Add a Histogram view to our duration metric, and make it generate 1 exemplars per bucket
duration_view = View(
request_duration,
# Latency in buckets:
# [>=0ms, >=25ms, >=50ms, >=75ms, >=100ms, >=200ms, >=400ms, >=600ms, >=800ms, >=1s, >=2s, >=4s, >=6s]
# We want to generate 1 exemplar per bucket, where each exemplar has a linked trace that was recorded.
# So we need to set num_exemplars to 1 and not specify statistical_exemplars (defaults to false)
HistogramAggregator,
aggregator_config={
"bounds": [
0,
25,
50,
75,
100,
200,
400,
600,
800,
1000,
2000,
4000,
6000,
],
"num_exemplars": 1,
},
label_keys=["environment"],
view_config=ViewConfig.LABEL_KEYS,
)
meter.register_view(duration_view)
for i in range(100):
# Generate some random data for the histogram with a dropped label "customer_id"
request_duration.record(
random.randint(1, 8000),
{"environment": "staging", "customer_id": random.randint(1, 100)},
)
time.sleep(1)