Skip to content

Commit

Permalink
Azure: Allow specifying metrics for custom events (#1117)
Browse files Browse the repository at this point in the history
  • Loading branch information
Davi-DeGanne authored Apr 21, 2022
1 parent 79b5021 commit c16e324
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## Unreleased
- Allow specifying metrics (custom_measurements) for Azure custom events
([#1117](https://github.com/census-instrumentation/opencensus-python/pull/1117))

# 0.9.0
Released 2022-04-20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,16 @@ def log_record_to_envelope(self, record):
isinstance(record.custom_dimensions, dict)):
properties.update(record.custom_dimensions)

measurements = {}
if (hasattr(record, 'custom_measurements') and
isinstance(record.custom_measurements, dict)):
measurements.update(record.custom_measurements)

envelope.name = 'Microsoft.ApplicationInsights.Event'
data = Event(
name=self.format(record),
properties=properties,
measurements=measurements,
)
envelope.data = Data(baseData=data, baseType='EventData')

Expand Down
18 changes: 17 additions & 1 deletion contrib/opencensus-ext-azure/tests/test_azure_log_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ def test_exception_with_custom_properties(self, requests_mock):
{
'key_1': 'value_1',
'key_2': 'value_2'
},
'custom_measurements':
{
'measure_1': 1,
'measure_2': 2
}
}
logger.exception('Captured an exception.', extra=properties)
Expand All @@ -382,6 +387,8 @@ def test_exception_with_custom_properties(self, requests_mock):
self.assertTrue('ZeroDivisionError' in post_body)
self.assertTrue('key_1' in post_body)
self.assertTrue('key_2' in post_body)
self.assertTrue('measure_1' in post_body)
self.assertTrue('measure_2' in post_body)

@mock.patch('requests.post', return_value=mock.Mock())
def test_export_empty(self, request_mock):
Expand Down Expand Up @@ -436,13 +443,20 @@ def test_log_record_with_custom_properties(self, requests_mock):
{
'key_1': 'value_1',
'key_2': 'value_2'
},
'custom_measurements':
{
'measure_1': 1,
'measure_2': 2
}
})
handler.close()
post_body = requests_mock.call_args_list[0][1]['data']
self.assertTrue('action' in post_body)
self.assertTrue('key_1' in post_body)
self.assertTrue('key_2' in post_body)
self.assertTrue('measure_1' in post_body)
self.assertTrue('measure_2' in post_body)

@mock.patch('requests.post', return_value=mock.Mock())
def test_log_with_invalid_custom_properties(self, requests_mock):
Expand All @@ -454,7 +468,8 @@ def test_log_with_invalid_custom_properties(self, requests_mock):
logger.addHandler(handler)
logger.warning('action_1_%s', None)
logger.warning('action_2_%s', 'arg', extra={
'custom_dimensions': 'not_a_dict'
'custom_dimensions': 'not_a_dict',
'custom_measurements': 'also_not'
})
logger.warning('action_3_%s', 'arg', extra={
'notcustom_dimensions': {'key_1': 'value_1'}
Expand All @@ -468,6 +483,7 @@ def test_log_with_invalid_custom_properties(self, requests_mock):
self.assertTrue('action_3_arg' in post_body)

self.assertFalse('not_a_dict' in post_body)
self.assertFalse('also_not' in post_body)
self.assertFalse('key_1' in post_body)

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

0 comments on commit c16e324

Please sign in to comment.