Skip to content

Commit

Permalink
Add support for exporting Custom Events to Azure App Insights.
Browse files Browse the repository at this point in the history
Enable exporter for custom  events using the same logging pipeline available in OC.
  • Loading branch information
ivaramme committed Nov 12, 2019
1 parent e9129b7 commit 8ba8125
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from opencensus.ext.azure.log_exporter import AzureLogHandler
from opencensus.ext.azure.common.protocol import Event
from opencensus.ext.azure.common import utils
from opencensus.ext.azure.common.protocol import Data
from opencensus.ext.azure.common.protocol import Envelope

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
66 changes: 66 additions & 0 deletions contrib/opencensus-ext-azure/tests/test_azure_event_exporter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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)

0 comments on commit 8ba8125

Please sign in to comment.