Duibonduil commited on
Commit
1992190
·
verified ·
1 Parent(s): f1a8789

Upload prometheus_example.py

Browse files
examples/metric/prometheus_example.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import time
3
+ from aworld.metrics.metric import MetricType
4
+ from aworld.metrics.context_manager import MetricContext, ApiMetricTracker
5
+ from aworld.metrics.template import MetricTemplate
6
+
7
+ MetricContext.configure(
8
+ provider="prometheus",
9
+ backend="console"
10
+ )
11
+
12
+ # my_counter = get_metric_provider().create_counter("my_counter", "test_counter_desc", "count")
13
+
14
+ # while 1:
15
+ # my_counter.add(random.randint(1, 10), )
16
+ # time.sleep(random.random())
17
+
18
+
19
+ my_counter = MetricTemplate(
20
+ type=MetricType.COUNTER,
21
+ name="my_counter",
22
+ description="My custom counter",
23
+ unit="1"
24
+ )
25
+
26
+ my_gauge = MetricTemplate(
27
+ type=MetricType.GAUGE,
28
+ name="my_gauge"
29
+ )
30
+
31
+ my_histogram = MetricTemplate(
32
+ type=MetricType.HISTOGRAM,
33
+ name="my_histogram",
34
+ buckets=[2, 4, 6, 8, 10]
35
+ )
36
+
37
+
38
+ @ApiMetricTracker()
39
+ def api():
40
+ time.sleep(random.uniform(0, 1))
41
+
42
+
43
+ def custom_code():
44
+ with ApiMetricTracker("test_custom_code"):
45
+ time.sleep(random.uniform(0, 1))
46
+
47
+
48
+ if __name__ == '__main__':
49
+ while 1:
50
+ MetricContext.count(my_counter, random.randint(1, 10))
51
+ MetricContext.gauge_set(my_gauge, random.randint(1, 10))
52
+ MetricContext.histogram_record(my_histogram, random.randint(1, 10))
53
+ api()
54
+ custom_code()
55
+ time.sleep(random.random())