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

Commit 0ec23a6

Browse files
lzchenreyang
authored andcommitted
Skeleton for Azure Metrics Exporter (#678)
Add skeleton metrics exporter to azure
1 parent 23ccd8a commit 0ec23a6

File tree

7 files changed

+354
-58
lines changed

7 files changed

+354
-58
lines changed

README.rst

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,15 @@ OpenCensus supports integration with popular web frameworks, client libraries an
205205
- `SQLAlchemy`_
206206
- `threading`_
207207

208-
Trace Exporter
209-
--------------
208+
Log Exporter
209+
------------
210+
211+
- `Azure`_
212+
213+
Metrics Exporter
214+
----------------
210215

211216
- `Azure`_
212-
- `Jaeger`_
213-
- `OCAgent`_
214-
- `Stackdriver`_
215-
- `Zipkin`_
216217

217218
Stats Exporter
218219
--------------
@@ -221,6 +222,15 @@ Stats Exporter
221222
- `Prometheus`_
222223
- `Stackdriver`_
223224

225+
Trace Exporter
226+
--------------
227+
228+
- `Azure`_
229+
- `Jaeger`_
230+
- `OCAgent`_
231+
- `Stackdriver`_
232+
- `Zipkin`_
233+
224234
.. _Azure: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-azure
225235
.. _Django: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-django
226236
.. _Flask: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-flask
@@ -243,11 +253,6 @@ Stats Exporter
243253
.. _threading: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-threading
244254
.. _Zipkin: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-zipkin
245255

246-
Log Exporter
247-
--------------
248-
249-
- `Azure`_
250-
251256
------------
252257
Versioning
253258
------------

contrib/opencensus-ext-azure/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changelog
22

33
## Unreleased
4+
- Added metrics exporter
5+
([#678](https://github.com/census-instrumentation/opencensus-python/pull/678)
46

57
## 0.2.1
68
Released 2019-06-13

contrib/opencensus-ext-azure/README.rst

Lines changed: 109 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,6 @@ Installation
1616
Usage
1717
-----
1818

19-
Trace
20-
~~~~~
21-
22-
The **Azure Monitor Trace Exporter** allows you to export `OpenCensus`_ traces to `Azure Monitor`_.
23-
24-
This example shows how to send a span "hello" to Azure Monitor.
25-
26-
* Create an Azure Monitor resource and get the instrumentation key, more information can be found `here <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_.
27-
* Put the instrumentation key in ``APPINSIGHTS_INSTRUMENTATIONKEY`` environment variable.
28-
29-
.. code:: python
30-
31-
from opencensus.ext.azure.trace_exporter import AzureExporter
32-
from opencensus.trace.samplers import ProbabilitySampler
33-
from opencensus.trace.tracer import Tracer
34-
35-
tracer = Tracer(exporter=AzureExporter(), sampler=ProbabilitySampler(1.0))
36-
37-
with tracer.span(name='hello'):
38-
print('Hello, World!')
39-
40-
You can also specify the instrumentation key explicitly in the code.
41-
42-
* Create an Azure Monitor resource and get the instrumentation key, more information can be found `here <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_.
43-
* Install the `requests integration package <../opencensus-ext-requests>`_ using ``pip install opencensus-ext-requests``.
44-
* Put the instrumentation key in the following code.
45-
46-
.. code:: python
47-
48-
import requests
49-
50-
from opencensus.ext.azure.trace_exporter import AzureExporter
51-
from opencensus.trace import config_integration
52-
from opencensus.trace.samplers import ProbabilitySampler
53-
from opencensus.trace.tracer import Tracer
54-
55-
config_integration.trace_integrations(['requests'])
56-
tracer = Tracer(
57-
exporter=AzureExporter(
58-
# TODO: replace this with your own instrumentation key.
59-
instrumentation_key='00000000-0000-0000-0000-000000000000',
60-
),
61-
sampler=ProbabilitySampler(1.0),
62-
)
63-
with tracer.span(name='parent'):
64-
response = requests.get(url='https://www.wikipedia.org/wiki/Rabbit')
65-
6619
Log
6720
~~~
6821

@@ -72,6 +25,7 @@ This example shows how to send a warning level log to Azure Monitor.
7225

7326
* Create an Azure Monitor resource and get the instrumentation key, more information can be found `here <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_.
7427
* Put the instrumentation key in ``APPINSIGHTS_INSTRUMENTATIONKEY`` environment variable.
28+
* You can also specify the instrumentation key explicitly in the code, which will take priority over a set environment variable.
7529

7630
.. code:: python
7731
@@ -88,6 +42,7 @@ You can enrich the logs with trace IDs and span IDs by using the `logging integr
8842
* Create an Azure Monitor resource and get the instrumentation key, more information can be found `here <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_.
8943
* Install the `logging integration package <../opencensus-ext-logging>`_ using ``pip install opencensus-ext-logging``.
9044
* Put the instrumentation key in ``APPINSIGHTS_INSTRUMENTATIONKEY`` environment variable.
45+
* You can also specify the instrumentation key explicitly in the code, which will take priority over a set environment variable.
9146

9247
.. code:: python
9348
@@ -114,6 +69,113 @@ You can enrich the logs with trace IDs and span IDs by using the `logging integr
11469
logger.warning('In the span')
11570
logger.warning('After the span')
11671
72+
Metrics
73+
~~~~~~~
74+
75+
The **OpenCensus Azure Monitor Metrics Exporter** allows you to export metrics to `Azure Monitor`_.
76+
77+
* Create an Azure Monitor resource and get the instrumentation key, more information can be found `here <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_.
78+
* Put the instrumentation key in ``APPINSIGHTS_INSTRUMENTATIONKEY`` environment variable.
79+
* You can also specify the instrumentation key explicitly in the code, which will take priority over a set environment variable.
80+
81+
Using the Metrics exporter
82+
*****************************
83+
84+
.. code:: python
85+
86+
import time
87+
88+
from opencensus.ext.azure import metrics_exporter
89+
from opencensus.stats import aggregation as aggregation_module
90+
from opencensus.stats import measure as measure_module
91+
from opencensus.stats import stats as stats_module
92+
from opencensus.stats import view as view_module
93+
from opencensus.tags import tag_map as tag_map_module
94+
95+
stats = stats_module.stats
96+
view_manager = stats.view_manager
97+
stats_recorder = stats.stats_recorder
98+
99+
CARROTS_MEASURE = measure_module.MeasureInt("carrots",
100+
"number of carrots",
101+
"carrots")
102+
CARROTS_VIEW = view_module.View("carrots_view",
103+
"number of carrots",
104+
[],
105+
CARROTS_MEASURE,
106+
aggregation_module.CountAggregation())
107+
108+
109+
def main():
110+
# Enable metrics
111+
# Set the interval in seconds in which you want to send metrics
112+
exporter = metrics_exporter.new_metrics_exporter(export_interval=2)
113+
view_manager.register_exporter(exporter)
114+
115+
view_manager.register_view(CARROTS_VIEW)
116+
mmap = stats_recorder.new_measurement_map()
117+
tmap = tag_map_module.TagMap()
118+
119+
mmap.measure_int_put(CARROTS_MEASURE, 1000)
120+
mmap.record(tmap)
121+
time.sleep(10)
122+
123+
print("Done recording metrics")
124+
125+
126+
if __name__ == "__main__":
127+
main()
128+
129+
Trace
130+
~~~~~
131+
132+
The **Azure Monitor Trace Exporter** allows you to export `OpenCensus`_ traces to `Azure Monitor`_.
133+
134+
This example shows how to send a span "hello" to Azure Monitor.
135+
136+
* Create an Azure Monitor resource and get the instrumentation key, more information can be found `here <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_.
137+
* Put the instrumentation key in ``APPINSIGHTS_INSTRUMENTATIONKEY`` environment variable.
138+
* You can also specify the instrumentation key explicitly in the code, which will take priority over a set environment variable.
139+
140+
.. code:: python
141+
142+
from opencensus.ext.azure.trace_exporter import AzureExporter
143+
from opencensus.trace.samplers import ProbabilitySampler
144+
from opencensus.trace.tracer import Tracer
145+
146+
tracer = Tracer(exporter=AzureExporter(), sampler=ProbabilitySampler(1.0))
147+
148+
with tracer.span(name='hello'):
149+
print('Hello, World!')
150+
151+
You can also specify the instrumentation key explicitly in the code.
152+
153+
* Create an Azure Monitor resource and get the instrumentation key, more information can be found `here <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_.
154+
* Install the `requests integration package <../opencensus-ext-requests>`_ using ``pip install opencensus-ext-requests``.
155+
* Put the instrumentation key in ``APPINSIGHTS_INSTRUMENTATIONKEY`` environment variable.
156+
* You can also specify the instrumentation key explicitly in the code, which will take priority over a set environment variable.
157+
158+
.. code:: python
159+
160+
import requests
161+
162+
from opencensus.ext.azure.trace_exporter import AzureExporter
163+
from opencensus.trace import config_integration
164+
from opencensus.trace.samplers import ProbabilitySampler
165+
from opencensus.trace.tracer import Tracer
166+
167+
config_integration.trace_integrations(['requests'])
168+
tracer = Tracer(
169+
exporter=AzureExporter(
170+
# TODO: replace this with your own instrumentation key.
171+
instrumentation_key='00000000-0000-0000-0000-000000000000',
172+
),
173+
sampler=ProbabilitySampler(1.0),
174+
)
175+
with tracer.span(name='parent'):
176+
response = requests.get(url='https://www.wikipedia.org/wiki/Rabbit')
177+
178+
117179
References
118180
----------
119181

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright 2019, 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 random
16+
import time
17+
18+
from opencensus.ext.azure import metrics_exporter
19+
from opencensus.stats import aggregation as aggregation_module
20+
from opencensus.stats import measure as measure_module
21+
from opencensus.stats import stats as stats_module
22+
from opencensus.stats import view as view_module
23+
from opencensus.tags import tag_map as tag_map_module
24+
25+
stats = stats_module.stats
26+
view_manager = stats.view_manager
27+
stats_recorder = stats.stats_recorder
28+
29+
# Create the measures
30+
# The latency in milliseconds
31+
m_latency_ms = measure_module.MeasureFloat(
32+
"task_latency", "The task latency in milliseconds", "ms")
33+
34+
# Create a view in which defines an aggregation and tag keys
35+
latency_view = view_module.View(
36+
"task_latency_distribution",
37+
"The distribution of the task latencies",
38+
[],
39+
m_latency_ms,
40+
# Latency in buckets: [>=0ms, >=100ms, >=200ms, >=400ms, >=1s, >=2s, >=4s]
41+
aggregation_module.DistributionAggregation(
42+
[100.0, 200.0, 400.0, 1000.0, 2000.0, 4000.0]))
43+
44+
45+
def main():
46+
# Enable metrics
47+
# Set the interval in seconds in which you want to send metrics
48+
exporter = metrics_exporter.new_metrics_exporter(export_interval=5)
49+
view_manager.register_exporter(exporter)
50+
51+
view_manager.register_view(latency_view)
52+
mmap = stats_recorder.new_measurement_map()
53+
tmap = tag_map_module.TagMap()
54+
55+
for i in range(100):
56+
ms = random.random() * 5 * 1000
57+
print("Latency {0}:{1}".format(i, ms))
58+
mmap.measure_float_put(m_latency_ms, ms)
59+
mmap.record(tmap)
60+
time.sleep(1)
61+
62+
print("Done recording metrics")
63+
64+
65+
if __name__ == "__main__":
66+
main()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2019, 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+
17+
from opencensus.ext.azure import metrics_exporter
18+
from opencensus.stats import aggregation as aggregation_module
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_map as tag_map_module
23+
24+
stats = stats_module.stats
25+
view_manager = stats.view_manager
26+
stats_recorder = stats.stats_recorder
27+
28+
CARROTS_MEASURE = measure_module.MeasureInt("carrots",
29+
"number of carrots",
30+
"carrots")
31+
CARROTS_VIEW = view_module.View("carrots_view",
32+
"number of carrots",
33+
[],
34+
CARROTS_MEASURE,
35+
aggregation_module.CountAggregation())
36+
37+
38+
def main():
39+
# Enable metrics
40+
# Set the interval in seconds in which you want to send metrics
41+
exporter = metrics_exporter.new_metrics_exporter(export_interval=2)
42+
view_manager.register_exporter(exporter)
43+
44+
view_manager.register_view(CARROTS_VIEW)
45+
mmap = stats_recorder.new_measurement_map()
46+
tmap = tag_map_module.TagMap()
47+
48+
mmap.measure_int_put(CARROTS_MEASURE, 1000)
49+
mmap.record(tmap)
50+
time.sleep(10)
51+
52+
print("Done recording metrics")
53+
54+
55+
if __name__ == "__main__":
56+
main()

0 commit comments

Comments
 (0)