File tree Expand file tree Collapse file tree 5 files changed +40
-1
lines changed 
azure-monitor-opentelemetry-exporter 
azure/monitor/opentelemetry/exporter/export/logs 
azure-monitor-opentelemetry/samples/logging Expand file tree Collapse file tree 5 files changed +40
-1
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 3232    BaseExporter ,
3333    ExportResult ,
3434)
35+ from  azure .monitor .opentelemetry .exporter .export .trace  import  _utils  as  trace_utils 
3536from  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    )
Original file line number Diff line number Diff line change 2323logger_provider  =  LoggerProvider ()
2424set_logger_provider (logger_provider )
2525exporter  =  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 
2929handler  =  LoggingHandler ()
3535# The name of the `customEvent` will correspond to the value of the attribute`  
3636logger .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+ 
3841input ()
Original file line number Diff line number Diff 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 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