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

Commit c16e324

Browse files
authored
Azure: Allow specifying metrics for custom events (#1117)
1 parent 79b5021 commit c16e324

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

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+
- Allow specifying metrics (custom_measurements) for Azure custom events
5+
([#1117](https://github.com/census-instrumentation/opencensus-python/pull/1117))
46

57
# 0.9.0
68
Released 2022-04-20

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,16 @@ def log_record_to_envelope(self, record):
255255
isinstance(record.custom_dimensions, dict)):
256256
properties.update(record.custom_dimensions)
257257

258+
measurements = {}
259+
if (hasattr(record, 'custom_measurements') and
260+
isinstance(record.custom_measurements, dict)):
261+
measurements.update(record.custom_measurements)
262+
258263
envelope.name = 'Microsoft.ApplicationInsights.Event'
259264
data = Event(
260265
name=self.format(record),
261266
properties=properties,
267+
measurements=measurements,
262268
)
263269
envelope.data = Data(baseData=data, baseType='EventData')
264270

contrib/opencensus-ext-azure/tests/test_azure_log_exporter.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,11 @@ def test_exception_with_custom_properties(self, requests_mock):
373373
{
374374
'key_1': 'value_1',
375375
'key_2': 'value_2'
376+
},
377+
'custom_measurements':
378+
{
379+
'measure_1': 1,
380+
'measure_2': 2
376381
}
377382
}
378383
logger.exception('Captured an exception.', extra=properties)
@@ -382,6 +387,8 @@ def test_exception_with_custom_properties(self, requests_mock):
382387
self.assertTrue('ZeroDivisionError' in post_body)
383388
self.assertTrue('key_1' in post_body)
384389
self.assertTrue('key_2' in post_body)
390+
self.assertTrue('measure_1' in post_body)
391+
self.assertTrue('measure_2' in post_body)
385392

386393
@mock.patch('requests.post', return_value=mock.Mock())
387394
def test_export_empty(self, request_mock):
@@ -436,13 +443,20 @@ def test_log_record_with_custom_properties(self, requests_mock):
436443
{
437444
'key_1': 'value_1',
438445
'key_2': 'value_2'
446+
},
447+
'custom_measurements':
448+
{
449+
'measure_1': 1,
450+
'measure_2': 2
439451
}
440452
})
441453
handler.close()
442454
post_body = requests_mock.call_args_list[0][1]['data']
443455
self.assertTrue('action' in post_body)
444456
self.assertTrue('key_1' in post_body)
445457
self.assertTrue('key_2' in post_body)
458+
self.assertTrue('measure_1' in post_body)
459+
self.assertTrue('measure_2' in post_body)
446460

447461
@mock.patch('requests.post', return_value=mock.Mock())
448462
def test_log_with_invalid_custom_properties(self, requests_mock):
@@ -454,7 +468,8 @@ def test_log_with_invalid_custom_properties(self, requests_mock):
454468
logger.addHandler(handler)
455469
logger.warning('action_1_%s', None)
456470
logger.warning('action_2_%s', 'arg', extra={
457-
'custom_dimensions': 'not_a_dict'
471+
'custom_dimensions': 'not_a_dict',
472+
'custom_measurements': 'also_not'
458473
})
459474
logger.warning('action_3_%s', 'arg', extra={
460475
'notcustom_dimensions': {'key_1': 'value_1'}
@@ -468,6 +483,7 @@ def test_log_with_invalid_custom_properties(self, requests_mock):
468483
self.assertTrue('action_3_arg' in post_body)
469484

470485
self.assertFalse('not_a_dict' in post_body)
486+
self.assertFalse('also_not' in post_body)
471487
self.assertFalse('key_1' in post_body)
472488

473489
@mock.patch('requests.post', return_value=mock.Mock())

0 commit comments

Comments
 (0)