-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for exporting Custom Events to Azure App Insights.
Enable exporter for custom events using the same logging pipeline available in OC.
- Loading branch information
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
contrib/opencensus-ext-azure/opencensus/ext/azure/events_exporter/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
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" | ||
data = Event( | ||
name=record.msg, | ||
properties=record.args, | ||
measurements=None, | ||
) | ||
envelope.data = Data(baseData=data, baseType='EventData') | ||
return envelope |
69 changes: 69 additions & 0 deletions
69
contrib/opencensus-ext-azure/tests/test_azure_event_exporter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |