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

Commit a4820a9

Browse files
authored
Refactor stats exporters into ext packages (#556)
1. Extracted Prometheus Stats exporter into opencensus.ext.prometheus. 2. Extracted Stackdriver Stats exporter and reuse opencensus.ext.stackdriver. 3. Renamed opencensus.stats.exporters.base to opencensus.stats.base_exporter. 4. Moved Prometheus/Stackdriver specific document/samples to ext packages.
1 parent cf70f02 commit a4820a9

24 files changed

Lines changed: 292 additions & 199 deletions

File tree

README.rst

Lines changed: 32 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,11 @@ and ``ProbabilitySampler``
122122
Exporters
123123
~~~~~~~~~
124124

125-
You can choose different exporters to send the traces to. By default,
126-
the traces are printed to stdout in JSON format. Other options include
127-
writing to a file, sending to Python logging, or reporting to
128-
Stackdriver.
125+
By default, the traces are printed to stdout in JSON format. You can choose
126+
different exporters to send the traces to. There are three built-in exporters,
127+
which are ``opencensus.trace.print_exporter``, ``opencensus.trace.file_exporter``
128+
and ``opencensus.trace.logging_exporter``, other exporters are provided as
129+
`extensions <#trace-exporter>`__.
129130

130131
This example shows how to configure OpenCensus to save the traces to a
131132
file:
@@ -138,41 +139,6 @@ file:
138139
exporter = file_exporter.FileExporter(file_name='traces')
139140
tracer = context_tracer.ContextTracer(exporter=exporter)
140141
141-
This example shows how to report the traces to Stackdriver Trace:
142-
143-
.. code:: python
144-
145-
from opencensus.ext.stackdriver import trace_exporter as stackdriver_exporter
146-
from opencensus.trace import tracer as tracer_module
147-
148-
exporter = stackdriver_exporter.StackdriverExporter(
149-
project_id='your_cloud_project')
150-
tracer = tracer_module.Tracer(exporter=exporter)
151-
152-
StackdriverExporter requires the google-cloud-trace package. Install
153-
google-cloud-trace using `pip`_ or `pipenv`_:
154-
155-
::
156-
157-
pip install google-cloud-trace
158-
pipenv install google-cloud-trace
159-
160-
By default, traces are exported synchronously, which introduces latency during
161-
your code's execution. To avoid blocking code execution, you can initialize
162-
your exporter to use a background thread.
163-
164-
This example shows how to configure OpenCensus to use a background thread:
165-
166-
.. code:: python
167-
168-
from opencensus.common.transports.async_ import AsyncTransport
169-
from opencensus.ext.stackdriver import trace_exporter as stackdriver_exporter
170-
from opencensus.trace import tracer as tracer_module
171-
172-
exporter = stackdriver_exporter.StackdriverExporter(
173-
project_id='your_cloud_project', transport=AsyncTransport)
174-
tracer = tracer_module.Tracer(exporter=exporter)
175-
176142
Propagators
177143
~~~~~~~~~~~
178144

@@ -205,10 +171,10 @@ This example shows how to use the ``TraceContextPropagator``:
205171
from opencensus.trace.tracer import Tracer
206172
207173
config_integration.trace_integrations(['httplib'])
208-
tracer = Tracer(propagator = TraceContextPropagator())
174+
tracer = Tracer(propagator=TraceContextPropagator())
209175
210-
with tracer.span(name = 'parent'):
211-
with tracer.span(name = 'child'):
176+
with tracer.span(name='parent'):
177+
with tracer.span(name='child'):
212178
response = requests.get('http://localhost:5000')
213179
214180
Blacklist Paths
@@ -217,7 +183,7 @@ Blacklist Paths
217183
You can specify which paths you do not want to trace by configuring the
218184
blacklist paths.
219185

220-
This example shows how to configure the blacklist to ignore the `_ah/health` endpoint
186+
This example shows how to configure the blacklist to ignore the ``_ah/health`` endpoint
221187
for a Flask application:
222188

223189
.. code:: python
@@ -242,6 +208,10 @@ For Django, you can configure the blacklist in the ``OPENCENSUS_TRACE_PARAMS`` i
242208
.. note:: By default, the health check path for the App Engine flexible environment is not traced,
243209
but you can turn it on by excluding it from the blacklist setting.
244210

211+
------------
212+
Extensions
213+
------------
214+
245215
Integration
246216
-----------
247217

@@ -261,137 +231,38 @@ OpenCensus supports integration with popular web frameworks, client libraries an
261231
- `SQLAlchemy`_
262232
- `threading`_
263233

234+
Trace Exporter
235+
--------------
236+
237+
- `Jaeger`_
238+
- `OCAgent`_
239+
- `Stackdriver`_
240+
- `Zipkin`_
241+
242+
Stats Exporter
243+
--------------
244+
245+
- `Prometheus`_
246+
- `Stackdriver`_
247+
264248
.. _Django: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-django
265249
.. _Flask: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-flask
266250
.. _Google Cloud Client Libraries: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-google-cloud-clientlibs
267251
.. _gRPC: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-grpc
268252
.. _httplib: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-httplib
253+
.. _Jaeger: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-jaeger
269254
.. _MySQL: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-mysql
255+
.. _OCAgent: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-ocagent
270256
.. _PostgreSQL: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-postgresql
257+
.. _Prometheus: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-prometheus
271258
.. _pymongo: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-pymongo
272259
.. _PyMySQL: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-pymysql
273260
.. _Pyramid: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-pyramid
274261
.. _requests: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-requests
275262
.. _SQLAlchemy: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-sqlalchemy
263+
.. _Stackdriver: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-stackdriver
276264
.. _threading: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-threading
277-
278-
------
279-
Stats
280-
------
281-
282-
Stackdriver Stats
283-
-----------------
284-
285-
The OpenCensus Stackdriver Stats Exporter allows users
286-
to export metrics to Stackdriver Monitoring.
287-
The API of this project is still evolving.
288-
The use of vendoring or a dependency management tool is recommended.
289-
290-
.. _Stackdriver: https://app.google.stackdriver.com/metrics-explorer
291-
292-
Stackdriver Exporter Usage
293-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
294-
295-
Stackdriver Import
296-
************************
297-
298-
.. code:: python
299-
300-
from opencensus.stats.exporters import stackdriver_exporter as stackdriver
301-
from opencensus.stats import stats as stats_module
302-
303-
Stackdriver Prerequisites
304-
**************************
305-
306-
- OpenCensus Python libraries require Python 2.7 or later.
307-
- Google Cloud Platform account and project.
308-
- Google Stackdriver Monitoring enabled on your project (Need help? `Click here`_).
309-
310-
.. _Click here: https://opencensus.io/codelabs/stackdriver
311-
312-
Register the Stackdriver exporter
313-
**********************************
314-
315-
.. code:: python
316-
317-
stats = stats_module.Stats()
318-
view_manager = stats.view_manager
319-
320-
exporter = stackdriver.new_stats_exporter(stackdriver.Options(project_id="<id_value>"))
321-
view_manager.register_exporter(exporter)
322-
...
323-
324-
325-
Stackdriver Code Reference
326-
******************************
327-
328-
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*.
329-
330-
For further details for the Stackdriver implementation, see the file *stackdriver_exporter.py*.
331-
332-
+----------------------------------------------------+-------------------------------------+
333-
| Path & File | Short Description |
334-
+====================================================+=====================================+
335-
| examples/stats/exporter/stackdriver.py | End to end example |
336-
+----------------------------------------------------+-------------------------------------+
337-
| opencensus/stats/exporters/stackdriver_exporter.py | Stats implementation for Stackdriver|
338-
+----------------------------------------------------+-------------------------------------+
339-
340-
Prometheus Stats
341-
-----------------
342-
343-
The OpenCensus `Prometheus`_ Stats Exporter allows users
344-
to export metrics to Prometheus monitoring solution.
345-
The API of this project is still evolving.
346-
The use of vendoring or a dependency management tool is recommended.
347-
348-
.. _Prometheus: https://prometheus.io/
349-
350-
Prometheus Exporter Usage
351-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
352-
353-
Prometheus Import
354-
********************
355-
356-
.. code:: python
357-
358-
from opencensus.stats.exporters import prometheus_exporter as prometheus
359-
from opencensus.stats import stats as stats_module
360-
361-
Prometheus Prerequisites
362-
***************************
363-
364-
- OpenCensus Python libraries require Python 2.7 or later.
365-
- Prometheus up and running.
366-
367-
Register the Prometheus exporter
368-
***********************************
369-
370-
.. code:: python
371-
372-
stats = stats_module.Stats()
373-
view_manager = stats.view_manager
374-
375-
exporter = prometheus.new_stats_exporter(prometheus.Options(namespace="<namespace>"))
376-
view_manager.register_exporter(exporter)
377-
...
378-
379-
380-
Prometheus Code Reference
381-
***************************
382-
383-
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 Prometheus monitoring tool.
384-
385-
For further details for the Prometheus implementation, see the file *prometheus_exporter.py*.
386-
387-
388-
+----------------------------------------------------+-------------------------------------+
389-
| Path & File | Short Description |
390-
+====================================================+=====================================+
391-
| examples/stats/exporter/prometheus.py | End to end example |
392-
+----------------------------------------------------+-------------------------------------+
393-
| opencensus/stats/exporters/prometheus_exporter.py | Stats implementation for Prometheus |
394-
+----------------------------------------------------+-------------------------------------+
265+
.. _Zipkin: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-zipkin
395266

396267
------------------
397268
Additional Info
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## Unreleased
4+
5+
- Initial version.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
OpenCensus Prometheus Exporter
2+
============================================================================
3+
4+
Installation
5+
------------
6+
7+
::
8+
9+
pip install opencensus-ext-prometheus
10+
11+
Usage
12+
-----
13+
14+
The OpenCensus `Prometheus`_ Stats Exporter allows users
15+
to export metrics to Prometheus monitoring solution.
16+
The API of this project is still evolving.
17+
The use of vendoring or a dependency management tool is recommended.
18+
19+
.. _Prometheus: https://prometheus.io/
20+
21+
Prometheus Exporter Usage
22+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23+
24+
Prometheus Import
25+
********************
26+
27+
.. code:: python
28+
29+
from opencensus.ext.prometheus import stats_exporter as prometheus
30+
from opencensus.stats import stats as stats_module
31+
32+
Prometheus Prerequisites
33+
***************************
34+
35+
- OpenCensus Python libraries require Python 2.7 or later.
36+
- Prometheus up and running.
37+
38+
Register the Prometheus exporter
39+
***********************************
40+
41+
.. code:: python
42+
43+
stats = stats_module.Stats()
44+
view_manager = stats.view_manager
45+
46+
exporter = prometheus.new_stats_exporter(prometheus.Options(namespace="<namespace>"))
47+
view_manager.register_exporter(exporter)
48+
...
49+
50+
51+
Prometheus Code Reference
52+
***************************
53+
54+
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 Prometheus monitoring tool.
55+
56+
For further details for the Prometheus implementation, see the folder *prometheus/stats_exporter*.
57+
58+
59+
+-------------------------------------------------------------------------------+-------------------------------------+
60+
| Path & File | Short Description |
61+
+===============================================================================+=====================================+
62+
| contrib/opencensus-ext-prometheus/examples/ | End to end example |
63+
+-------------------------------------------------------------------------------+-------------------------------------+
64+
| contrib/opencensus-ext-prometheus/opencensus/ext/prometheus/stats_exporter/ | Stats implementation for Prometheus |
65+
+-------------------------------------------------------------------------------+-------------------------------------+
66+

examples/stats/exporter/prometheus.py renamed to contrib/opencensus-ext-prometheus/examples/prometheus.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import random
1818
import time
1919

20+
from opencensus.ext.prometheus import stats_exporter as prometheus
2021
from opencensus.stats import aggregation as aggregation_module
21-
from opencensus.stats.exporters import prometheus_exporter as prometheus
2222
from opencensus.stats import measure as measure_module
2323
from opencensus.stats import stats as stats_module
2424
from opencensus.stats import view as view_module
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__('pkgutil').extend_path(__path__, __name__)

opencensus/stats/exporters/prometheus_exporter.py renamed to contrib/opencensus-ext-prometheus/opencensus/ext/prometheus/stats_exporter/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from opencensus.common.transports import sync
2424
from opencensus.stats import aggregation_data as aggregation_data_module
25-
from opencensus.stats.exporters import base
25+
from opencensus.stats import base_exporter
2626

2727
import re
2828

@@ -243,13 +243,13 @@ def collect(self): # pragma: NO COVER
243243
yield metric
244244

245245

246-
class PrometheusStatsExporter(base.StatsExporter):
246+
class PrometheusStatsExporter(base_exporter.StatsExporter):
247247
""" Exporter exports stats to Prometheus, users need
248248
to register the exporter as an HTTP Handler to be
249249
able to export.
250250
251251
:type options:
252-
:class:`~opencensus.stats.exporters.prometheus_exporters.Options`
252+
:class:`~opencensus.ext.prometheus.stats_exporter.Options`
253253
:param options: An options object with the parameters to instantiate the
254254
prometheus exporter.
255255
@@ -262,7 +262,7 @@ class PrometheusStatsExporter(base.StatsExporter):
262262
:param transport: An instance of a Transpor to send data with.
263263
264264
:type collector:
265-
:class:`~opencensus.stats.exporters.prometheus_exporters.Collector`
265+
:class:`~opencensus.ext.prometheus.stats_exporter.Collector`
266266
:param collector: An instance of the Prometheus Collector object.
267267
"""
268268
def __init__(self,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[bdist_wheel]
2+
universal = 1

0 commit comments

Comments
 (0)