Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom_dimensions using kwargs instead of args #837

Merged
merged 6 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions contrib/opencensus-ext-azure/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ You can enrich the logs with trace IDs and span IDs by using the `logging integr
logger.warning('In the span')
logger.warning('After the span')

You can also add custom properties to your log messages in the form of key-values.
You can also add custom properties to your log messages in the *extra* keyword argument using the custom_dimensions field.

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.
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.

.. code:: python

Expand All @@ -85,7 +85,9 @@ WARNING: For this feature to work, you need to pass a dictionary as the argument

logger = logging.getLogger(__name__)
logger.addHandler(AzureLogHandler(connection_string='InstrumentationKey=<your-instrumentation_key-here>'))
logger.warning('action', {'key-1': 'value-1', 'key-2': 'value2'})

properties = {'custom_dimensions': {'key_1': 'value_1', 'key_2': 'value_2'}}
logger.warning('action', extra=properties)

Metrics
~~~~~~~
Expand Down
12 changes: 11 additions & 1 deletion contrib/opencensus-ext-azure/examples/logs/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@
# and place it in the APPLICATIONINSIGHTS_CONNECTION_STRING
# environment variable.
logger.addHandler(AzureLogHandler())
logger.warning('action', {'key-1': 'value-1', 'key-2': 'value2'})

properties = {'custom_dimensions': {'key_1': 'value_1', 'key_2': 'value_2'}}

# Use properties in logging statements
logger.warning('action', extra=properties)

# Use properties in exception logs
try:
result = 1 / 0 # generate a ZeroDivisionError
except Exception:
logger.exception('Captured an exception.', extra=properties)
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def log_record_to_envelope(self, record):
tags=dict(utils.azure_monitor_context),
time=utils.timestamp_to_iso_str(record.created),
)

envelope.tags['ai.operation.id'] = getattr(
record,
'traceId',
Expand All @@ -169,6 +170,11 @@ def log_record_to_envelope(self, record):
'lineNumber': record.lineno,
'level': record.levelname,
}

if (hasattr(record, 'custom_dimensions') and
isinstance(record.custom_dimensions, dict)):
properties.update(record.custom_dimensions)

if record.exc_info:
exctype, _value, tb = record.exc_info
callstack = []
Expand Down Expand Up @@ -198,8 +204,6 @@ def log_record_to_envelope(self, record):
)
envelope.data = Data(baseData=data, baseType='ExceptionData')
else:
if isinstance(record.args, dict):
properties.update(record.args)
envelope.name = 'Microsoft.ApplicationInsights.Message'
data = Message(
message=self.format(record),
Expand Down
54 changes: 48 additions & 6 deletions contrib/opencensus-ext-azure/tests/test_azure_log_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,32 @@ def test_exception(self, requests_mock):
post_body = requests_mock.call_args_list[0][1]['data']
self.assertTrue('ZeroDivisionError' in post_body)

@mock.patch('requests.post', return_value=mock.Mock())
def test_exception_with_custom_properties(self, requests_mock):
logger = logging.getLogger(self.id())
handler = log_exporter.AzureLogHandler(
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
storage_path=os.path.join(TEST_FOLDER, self.id()),
)
logger.addHandler(handler)
try:
return 1 / 0 # generate a ZeroDivisionError
except Exception:
properties = {
'custom_dimensions':
{
'key_1': 'value_1',
'key_2': 'value_2'
}
}
logger.exception('Captured an exception.', extra=properties)
handler.close()
self.assertEqual(len(requests_mock.call_args_list), 1)
post_body = requests_mock.call_args_list[0][1]['data']
self.assertTrue('ZeroDivisionError' in post_body)
self.assertTrue('key_1' in post_body)
self.assertTrue('key_2' in post_body)

@mock.patch('requests.post', return_value=mock.Mock())
def test_export_empty(self, request_mock):
handler = log_exporter.AzureLogHandler(
Expand Down Expand Up @@ -143,12 +169,18 @@ def test_log_record_with_custom_properties(self, requests_mock):
storage_path=os.path.join(TEST_FOLDER, self.id()),
)
logger.addHandler(handler)
logger.warning('action', {'key-1': 'value-1', 'key-2': 'value-2'})
logger.warning('action', extra={
'custom_dimensions':
{
'key_1': 'value_1',
'key_2': 'value_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('key_1' in post_body)
self.assertTrue('key_2' in post_body)

@mock.patch('requests.post', return_value=mock.Mock())
def test_log_with_invalid_custom_properties(self, requests_mock):
Expand All @@ -159,9 +191,19 @@ def test_log_with_invalid_custom_properties(self, requests_mock):
)
logger.addHandler(handler)
logger.warning('action_1_%s', None)
logger.warning('action_2_%s', 'not_a_dict')
logger.warning('action_2_%s', 'arg', extra={
'custom_dimensions': 'not_a_dict'
})
logger.warning('action_3_%s', 'arg', extra={
'notcustom_dimensions': {'key_1': 'value_1'}
})

handler.close()
self.assertEqual(len(os.listdir(handler.storage.path)), 0)
post_body = requests_mock.call_args_list[0][1]['data']
self.assertTrue('action_1' in post_body)
self.assertTrue('action_2' in post_body)
self.assertTrue('action_1_' in post_body)
self.assertTrue('action_2_arg' in post_body)
self.assertTrue('action_3_arg' in post_body)

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