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 optional custom properties to logging messages. #822

Merged
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
14 changes: 14 additions & 0 deletions contrib/opencensus-ext-azure/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ 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.

ivaramme marked this conversation as resolved.
Show resolved Hide resolved
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.

.. code:: python

import logging

from opencensus.ext.azure.log_exporter import AzureLogHandler

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

Metrics
~~~~~~~

Expand Down
24 changes: 24 additions & 0 deletions contrib/opencensus-ext-azure/examples/logs/properties.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2019, OpenCensus Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
ivaramme marked this conversation as resolved.
Show resolved Hide resolved

from opencensus.ext.azure.log_exporter import AzureLogHandler

logger = logging.getLogger(__name__)
# TODO: you need to specify the instrumentation key in a connection string
# and place it in the APPLICATIONINSIGHTS_CONNECTION_STRING
# environment variable.
logger.addHandler(AzureLogHandler())
logger.warning('action', {'key-1': 'value-1', 'key-2': 'value2'})
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ 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
31 changes: 31 additions & 0 deletions contrib/opencensus-ext-azure/tests/test_azure_log_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,34 @@ def test_log_record_to_envelope(self):
envelope.iKey,
'12345678-1234-5678-abcd-12345678abcd')
handler.close()

@mock.patch('requests.post', return_value=mock.Mock())
def test_log_record_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)
logger.warning('action', {'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)

@mock.patch('requests.post', return_value=mock.Mock())
def test_log_with_invalid_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)
logger.warning('action_1_%s', None)
logger.warning('action_2_%s', 'not_a_dict')
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)