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

Commit 72f2363

Browse files
authored
Release for v0.7.7 (#853)
1 parent 9ba948f commit 72f2363

File tree

25 files changed

+915
-554
lines changed

25 files changed

+915
-554
lines changed

CHANGELOG.md

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

33
## Unreleased
44

5+
## 0.7.7
6+
Released 2020-02-03
7+
- Update `azure` module
8+
([#837](https://github.com/census-instrumentation/opencensus-python/pull/837),
9+
[#845](https://github.com/census-instrumentation/opencensus-python/pull/845),
10+
[#848](https://github.com/census-instrumentation/opencensus-python/pull/848),
11+
[#851](https://github.com/census-instrumentation/opencensus-python/pull/851))
12+
513
## 0.7.6
614
Released 2019-11-26
715

contrib/opencensus-ext-azure/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
## Unreleased
44

5+
## 1.0.2
6+
Released 2020-02-03
7+
8+
- Add local storage and retry logic for Azure Metrics Exporter
9+
([#845](https://github.com/census-instrumentation/opencensus-python/pull/845))
10+
- Add Fixed-rate sampling logic for Azure Log Exporter
11+
([#848](https://github.com/census-instrumentation/opencensus-python/pull/848))
12+
- Implement TelemetryProcessors for Azure exporters
13+
([#851](https://github.com/census-instrumentation/opencensus-python/pull/851))
14+
515
## 1.0.1
616
Released 2019-11-26
717

contrib/opencensus-ext-azure/README.rst

Lines changed: 143 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ This example shows how to send a warning level log to Azure Monitor.
3737
logger.addHandler(AzureLogHandler(connection_string='InstrumentationKey=<your-instrumentation_key-here>'))
3838
logger.warning('Hello, World!')
3939
40+
Correlation
41+
###########
4042

4143
You can enrich the logs with trace IDs and span IDs by using the `logging integration <../opencensus-ext-logging>`_.
4244

@@ -73,9 +75,12 @@ You can enrich the logs with trace IDs and span IDs by using the `logging integr
7375
logger.warning('In the span')
7476
logger.warning('After the span')
7577
76-
You can also add custom properties to your log messages in the form of key-values.
78+
Custom Properties
79+
#################
7780

78-
WARNING: For this feature to work, you need to pass a dictionary as the argument. If you pass arguments of any other type, the logger will ignore them. The solution is to convert these arguments into a dictionary.
81+
You can also add custom properties to your log messages in the *extra* keyword argument using the custom_dimensions field.
82+
83+
WARNING: For this feature to work, you need to pass a dictionary to the custom_dimensions field. If you pass arguments of any other type, the logger will ignore them.
7984

8085
.. code:: python
8186
@@ -85,7 +90,37 @@ WARNING: For this feature to work, you need to pass a dictionary as the argument
8590
8691
logger = logging.getLogger(__name__)
8792
logger.addHandler(AzureLogHandler(connection_string='InstrumentationKey=<your-instrumentation_key-here>'))
88-
logger.warning('action', {'key-1': 'value-1', 'key-2': 'value2'})
93+
94+
properties = {'custom_dimensions': {'key_1': 'value_1', 'key_2': 'value_2'}}
95+
logger.warning('action', extra=properties)
96+
97+
Modifying Logs
98+
##############
99+
100+
* You can pass a callback function to the exporter to process telemetry before it is exported.
101+
* Your callback function can return `False` if you do not want this envelope exported.
102+
* Your callback function must accept an [envelope](https://github.com/census-instrumentation/opencensus-python/blob/master/contrib/opencensus-ext-azure/opencensus/ext/azure/common/protocol.py#L86) data type as its parameter.
103+
* You can see the schema for Azure Monitor data types in the envelopes [here](https://github.com/census-instrumentation/opencensus-python/blob/master/contrib/opencensus-ext-azure/opencensus/ext/azure/common/protocol.py).
104+
* The `AzureLogHandler` handles `ExceptionData` and `MessageData` data types.
105+
106+
.. code:: python
107+
108+
import logging
109+
110+
from opencensus.ext.azure.log_exporter import AzureLogHandler
111+
112+
logger = logging.getLogger(__name__)
113+
114+
# Callback function to append '_hello' to each log message telemetry
115+
def callback_function(envelope):
116+
envelope.data.baseData.message += '_hello'
117+
return True
118+
119+
handler = AzureLogHandler(connection_string='InstrumentationKey=<your-instrumentation_key-here>')
120+
handler.add_telemetry_processor(callback_function)
121+
logger.addHandler(handler)
122+
logger.warning('Hello, World!')
123+
89124
90125
Metrics
91126
~~~~~~~
@@ -143,6 +178,9 @@ The **Azure Monitor Metrics Exporter** allows you to export metrics to `Azure Mo
143178
if __name__ == "__main__":
144179
main()
145180
181+
Standard Metrics
182+
################
183+
146184
The exporter also includes a set of standard metrics that are exported to Azure Monitor by default.
147185

148186
.. code:: python
@@ -177,6 +215,67 @@ Below is a list of standard metrics that are currently available:
177215
- Process CPU Usage (percentage)
178216
- Process Private Bytes (bytes)
179217

218+
Modifying Metrics
219+
#################
220+
221+
* You can pass a callback function to the exporter to process telemetry before it is exported.
222+
* Your callback function can return `False` if you do not want this envelope exported.
223+
* Your callback function must accept an [envelope](https://github.com/census-instrumentation/opencensus-python/blob/master/contrib/opencensus-ext-azure/opencensus/ext/azure/common/protocol.py#L86) data type as its parameter.
224+
* You can see the schema for Azure Monitor data types in the envelopes [here](https://github.com/census-instrumentation/opencensus-python/blob/master/contrib/opencensus-ext-azure/opencensus/ext/azure/common/protocol.py).
225+
* The `MetricsExporter` handles `MetricData` data types.
226+
227+
.. code:: python
228+
229+
import time
230+
231+
from opencensus.ext.azure import metrics_exporter
232+
from opencensus.stats import aggregation as aggregation_module
233+
from opencensus.stats import measure as measure_module
234+
from opencensus.stats import stats as stats_module
235+
from opencensus.stats import view as view_module
236+
from opencensus.tags import tag_map as tag_map_module
237+
238+
stats = stats_module.stats
239+
view_manager = stats.view_manager
240+
stats_recorder = stats.stats_recorder
241+
242+
CARROTS_MEASURE = measure_module.MeasureInt("carrots",
243+
"number of carrots",
244+
"carrots")
245+
CARROTS_VIEW = view_module.View("carrots_view",
246+
"number of carrots",
247+
[],
248+
CARROTS_MEASURE,
249+
aggregation_module.CountAggregation())
250+
251+
# Callback function to only export the metric if value is greater than 0
252+
def callback_function(envelope):
253+
return envelope.data.baseData.metrics[0].value > 0
254+
255+
def main():
256+
# Enable metrics
257+
# Set the interval in seconds in which you want to send metrics
258+
exporter = metrics_exporter.new_metrics_exporter(connection_string='InstrumentationKey=<your-instrumentation-key-here>')
259+
exporter.add_telemetry_processor(callback_function)
260+
view_manager.register_exporter(exporter)
261+
262+
view_manager.register_view(CARROTS_VIEW)
263+
mmap = stats_recorder.new_measurement_map()
264+
tmap = tag_map_module.TagMap()
265+
266+
mmap.measure_int_put(CARROTS_MEASURE, 1000)
267+
mmap.record(tmap)
268+
# Default export interval is every 15.0s
269+
# Your application should run for at least this amount
270+
# of time so the exporter will meet this interval
271+
# Sleep can fulfill this
272+
time.sleep(60)
273+
274+
print("Done recording metrics")
275+
276+
if __name__ == "__main__":
277+
main()
278+
180279
Trace
181280
~~~~~
182281

@@ -195,16 +294,21 @@ This example shows how to send a span "hello" to Azure Monitor.
195294
from opencensus.trace.tracer import Tracer
196295
197296
tracer = Tracer(
198-
exporter=AzureExporter(connection_string='InstrumentationKey=<your-instrumentation-key-here>'),
297+
exporter=AzureExporter(
298+
connection_string='InstrumentationKey=<your-instrumentation-key-here>'
299+
),
199300
sampler=ProbabilitySampler(1.0)
200301
)
201302
202303
with tracer.span(name='hello'):
203304
print('Hello, World!')
204305
205-
OpenCensus also supports several [integrations](https://github.com/census-instrumentation/opencensus-python#integration) which allows OpenCensus to integrate with third party libraries.
306+
Integrations
307+
############
308+
309+
OpenCensus also supports several `integrations <https://github.com/census-instrumentation/opencensus-python#integration>`_ which allows OpenCensus to integrate with third party libraries.
206310

207-
This example shows how to integrate with the [requests](https://2.python-requests.org/en/master/) library.
311+
This example shows how to integrate with the `requests <https://2.python-requests.org/en/master/>`_ library.
208312

209313
* 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>`_.
210314
* Install the `requests integration package <../opencensus-ext-requests>`_ using ``pip install opencensus-ext-requests``.
@@ -223,14 +327,45 @@ This example shows how to integrate with the [requests](https://2.python-request
223327
config_integration.trace_integrations(['requests'])
224328
tracer = Tracer(
225329
exporter=AzureExporter(
226-
# TODO: replace this with your own instrumentation key.
227-
instrumentation_key='00000000-0000-0000-0000-000000000000',
330+
connection_string='InstrumentationKey=<your-instrumentation-key-here>',
228331
),
229332
sampler=ProbabilitySampler(1.0),
230333
)
231334
with tracer.span(name='parent'):
232335
response = requests.get(url='https://www.wikipedia.org/wiki/Rabbit')
233336
337+
Modifying Traces
338+
################
339+
340+
* You can pass a callback function to the exporter to process telemetry before it is exported.
341+
* Your callback function can return `False` if you do not want this envelope exported.
342+
* Your callback function must accept an [envelope](https://github.com/census-instrumentation/opencensus-python/blob/master/contrib/opencensus-ext-azure/opencensus/ext/azure/common/protocol.py#L86) data type as its parameter.
343+
* You can see the schema for Azure Monitor data types in the envelopes [here](https://github.com/census-instrumentation/opencensus-python/blob/master/contrib/opencensus-ext-azure/opencensus/ext/azure/common/protocol.py).
344+
* The `AzureExporter` handles `Data` data types.
345+
346+
.. code:: python
347+
348+
import requests
349+
350+
from opencensus.ext.azure.trace_exporter import AzureExporter
351+
from opencensus.trace import config_integration
352+
from opencensus.trace.samplers import ProbabilitySampler
353+
from opencensus.trace.tracer import Tracer
354+
355+
config_integration.trace_integrations(['requests'])
356+
357+
# Callback function to add os_type: linux to span properties
358+
def callback_function(envelope):
359+
envelope.data.baseData.properties['os_type'] = 'linux'
360+
return True
361+
362+
exporter = AzureExporter(
363+
connection_string='InstrumentationKey=<your-instrumentation-key-here>'
364+
)
365+
exporter.add_telemetry_processor(callback_function)
366+
tracer = Tracer(exporter=exporter, sampler=ProbabilitySampler(1.0))
367+
with tracer.span(name='parent'):
368+
response = requests.get(url='https://www.wikipedia.org/wiki/Rabbit')
234369
235370
References
236371
----------

contrib/opencensus-ext-azure/examples/logs/properties.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,14 @@
2121
# and place it in the APPLICATIONINSIGHTS_CONNECTION_STRING
2222
# environment variable.
2323
logger.addHandler(AzureLogHandler())
24-
logger.warning('action', {'key-1': 'value-1', 'key-2': 'value2'})
24+
25+
properties = {'custom_dimensions': {'key_1': 'value_1', 'key_2': 'value_2'}}
26+
27+
# Use properties in logging statements
28+
logger.warning('action', extra=properties)
29+
30+
# Use properties in exception logs
31+
try:
32+
result = 1 / 0 # generate a ZeroDivisionError
33+
except Exception:
34+
logger.exception('Captured an exception.', extra=properties)

contrib/opencensus-ext-azure/opencensus/ext/azure/common/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def __init__(self, *args, **kwargs):
9292
export_interval=15.0,
9393
grace_period=5.0,
9494
instrumentation_key=None,
95+
logging_sampling_rate=1.0,
9596
max_batch_size=100,
9697
minimum_retry_interval=60, # minimum retry interval in seconds
9798
proxy=None,
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 logging
16+
17+
logger = logging.getLogger(__name__)
18+
19+
20+
class ProcessorMixin(object):
21+
"""ProcessorMixin adds the ability to process telemetry processors
22+
23+
Telemetry processors are functions that are called before exporting of
24+
telemetry to possibly modify the envelope contents.
25+
"""
26+
27+
def add_telemetry_processor(self, processor):
28+
"""Adds telemetry processor to the collection. Telemetry processors
29+
will be called one by one before telemetry item is pushed for sending
30+
and in the order they were added.
31+
32+
:param processor: The processor to add.
33+
"""
34+
self._telemetry_processors.append(processor)
35+
36+
def clear_telemetry_processors(self):
37+
"""Removes all telemetry processors"""
38+
self._telemetry_processors = []
39+
40+
def apply_telemetry_processors(self, envelopes):
41+
"""Applies all telemetry processors in the order they were added.
42+
43+
This function will return the list of envelopes to be exported after
44+
each processor has been run sequentially. Individual processors can
45+
throw exceptions and fail, but the applying of all telemetry processors
46+
will proceed (not fast fail). Processors also return True if envelope
47+
should be included for exporting, False otherwise.
48+
49+
:param envelopes: The envelopes to apply each processor to.
50+
"""
51+
filtered_envelopes = []
52+
for envelope in envelopes:
53+
accepted = True
54+
for processor in self._telemetry_processors:
55+
try:
56+
if processor(envelope) is False:
57+
accepted = False
58+
break
59+
except Exception as ex:
60+
logger.warning('Telemetry processor failed with: %s.', ex)
61+
if accepted:
62+
filtered_envelopes.append(envelope)
63+
return filtered_envelopes

contrib/opencensus-ext-azure/opencensus/ext/azure/common/utils.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@
2323
from opencensus.common.version import __version__ as opencensus_version
2424
from opencensus.ext.azure.common.version import __version__ as ext_version
2525

26-
try:
27-
from urllib.parse import urlparse
28-
except ImportError:
29-
from urlparse import urlparse
30-
31-
3226
azure_monitor_context = {
3327
'ai.cloud.role': os.path.basename(sys.argv[0]) or 'Python Application',
3428
'ai.cloud.roleInstance': platform.node(),
@@ -64,10 +58,6 @@ def timestamp_to_iso_str(timestamp):
6458
return to_iso_str(datetime.datetime.utcfromtimestamp(timestamp))
6559

6660

67-
def url_to_dependency_name(url):
68-
return urlparse(url).netloc
69-
70-
7161
# Validate UUID format
7262
# Specs taken from https://tools.ietf.org/html/rfc4122
7363
uuid_regex_pattern = re.compile('^[0-9a-f]{8}-'

contrib/opencensus-ext-azure/opencensus/ext/azure/common/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
__version__ = '1.0.1'
15+
__version__ = '1.0.2'

0 commit comments

Comments
 (0)