Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit dfae33d

Browse files
MarceloAquino7liyanhui1228
authored andcommitted
Stackdriver stats exporter (#250)
1 parent d59ee42 commit dfae33d

9 files changed

Lines changed: 1119 additions & 4 deletions

File tree

README.rst

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ OpenCensus for Python - A stats collection and distributed tracing framework
1616

1717
.. _API Documentation: https://opencensus.io/api/python/trace/usage.html
1818

19+
--------
20+
Tracing
21+
--------
22+
1923
Installation & basic usage
2024
--------------------------
2125

@@ -210,7 +214,7 @@ For Django, you can configure the blacklist in the ``OPENCENSUS_PARAMS`` in ``se
210214
}
211215
212216
213-
.. note:: By default the health check path for the App Engine flexible environment is not traced,
217+
.. note:: By default, the health check path for the App Engine flexible environment is not traced,
214218
but you can turn it on by excluding it from the blacklist setting.
215219

216220
Framework Integration
@@ -219,7 +223,7 @@ Framework Integration
219223
Census supports integration with popular web frameworks including
220224
Django, Flask, Pyramid, and Webapp2. When the application receives a HTTP request,
221225
the tracer will automatically generate a span context using the trace
222-
information extracted from the request headers, and propagated to the
226+
information extracted from the request headers and propagated to the
223227
child spans.
224228

225229
Flask
@@ -397,6 +401,70 @@ You can enable Requests integration by specifying ``'requests'`` to ``trace_inte
397401

398402
.. _Requests package: https://pypi.python.org/pypi/requests
399403

404+
------
405+
Stats
406+
------
407+
408+
Stackdriver Stats
409+
-----------------
410+
411+
The OpenCensus Stackdriver Stats Exporter allows users
412+
to export metrics to Stackdriver Monitoring.
413+
The API of this project is still evolving.
414+
The use of vendoring or a dependency management tool is recommended.
415+
416+
.. _Stackdriver: https://app.google.stackdriver.com/metrics-explorer
417+
418+
Exporter Usage
419+
~~~~~~~~~~~~~~
420+
421+
Import
422+
******
423+
424+
.. code:: python
425+
426+
from opencensus.stats.exporters import stackdriver_exporter as stackdriver
427+
from opencensus.stats import stats as stats_module
428+
429+
Prerequisites
430+
*************
431+
432+
- OpenCensus Python libraries require Python 2.7 or later.
433+
- Google Cloud Platform account and project.
434+
- Google Stackdriver Monitoring enabled on your project (Need help? `Click here`_).
435+
436+
.. _Click here: https://opencensus.io/codelabs/stackdriver
437+
438+
Register the exporter
439+
*********************
440+
.. code:: python
441+
442+
stats = stats_module.Stats()
443+
view_manager = stats.view_manager
444+
445+
exporter = stackdriver.new_stats_exporter(stackdriver.Options(project_id="<id_value>"))
446+
view_manager.register_exporter(exporter)
447+
...
448+
449+
450+
Code Reference
451+
**************
452+
453+
In the *examples* folder, you can find all the necessary steps to get the exporter, register a view, put tags on the measure, and see the values against the Stackdriver monitoring tool once you have defined the *project_id*.
454+
455+
For further details for the Stackdriver implementation, see the file *stackdriver_exporter.py*.
456+
457+
+----------------------------------------------------+-------------------------------------+
458+
| Path & File | Short Description |
459+
+====================================================+=====================================+
460+
| examples/stats/exporter/stackdriver.py | End to end example |
461+
+----------------------------------------------------+-------------------------------------+
462+
| opencensus/stats/exporters/stackdriver_exporter.py | Stats implementation for Stackdriver|
463+
+----------------------------------------------------+-------------------------------------+
464+
465+
------------------
466+
Additional Info
467+
------------------
400468

401469
Contributing
402470
------------
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2018, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import time
16+
import random
17+
from opencensus.stats import aggregation as aggregation_module
18+
from opencensus.stats.exporters import stackdriver_exporter as stackdriver
19+
from opencensus.stats import measure as measure_module
20+
from opencensus.stats import stats as stats_module
21+
from opencensus.stats import view as view_module
22+
from opencensus.tags import tag_key as tag_key_module
23+
from opencensus.tags import tag_map as tag_map_module
24+
from opencensus.tags import tag_value as tag_value_module
25+
26+
MiB = 1 << 20
27+
FRONTEND_KEY = tag_key_module.TagKey("my.org/keys/frontend")
28+
VIDEO_SIZE_MEASURE = measure_module.MeasureInt(
29+
"my.org/measure/video_size_test2", "size of processed videos", "By")
30+
VIDEO_SIZE_VIEW_NAME = "my.org/views/video_size_test2"
31+
VIDEO_SIZE_DISTRIBUTION = aggregation_module.DistributionAggregation(
32+
[0.0, 16.0 * MiB, 256.0 * MiB])
33+
VIDEO_SIZE_VIEW = view_module.View(VIDEO_SIZE_VIEW_NAME,
34+
"processed video size over time",
35+
[FRONTEND_KEY],
36+
VIDEO_SIZE_MEASURE,
37+
VIDEO_SIZE_DISTRIBUTION)
38+
39+
40+
41+
stats = stats_module.Stats()
42+
view_manager = stats.view_manager
43+
stats_recorder = stats.stats_recorder
44+
45+
exporter = stackdriver.new_stats_exporter(stackdriver.Options(project_id="opencenus-node"))
46+
view_manager.register_exporter(exporter)
47+
48+
# Register view.
49+
view_manager.register_view(VIDEO_SIZE_VIEW)
50+
51+
# Sleep for [0, 10] milliseconds to fake work.
52+
time.sleep(random.randint(1, 10) / 1000.0)
53+
54+
# Process video.
55+
# Record the processed video size.
56+
tag_value = tag_value_module.TagValue(1200)
57+
tag_map = tag_map_module.TagMap()
58+
tag_map.insert(FRONTEND_KEY, tag_value)
59+
measure_map = stats_recorder.new_measurement_map()
60+
measure_map.measure_int_put(VIDEO_SIZE_MEASURE, 25 * MiB)
61+
62+
measure_map.record(tag_map)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2018, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

opencensus/stats/exporters/base.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2018, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Module containing base class for exporters."""
16+
17+
18+
class StatsExporter(object):
19+
"""Base class for opencensus stats exporters.
20+
21+
Subclasses of :class:`Exporter` must override :meth:`export`.
22+
"""
23+
24+
def on_register_view(self, view):
25+
"""
26+
:type view: object of :class:
27+
`~opencensus.stats.view.View`
28+
:param object of opencensus.stats.view.View view:
29+
View object to register
30+
"""
31+
raise NotImplementedError
32+
33+
def emit(self, view_datas):
34+
"""Send view and measurement to exporter record method,
35+
and then it will record on its own way.
36+
37+
:type view_datas: object of :class:
38+
`~opencensus.stats.view_data.ViewData`
39+
:param list of opencensus.stats.view_data.ViewData ViewData:
40+
list of ViewData object to send to Stackdriver Monitoring
41+
"""
42+
raise NotImplementedError

0 commit comments

Comments
 (0)