-
Notifications
You must be signed in to change notification settings - Fork 624
/
example.py
55 lines (39 loc) · 1.51 KB
/
example.py
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
from typing import Iterable
from opentelemetry._metrics import get_meter_provider, set_meter_provider
from opentelemetry._metrics.observation import Observation
from opentelemetry.exporter.otlp.proto.grpc._metric_exporter import (
OTLPMetricExporter,
)
from opentelemetry.sdk._metrics import MeterProvider
from opentelemetry.sdk._metrics.export import PeriodicExportingMetricReader
exporter = OTLPMetricExporter(insecure=True)
reader = PeriodicExportingMetricReader(exporter)
provider = MeterProvider(metric_readers=[reader])
set_meter_provider(provider)
def observable_counter_func() -> Iterable[Observation]:
yield Observation(1, {})
def observable_up_down_counter_func() -> Iterable[Observation]:
yield Observation(-10, {})
def observable_gauge_func() -> Iterable[Observation]:
yield Observation(9, {})
meter = get_meter_provider().get_meter("getting-started", "0.1.2")
# Counter
counter = meter.create_counter("counter")
counter.add(1)
# Async Counter
observable_counter = meter.create_observable_counter(
"observable_counter", observable_counter_func
)
# UpDownCounter
updown_counter = meter.create_up_down_counter("updown_counter")
updown_counter.add(1)
updown_counter.add(-5)
# Async UpDownCounter
observable_updown_counter = meter.create_observable_up_down_counter(
"observable_updown_counter", observable_up_down_counter_func
)
# Histogram
histogram = meter.create_histogram("histogram")
histogram.record(99.9)
# Async Gauge
gauge = meter.create_observable_gauge("gauge", observable_gauge_func)