File tree 5 files changed +40
-1
lines changed
azure-monitor-opentelemetry/samples/logging
azure-monitor-opentelemetry-exporter
azure/monitor/opentelemetry/exporter/export/logs
5 files changed +40
-1
lines changed Original file line number Diff line number Diff line change 6
6
7
7
- Support sending ` customEvent ` telemetry through special ` microsoft ` marker
8
8
([ #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 ) )
9
11
10
12
### Breaking Changes
11
13
Original file line number Diff line number Diff line change 32
32
BaseExporter ,
33
33
ExportResult ,
34
34
)
35
+ from azure .monitor .opentelemetry .exporter .export .trace import _utils as trace_utils
35
36
from azure .monitor .opentelemetry .exporter ._constants import (
36
37
_APPLICATION_INSIGHTS_EVENT_MARKER_ATTRIBUTE ,
37
38
_MICROSOFT_CUSTOM_EVENT_NAME ,
@@ -125,6 +126,10 @@ def _convert_log_to_envelope(log_data: LogData) -> TelemetryItem:
125
126
envelope .tags [ContextTagKeys .AI_OPERATION_PARENT_ID ] = "{:016x}" .format ( # type: ignore
126
127
log_record .span_id or _DEFAULT_SPAN_ID
127
128
)
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
128
133
properties = _utils ._filter_custom_properties (
129
134
log_record .attributes , lambda key , val : not _is_ignored_attribute (key )
130
135
)
Original file line number Diff line number Diff line change 23
23
logger_provider = LoggerProvider ()
24
24
set_logger_provider (logger_provider )
25
25
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 ))
27
27
28
28
# Attach LoggingHandler to namespaced logger
29
29
handler = LoggingHandler ()
35
35
# The name of the `customEvent` will correspond to the value of the attribute`
36
36
logger .info ("Hello World!" , extra = {"microsoft.custom_event.name" : "test-event-name" , "additional_attrs" : "val1" })
37
37
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
+
38
41
input ()
Original file line number Diff line number Diff line change @@ -195,6 +195,7 @@ def setUpClass(cls):
195
195
attributes = {
196
196
"event_key" : "event_attribute" ,
197
197
_MICROSOFT_CUSTOM_EVENT_NAME : "event_name" ,
198
+ "client.address" : "192.168.1.1" ,
198
199
},
199
200
),
200
201
InstrumentationScope ("test_name" ),
@@ -538,6 +539,7 @@ def test_log_to_envelope_custom_event(self):
538
539
envelope = exporter ._log_to_envelope (self ._log_data_custom_event )
539
540
record = self ._log_data_custom_event .log_record
540
541
self .assertEqual (envelope .name , "Microsoft.ApplicationInsights.Event" )
542
+ self .assertEqual (envelope .tags ["ai.location.ip" ], "192.168.1.1" )
541
543
self .assertEqual (envelope .time , ns_to_iso_str (record .timestamp ))
542
544
self .assertEqual (envelope .data .base_type , "EventData" )
543
545
self .assertEqual (envelope .data .base_data .name , "event_name" )
Original file line number Diff line number Diff line change
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 ()
You can’t perform that action at this time.
0 commit comments