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

Commit 0e586b3

Browse files
vcasadeiliyanhui1228
authored andcommitted
Prometheus exporter (#294)
1 parent cac4305 commit 0e586b3

10 files changed

Lines changed: 868 additions & 17 deletions

File tree

README.rst

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -443,28 +443,29 @@ The use of vendoring or a dependency management tool is recommended.
443443

444444
.. _Stackdriver: https://app.google.stackdriver.com/metrics-explorer
445445

446-
Exporter Usage
447-
~~~~~~~~~~~~~~
446+
Stackdriver Exporter Usage
447+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
448448

449-
Import
450-
******
449+
Stackdriver Import
450+
************************
451451

452452
.. code:: python
453453
454454
from opencensus.stats.exporters import stackdriver_exporter as stackdriver
455455
from opencensus.stats import stats as stats_module
456456
457-
Prerequisites
458-
*************
457+
Stackdriver Prerequisites
458+
**************************
459459

460460
- OpenCensus Python libraries require Python 2.7 or later.
461461
- Google Cloud Platform account and project.
462462
- Google Stackdriver Monitoring enabled on your project (Need help? `Click here`_).
463463

464464
.. _Click here: https://opencensus.io/codelabs/stackdriver
465465

466-
Register the exporter
467-
*********************
466+
Register the Stackdriver exporter
467+
**********************************
468+
468469
.. code:: python
469470
470471
stats = stats_module.Stats()
@@ -475,8 +476,8 @@ Register the exporter
475476
...
476477
477478
478-
Code Reference
479-
**************
479+
Stackdriver Code Reference
480+
******************************
480481

481482
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*.
482483

@@ -490,6 +491,62 @@ For further details for the Stackdriver implementation, see the file *stackdrive
490491
| opencensus/stats/exporters/stackdriver_exporter.py | Stats implementation for Stackdriver|
491492
+----------------------------------------------------+-------------------------------------+
492493

494+
Prometheus Stats
495+
-----------------
496+
497+
The OpenCensus `Prometheus`_ Stats Exporter allows users
498+
to export metrics to Prometheus monitoring solution.
499+
The API of this project is still evolving.
500+
The use of vendoring or a dependency management tool is recommended.
501+
502+
.. _Prometheus: https://prometheus.io/
503+
504+
Prometheus Exporter Usage
505+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
506+
507+
Prometheus Import
508+
********************
509+
510+
.. code:: python
511+
512+
from opencensus.stats.exporters import prometheus_exporter as prometheus
513+
from opencensus.stats import stats as stats_module
514+
515+
Prometheus Prerequisites
516+
***************************
517+
518+
- OpenCensus Python libraries require Python 2.7 or later.
519+
- Prometheus up and running.
520+
521+
Register the Prometheus exporter
522+
***********************************
523+
524+
.. code:: python
525+
526+
stats = stats_module.Stats()
527+
view_manager = stats.view_manager
528+
529+
exporter = prometheus.new_stats_exporter(prometheus.Options(namespace="<namespace>"))
530+
view_manager.register_exporter(exporter)
531+
...
532+
533+
534+
Prometheus Code Reference
535+
***************************
536+
537+
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.
538+
539+
For further details for the Prometheus implementation, see the file *prometheus_exporter.py*.
540+
541+
542+
+----------------------------------------------------+-------------------------------------+
543+
| Path & File | Short Description |
544+
+====================================================+=====================================+
545+
| examples/stats/exporter/prometheus.py | End to end example |
546+
+----------------------------------------------------+-------------------------------------+
547+
| opencensus/stats/exporters/prometheus_exporter.py | Stats implementation for Prometheus |
548+
+----------------------------------------------------+-------------------------------------+
549+
493550
------------------
494551
Additional Info
495552
------------------
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018, OpenCensus Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import random
18+
import time
19+
20+
from opencensus.stats import aggregation as aggregation_module
21+
from opencensus.stats.exporters import prometheus_exporter as prometheus
22+
from opencensus.stats import measure as measure_module
23+
from opencensus.stats import stats as stats_module
24+
from opencensus.stats import view as view_module
25+
from opencensus.tags import tag_key as tag_key_module
26+
from opencensus.tags import tag_map as tag_map_module
27+
from opencensus.tags import tag_value as tag_value_module
28+
from pprint import pprint
29+
30+
MiB = 1 << 20
31+
FRONTEND_KEY = tag_key_module.TagKey("my.org/keys/frontend")
32+
VIDEO_SIZE_MEASURE = measure_module.MeasureInt(
33+
"my.org/measures/video_size", "size of processed videos", "By")
34+
VIDEO_SIZE_VIEW_NAME = "my.org/views/video_size"
35+
VIDEO_SIZE_DISTRIBUTION = aggregation_module.DistributionAggregation(
36+
[0.0, 16.0 * MiB, 256.0 * MiB])
37+
VIDEO_SIZE_VIEW = view_module.View(VIDEO_SIZE_VIEW_NAME,
38+
"processed video size over time",
39+
[FRONTEND_KEY],
40+
VIDEO_SIZE_MEASURE,
41+
VIDEO_SIZE_DISTRIBUTION)
42+
43+
44+
def main():
45+
stats = stats_module.Stats()
46+
view_manager = stats.view_manager
47+
stats_recorder = stats.stats_recorder
48+
49+
exporter = prometheus.new_stats_exporter(prometheus.Options(namespace="opencensus"))
50+
view_manager.register_exporter(exporter)
51+
52+
# Register view.
53+
view_manager.register_view(VIDEO_SIZE_VIEW)
54+
55+
# Sleep for [0, 10] milliseconds to fake work.
56+
time.sleep(random.randint(1, 10) / 1000.0)
57+
58+
# Process video.
59+
# Record the processed video size.
60+
tag_value = tag_value_module.TagValue(str(random.randint(1, 10000)))
61+
tag_map = tag_map_module.TagMap()
62+
tag_map.insert(FRONTEND_KEY, tag_value)
63+
measure_map = stats_recorder.new_measurement_map()
64+
measure_map.measure_int_put(VIDEO_SIZE_MEASURE, 25 * MiB)
65+
measure_map.record(tag_map)
66+
67+
# Use the line below to see the data on prometheus
68+
# while True:
69+
# pass
70+
71+
# Get aggregated stats and print it to console.
72+
view_data = view_manager.get_view(VIDEO_SIZE_VIEW_NAME)
73+
pprint(vars(view_data))
74+
for k, v in view_data._tag_value_aggregation_data_map.items():
75+
pprint(k)
76+
pprint(vars(v))
77+
78+
79+
if __name__ == '__main__':
80+
main()

opencensus/stats/exporters/__init__.py

100644100755
File mode changed.

opencensus/stats/exporters/base.py

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def on_register_view(self, view):
2828
:param object of opencensus.stats.view.View view:
2929
View object to register
3030
"""
31-
raise NotImplementedError
31+
raise NotImplementedError # pragma: NO COVER
3232

3333
def emit(self, view_datas):
3434
"""Send view and measurement to exporter record method,
@@ -39,4 +39,4 @@ def emit(self, view_datas):
3939
:param list of opencensus.stats.view_data.ViewData ViewData:
4040
list of ViewData object to send to Stackdriver Monitoring
4141
"""
42-
raise NotImplementedError
42+
raise NotImplementedError # pragma: NO COVER

0 commit comments

Comments
 (0)