-
Notifications
You must be signed in to change notification settings - Fork 247
Add support for exporting Custom Events to Azure App Insights. #814
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| from opencensus.ext.azure.common import utils | ||
| from opencensus.ext.azure.common.protocol import Data, Envelope, Event | ||
| from opencensus.ext.azure.log_exporter import AzureLogHandler | ||
|
|
||
|
|
||
| class AzureEventHandler(AzureLogHandler): | ||
| def __init__(self, **options): | ||
| super(AzureEventHandler, self).__init__(**options) | ||
|
|
||
| def log_record_to_envelope(self, record): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think adding an example of how this works in |
||
| envelope = Envelope( | ||
| iKey=self.options.instrumentation_key, | ||
| tags=dict(utils.azure_monitor_context), | ||
| time=utils.timestamp_to_iso_str(record.created), | ||
| ) | ||
|
|
||
| envelope.name = "Microsoft.ApplicationInsights.Event" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we decided to use Logs as the actual telemetry type? This would be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But wouldn't that log the data as "searchable text" in the logs table (https://github.com/microsoft/ApplicationInsights-Home/blob/master/EndpointSpecs/Schemas/Bond/MessageData.bond#L6)? The event definition is different (and this link might be outdated) and more resonant to what we are trying to accomplish here (custom events): https://github.com/microsoft/ApplicationInsights-Home/blob/master/EndpointSpecs/Schemas/Bond/EventData.bond#L5 Maybe I'm missing something, please let me know. |
||
| data = Event( | ||
| name=record.msg, | ||
| properties=record.args, | ||
| measurements=None, | ||
| ) | ||
| envelope.data = Data(baseData=data, baseType='EventData') | ||
| return envelope | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import logging | ||
| import os | ||
| import shutil | ||
| import unittest | ||
|
|
||
| import mock | ||
|
|
||
| from opencensus.ext.azure import events_exporter | ||
|
|
||
| TEST_FOLDER = os.path.abspath('.test.logs') | ||
|
|
||
|
|
||
| def setUpModule(): | ||
| os.makedirs(TEST_FOLDER) | ||
|
|
||
|
|
||
| def tearDownModule(): | ||
| shutil.rmtree(TEST_FOLDER) | ||
|
|
||
|
|
||
| class TestAzureEventHandler(unittest.TestCase): | ||
| def test_log_record_to_envelope(self): | ||
| handler = events_exporter.AzureEventHandler( | ||
| instrumentation_key='12345678-1234-5678-abcd-12345678abcd', | ||
| storage_path=os.path.join(TEST_FOLDER, self.id()), | ||
| ) | ||
| envelope = handler.log_record_to_envelope(mock.MagicMock( | ||
| msg="test" | ||
| )) | ||
| self.assertEqual( | ||
| envelope.iKey, | ||
| '12345678-1234-5678-abcd-12345678abcd') | ||
|
|
||
| self.assertEqual( | ||
| envelope.name, | ||
| 'Microsoft.ApplicationInsights.Event') | ||
|
|
||
| self.assertEqual( | ||
| envelope.get("data").baseData.get("name"), | ||
| "test") | ||
| handler.close() | ||
|
|
||
| def test_log_record_to_envelope_with_properties(self): | ||
| handler = events_exporter.AzureEventHandler( | ||
| instrumentation_key='12345678-1234-5678-abcd-12345678abcd', | ||
| storage_path=os.path.join(TEST_FOLDER, self.id()), | ||
| ) | ||
| envelope = handler.log_record_to_envelope(mock.MagicMock( | ||
| msg="test", | ||
| args={"sku": "SKU-12312"} | ||
| )) | ||
| self.assertEqual( | ||
| len(envelope.get("data").baseData.get("properties")), | ||
| 1) | ||
| handler.close() | ||
|
|
||
| @mock.patch('requests.post', return_value=mock.Mock()) | ||
| def test_export(self, requests_mock): | ||
| logger = logging.getLogger(self.id()) | ||
| handler = events_exporter.AzureEventHandler( | ||
| instrumentation_key='12345678-1234-5678-abcd-12345678abcd', | ||
| storage_path=os.path.join(TEST_FOLDER, self.id()), | ||
| ) | ||
| logger.addHandler(handler) | ||
| logger.warning('test_metric') | ||
| handler.close() | ||
| self.assertEqual(len(requests_mock.call_args_list), 1) | ||
| post_body = requests_mock.call_args_list[0][1]['data'] | ||
| self.assertTrue('test_metric' in post_body) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need another handler type if we are going to be using
Messagetelemetry type. We can leverage the already existingAzureLogHandlerand place the new "properties" logic in there.