Spaces:
Sleeping
Sleeping
Upload opentelemetry_example.py
Browse files
examples/opentelemetry_example.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import time
|
3 |
+
import os
|
4 |
+
os.environ["MONITOR_SERVICE_NAME"] = "otlp_example"
|
5 |
+
# os.environ["LOGFIRE_WRITE_TOKEN"] = ""
|
6 |
+
os.environ["ANT_OTEL_ENDPOINT"] = "https://antcollector.alipay.com/namespace/aworld/task/aworld/otlp/api/v1/metrics"
|
7 |
+
os.environ["METRICS_SYSTEM_ENABLED"] = "true"
|
8 |
+
|
9 |
+
from aworld.metrics.metric import MetricType
|
10 |
+
from aworld.metrics.context_manager import MetricContext, ApiMetricTracker
|
11 |
+
from aworld.metrics.template import MetricTemplate
|
12 |
+
|
13 |
+
MetricContext.configure(provider="otlp",
|
14 |
+
backend="antmonitor"
|
15 |
+
)
|
16 |
+
|
17 |
+
|
18 |
+
my_counter = MetricTemplate(
|
19 |
+
type=MetricType.COUNTER,
|
20 |
+
name="my_counter",
|
21 |
+
description="My custom counter",
|
22 |
+
unit="1"
|
23 |
+
)
|
24 |
+
|
25 |
+
my_gauge = MetricTemplate(
|
26 |
+
type=MetricType.GAUGE,
|
27 |
+
name="my_gauge"
|
28 |
+
)
|
29 |
+
|
30 |
+
my_histogram = MetricTemplate(
|
31 |
+
type=MetricType.HISTOGRAM,
|
32 |
+
name="my_histogram",
|
33 |
+
buckets=[2,4,6,8,10]
|
34 |
+
)
|
35 |
+
|
36 |
+
@ApiMetricTracker()
|
37 |
+
def api():
|
38 |
+
time.sleep(random.uniform(0, 1))
|
39 |
+
|
40 |
+
def custom_code():
|
41 |
+
with ApiMetricTracker("test_custom_code"):
|
42 |
+
time.sleep(random.uniform(0, 1))
|
43 |
+
|
44 |
+
|
45 |
+
if __name__ == '__main__':
|
46 |
+
while 1:
|
47 |
+
MetricContext.count(my_counter, 1, {"test_label": "b"})
|
48 |
+
MetricContext.gauge_set(my_gauge, random.randint(1, 10), {"test_label": "b"})
|
49 |
+
# MetricContext.histogram_record(my_histogram, random.randint(0, 1000))
|
50 |
+
# api()
|
51 |
+
# custom_code()
|
52 |
+
time.sleep(random.random())
|