Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support client_Ip population for customEvents #39923

Merged
merged 5 commits into from
Mar 3, 2025
Merged
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
Expand Up @@ -6,6 +6,8 @@

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

### Breaking Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
BaseExporter,
ExportResult,
)
from azure.monitor.opentelemetry.exporter.export.trace import _utils as trace_utils
from azure.monitor.opentelemetry.exporter._constants import (
_APPLICATION_INSIGHTS_EVENT_MARKER_ATTRIBUTE,
_MICROSOFT_CUSTOM_EVENT_NAME,
Expand Down Expand Up @@ -125,6 +126,10 @@ def _convert_log_to_envelope(log_data: LogData) -> TelemetryItem:
envelope.tags[ContextTagKeys.AI_OPERATION_PARENT_ID] = "{:016x}".format( # type: ignore
log_record.span_id or _DEFAULT_SPAN_ID
)
# Special use case: Customers want to be able to set location ip on log records
location_ip = trace_utils._get_location_ip(log_record.attributes)
if location_ip:
envelope.tags[ContextTagKeys.AI_LOCATION_IP] = location_ip # type: ignore
properties = _utils._filter_custom_properties(
log_record.attributes, lambda key, val: not _is_ignored_attribute(key)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
logger_provider = LoggerProvider()
set_logger_provider(logger_provider)
exporter = AzureMonitorLogExporter.from_connection_string(os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"])
get_logger_provider().add_log_record_processor(BatchLogRecordProcessor(exporter, schedule_delay_millis=60000))
get_logger_provider().add_log_record_processor(BatchLogRecordProcessor(exporter, schedule_delay_millis=5000))

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

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

input()
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def setUpClass(cls):
attributes={
"event_key": "event_attribute",
_MICROSOFT_CUSTOM_EVENT_NAME: "event_name",
"client.address": "192.168.1.1",
},
),
InstrumentationScope("test_name"),
Expand Down Expand Up @@ -538,6 +539,7 @@ def test_log_to_envelope_custom_event(self):
envelope = exporter._log_to_envelope(self._log_data_custom_event)
record = self._log_data_custom_event.log_record
self.assertEqual(envelope.name, "Microsoft.ApplicationInsights.Event")
self.assertEqual(envelope.tags["ai.location.ip"], "192.168.1.1")
self.assertEqual(envelope.time, ns_to_iso_str(record.timestamp))
self.assertEqual(envelope.data.base_type, "EventData")
self.assertEqual(envelope.data.base_data.name, "event_name")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""
An example to show an application using Opentelemetry logging sdk. Logging calls to the standard Python
logging library are tracked and telemetry is exported to application insights with the AzureMonitorLogExporter.
"""
# mypy: disable-error-code="attr-defined"

from logging import getLogger

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace

configure_azure_monitor(
logger_name=__name__,
)

logger = getLogger(__name__)

# You can send `customEvent`` telemetry using a special `microsoft` attribute key through logging
# The name of the `customEvent` will correspond to the value of the attribute`
logger.info("Hello World!", extra={"microsoft.custom_event.name": "test-event-name", "additional_attrs": "val1"})

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

input()