Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.
Closed
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
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):
Copy link
Copy Markdown
Contributor

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 Message telemetry type. We can leverage the already existing AzureLogHandler and place the new "properties" logic in there.

def __init__(self, **options):
super(AzureEventHandler, self).__init__(**options)

def log_record_to_envelope(self, record):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding an example of how this works in examples.logs will help. As well, updating the example in the README and adding an excerpt of this feature will be good.

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"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 Message data.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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
69 changes: 69 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,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)