23
23
from . import bindings , constants , functions , loader , protos
24
24
from .bindings .shared_memory_data_transfer import SharedMemoryManager
25
25
from .constants import (
26
+ APPLICATIONINSIGHTS_CONNECTION_STRING ,
26
27
METADATA_PROPERTIES_WORKER_INDEXED ,
28
+ PYTHON_AZURE_MONITOR_LOGGER_NAME ,
29
+ PYTHON_AZURE_MONITOR_LOGGER_NAME_DEFAULT ,
27
30
PYTHON_ENABLE_DEBUG_LOGGING ,
28
31
PYTHON_ENABLE_INIT_INDEXING ,
29
32
PYTHON_ENABLE_OPENTELEMETRY ,
@@ -99,7 +102,7 @@ def __init__(self, loop: BaseEventLoop, host: str, port: int,
99
102
self ._function_metadata_exception = None
100
103
101
104
# Used for checking if open telemetry is enabled
102
- self ._otel_libs_available = False
105
+ self ._azure_monitor_available = False
103
106
self ._context_api = None
104
107
self ._trace_context_propagator = None
105
108
@@ -288,6 +291,46 @@ async def _dispatch_grpc_request(self, request):
288
291
resp = await request_handler (request )
289
292
self ._grpc_resp_queue .put_nowait (resp )
290
293
294
+ def initialize_azure_monitor (self ):
295
+ """Initializes OpenTelemetry and Azure monitor distro
296
+ """
297
+ self .update_opentelemetry_status ()
298
+ try :
299
+ from azure .monitor .opentelemetry import configure_azure_monitor
300
+
301
+ # Set functions resource detector manually until officially
302
+ # include in Azure monitor distro
303
+ os .environ .setdefault (
304
+ "OTEL_EXPERIMENTAL_RESOURCE_DETECTORS" ,
305
+ "azure_functions" ,
306
+ )
307
+
308
+ configure_azure_monitor (
309
+ # Connection string can be explicitly specified in Appsetting
310
+ # If not set, defaults to env var
311
+ # APPLICATIONINSIGHTS_CONNECTION_STRING
312
+ connection_string = get_app_setting (
313
+ setting = APPLICATIONINSIGHTS_CONNECTION_STRING
314
+ ),
315
+ logger_name = get_app_setting (
316
+ setting = PYTHON_AZURE_MONITOR_LOGGER_NAME ,
317
+ default_value = PYTHON_AZURE_MONITOR_LOGGER_NAME_DEFAULT
318
+ ),
319
+ )
320
+ self ._azure_monitor_available = True
321
+
322
+ logger .info ("Successfully configured Azure monitor distro." )
323
+ except ImportError :
324
+ logger .exception (
325
+ "Cannot import Azure Monitor distro."
326
+ )
327
+ self ._azure_monitor_available = False
328
+ except Exception :
329
+ logger .exception (
330
+ "Error initializing Azure monitor distro."
331
+ )
332
+ self ._azure_monitor_available = False
333
+
291
334
def update_opentelemetry_status (self ):
292
335
"""Check for OpenTelemetry library availability and
293
336
update the status attribute."""
@@ -299,12 +342,11 @@ def update_opentelemetry_status(self):
299
342
300
343
self ._context_api = context_api
301
344
self ._trace_context_propagator = TraceContextTextMapPropagator ()
302
- self ._otel_libs_available = True
303
345
304
- logger .info ("Successfully loaded OpenTelemetry modules. "
305
- "OpenTelemetry is now enabled." )
306
346
except ImportError :
307
- self ._otel_libs_available = False
347
+ logger .exception (
348
+ "Cannot import OpenTelemetry libraries."
349
+ )
308
350
309
351
async def _handle__worker_init_request (self , request ):
310
352
logger .info ('Received WorkerInitRequest, '
@@ -334,12 +376,11 @@ async def _handle__worker_init_request(self, request):
334
376
constants .RPC_HTTP_TRIGGER_METADATA_REMOVED : _TRUE ,
335
377
constants .SHARED_MEMORY_DATA_TRANSFER : _TRUE ,
336
378
}
337
-
338
379
if get_app_setting (setting = PYTHON_ENABLE_OPENTELEMETRY ,
339
380
default_value = PYTHON_ENABLE_OPENTELEMETRY_DEFAULT ):
340
- self .update_opentelemetry_status ()
381
+ self .initialize_azure_monitor ()
341
382
342
- if self ._otel_libs_available :
383
+ if self ._azure_monitor_available :
343
384
capabilities [constants .WORKER_OPEN_TELEMETRY_ENABLED ] = _TRUE
344
385
345
386
if DependencyManager .should_load_cx_dependencies ():
@@ -611,7 +652,7 @@ async def _handle__invocation_request(self, request):
611
652
args [name ] = bindings .Out ()
612
653
613
654
if fi .is_async :
614
- if self ._otel_libs_available :
655
+ if self ._azure_monitor_available :
615
656
self .configure_opentelemetry (fi_context )
616
657
617
658
call_result = \
@@ -731,9 +772,9 @@ async def _handle__function_environment_reload_request(self, request):
731
772
if get_app_setting (
732
773
setting = PYTHON_ENABLE_OPENTELEMETRY ,
733
774
default_value = PYTHON_ENABLE_OPENTELEMETRY_DEFAULT ):
734
- self .update_opentelemetry_status ()
775
+ self .initialize_azure_monitor ()
735
776
736
- if self ._otel_libs_available :
777
+ if self ._azure_monitor_available :
737
778
capabilities [constants .WORKER_OPEN_TELEMETRY_ENABLED ] = (
738
779
_TRUE )
739
780
@@ -944,7 +985,7 @@ def _run_sync_func(self, invocation_id, context, func, params):
944
985
# invocation_id from ThreadPoolExecutor's threads.
945
986
context .thread_local_storage .invocation_id = invocation_id
946
987
try :
947
- if self ._otel_libs_available :
988
+ if self ._azure_monitor_available :
948
989
self .configure_opentelemetry (context )
949
990
return ExtensionManager .get_sync_invocation_wrapper (context ,
950
991
func )(params )
0 commit comments