Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f6e1eda

Browse filesBrowse files
committed
Exporting to Stackdriver
1 parent 765a277 commit f6e1eda
Copy full SHA for f6e1eda

File tree

Expand file treeCollapse file tree

1 file changed

+30
-10
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+30
-10
lines changed

‎custom_metrics/quickstart/quickstart.py

Copy file name to clipboardExpand all lines: custom_metrics/quickstart/quickstart.py
+30-10Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,31 @@
2323
from opencensus.stats import stats
2424
from opencensus.stats import view
2525
from opencensus.stats.exporters import stackdriver_exporter
26+
from opencensus.trace.exporters.transports.background_thread import BackgroundThreadTransport
27+
2628
from opencensus.tags import tag_key
29+
from opencensus.tags import tag_value
30+
from opencensus.tags import tag_map
2731

2832
# [START setup_exporter]
2933
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
3343
latency_distribution = aggregation.DistributionAggregation(
3444
[0, 25, 50, 100, 200, 400, 800, 1600]
3545
)
46+
3647
latency_view = view.View(
37-
'latency',
48+
'latency/process_step',
3849
'Processing time',
39-
[tag_key.TagKey('latency')],
50+
[key_method],
4051
latency_measure,
4152
latency_distribution
4253
)
@@ -51,7 +62,7 @@ def initialize(project_id):
5162
view_manager.register_exporter(exporter)
5263
view_manager.register_view(latency_view)
5364

54-
return recorder, latency_measure
65+
return recorder, latency_measure, key_method
5566
# [END setup_exporter]
5667

5768

@@ -65,10 +76,16 @@ def process(kind):
6576

6677

6778
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+
6985
measure_map = recorder.new_measurement_map()
7086

7187
for i in range(iteration_count):
88+
7289
# Randomly pick a kind of processing
7390
if random.random() <= 0.5:
7491
kind = 'extra'
@@ -79,12 +96,15 @@ def main(project_id, iteration_count):
7996
start_time = time.time()
8097
process(kind)
8198
duration = time.time() - start_time
99+
milliseconds = int(round(1000.0 * duration))
82100

83101
# 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)
86106

87-
measure_map.record()
107+
measure_map.record(key_map)
88108
time.sleep(60)
89109
# [END monitoring_opencensus_metrics_quickstart]
90110

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.