23
23
from opencensus .stats import stats
24
24
from opencensus .stats import view
25
25
from opencensus .stats .exporters import stackdriver_exporter
26
+ from opencensus .trace .exporters .transports .background_thread import BackgroundThreadTransport
27
+
26
28
from opencensus .tags import tag_key
29
+ from opencensus .tags import tag_value
30
+ from opencensus .tags import tag_map
27
31
28
32
# [START setup_exporter]
29
33
def initialize (project_id ):
30
- # Records latencies measured in milliseconds, grouped in buckets of
31
- # increasing size. Exports to StackDriver for the given project_id
32
- latency_measure = measure .MeasureFloat ('latency' , 'Latency in ms' , 'ms' )
34
+ key_method = tag_key .TagKey ('kind' )
35
+
36
+ latency_measure = measure .MeasureFloat (
37
+ 'latency/process_step' ,
38
+ 'Process Step Latency in milliseconds' ,
39
+ 'ms'
40
+ )
41
+
42
+ # Track count of latency measures falling in each range
33
43
latency_distribution = aggregation .DistributionAggregation (
34
44
[0 , 25 , 50 , 100 , 200 , 400 , 800 , 1600 ]
35
45
)
46
+
36
47
latency_view = view .View (
37
- 'latency' ,
48
+ 'latency/process_step ' ,
38
49
'Processing time' ,
39
- [tag_key . TagKey ( 'latency' ) ],
50
+ [key_method ],
40
51
latency_measure ,
41
52
latency_distribution
42
53
)
@@ -51,7 +62,7 @@ def initialize(project_id):
51
62
view_manager .register_exporter (exporter )
52
63
view_manager .register_view (latency_view )
53
64
54
- return recorder , latency_measure
65
+ return recorder , latency_measure , key_method
55
66
# [END setup_exporter]
56
67
57
68
@@ -65,10 +76,16 @@ def process(kind):
65
76
66
77
67
78
def main (project_id , iteration_count ):
68
- recorder , measure = initialize (project_id )
79
+ recorder , measure , key_method = initialize (project_id )
80
+
81
+ tag_kind = tag_value .TagValue ('simulation' )
82
+ key_map = tag_map .TagMap ()
83
+ key_map .insert (key_method , tag_kind )
84
+
69
85
measure_map = recorder .new_measurement_map ()
70
86
71
87
for i in range (iteration_count ):
88
+
72
89
# Randomly pick a kind of processing
73
90
if random .random () <= 0.5 :
74
91
kind = 'extra'
@@ -79,12 +96,15 @@ def main(project_id, iteration_count):
79
96
start_time = time .time ()
80
97
process (kind )
81
98
duration = time .time () - start_time
99
+ milliseconds = int (round (1000.0 * duration ))
82
100
83
101
# Record the measurement
84
- measure_map .measure_float_put (measure , duration )
85
- print (duration )
102
+ measure_map .measure_int_put (measure , milliseconds )
103
+
104
+ # TODO - remove debugging print statement
105
+ print (milliseconds )
86
106
87
- measure_map .record ()
107
+ measure_map .record (key_map )
88
108
time .sleep (60 )
89
109
# [END monitoring_opencensus_metrics_quickstart]
90
110
0 commit comments