Skip to content

Commit 8101cd5

Browse files
lzchenallenkim0129
authored andcommitted
Support client_Ip population for customEvents (Azure#39923)
1 parent 6927031 commit 8101cd5

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
- Support sending `customEvent` telemetry through special `microsoft` marker
88
([#39886](https://github.com/Azure/azure-sdk-for-python/pull/39886))
9+
- Populate `client_Ip` on `customEvent` telemetry
10+
([#39923](https://github.com/Azure/azure-sdk-for-python/pull/39923))
911

1012
### Breaking Changes
1113

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/logs/_exporter.py

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
BaseExporter,
3333
ExportResult,
3434
)
35+
from azure.monitor.opentelemetry.exporter.export.trace import _utils as trace_utils
3536
from azure.monitor.opentelemetry.exporter._constants import (
3637
_APPLICATION_INSIGHTS_EVENT_MARKER_ATTRIBUTE,
3738
_MICROSOFT_CUSTOM_EVENT_NAME,
@@ -125,6 +126,10 @@ def _convert_log_to_envelope(log_data: LogData) -> TelemetryItem:
125126
envelope.tags[ContextTagKeys.AI_OPERATION_PARENT_ID] = "{:016x}".format( # type: ignore
126127
log_record.span_id or _DEFAULT_SPAN_ID
127128
)
129+
# Special use case: Customers want to be able to set location ip on log records
130+
location_ip = trace_utils._get_location_ip(log_record.attributes)
131+
if location_ip:
132+
envelope.tags[ContextTagKeys.AI_LOCATION_IP] = location_ip # type: ignore
128133
properties = _utils._filter_custom_properties(
129134
log_record.attributes, lambda key, val: not _is_ignored_attribute(key)
130135
)

sdk/monitor/azure-monitor-opentelemetry-exporter/samples/logs/sample_custom_event.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
logger_provider = LoggerProvider()
2424
set_logger_provider(logger_provider)
2525
exporter = AzureMonitorLogExporter.from_connection_string(os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"])
26-
get_logger_provider().add_log_record_processor(BatchLogRecordProcessor(exporter, schedule_delay_millis=60000))
26+
get_logger_provider().add_log_record_processor(BatchLogRecordProcessor(exporter, schedule_delay_millis=5000))
2727

2828
# Attach LoggingHandler to namespaced logger
2929
handler = LoggingHandler()
@@ -35,4 +35,7 @@
3535
# The name of the `customEvent` will correspond to the value of the attribute`
3636
logger.info("Hello World!", extra={"microsoft.custom_event.name": "test-event-name", "additional_attrs": "val1"})
3737

38+
# You can also populate fields like client_Ip with attribute `client.address`
39+
logger.info("This entry will have a custom client_Ip", extra={"microsoft.custom_event.name": "test_event", "client.address": "192.168.1.1"})
40+
3841
input()

sdk/monitor/azure-monitor-opentelemetry-exporter/tests/logs/test_logs.py

+2
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ def setUpClass(cls):
195195
attributes={
196196
"event_key": "event_attribute",
197197
_MICROSOFT_CUSTOM_EVENT_NAME: "event_name",
198+
"client.address": "192.168.1.1",
198199
},
199200
),
200201
InstrumentationScope("test_name"),
@@ -538,6 +539,7 @@ def test_log_to_envelope_custom_event(self):
538539
envelope = exporter._log_to_envelope(self._log_data_custom_event)
539540
record = self._log_data_custom_event.log_record
540541
self.assertEqual(envelope.name, "Microsoft.ApplicationInsights.Event")
542+
self.assertEqual(envelope.tags["ai.location.ip"], "192.168.1.1")
541543
self.assertEqual(envelope.time, ns_to_iso_str(record.timestamp))
542544
self.assertEqual(envelope.data.base_type, "EventData")
543545
self.assertEqual(envelope.data.base_data.name, "event_name")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
"""
4+
An example to show an application using Opentelemetry logging sdk. Logging calls to the standard Python
5+
logging library are tracked and telemetry is exported to application insights with the AzureMonitorLogExporter.
6+
"""
7+
# mypy: disable-error-code="attr-defined"
8+
9+
from logging import getLogger
10+
11+
from azure.monitor.opentelemetry import configure_azure_monitor
12+
from opentelemetry import trace
13+
14+
configure_azure_monitor(
15+
logger_name=__name__,
16+
)
17+
18+
logger = getLogger(__name__)
19+
20+
# You can send `customEvent`` telemetry using a special `microsoft` attribute key through logging
21+
# The name of the `customEvent` will correspond to the value of the attribute`
22+
logger.info("Hello World!", extra={"microsoft.custom_event.name": "test-event-name", "additional_attrs": "val1"})
23+
24+
# You can also populate fields like client_Ip with attribute `client.address`
25+
logger.info("This entry will have a custom client_Ip", extra={"microsoft.custom_event.name": "test_event", "client.address": "192.168.1.1"})
26+
27+
input()

0 commit comments

Comments
 (0)