77
88import logging
99import inspect
10+ from logging import INFO
1011from typing import Any , Callable , Dict , Optional
1112from .._models import VariantAssignmentReason , EvaluationEvent , TargetingContext
1213
1718_event_logger .propagate = False
1819
1920try :
20- from logging import INFO
2121 from opentelemetry .sdk ._logs import LoggingHandler
2222 from opentelemetry .sdk .trace import SpanProcessor
2323
2828 SpanProcessor = object # type: ignore
2929
3030
31- _EVENTS_LOGGER_INITIALIZED : list [ bool ] = []
31+ _EVENTS_LOGGER_INITIALIZED : bool = False
3232
3333
3434def _initialize_event_logger () -> None :
35+ global _EVENTS_LOGGER_INITIALIZED # pylint: disable=global-statement
3536 if _EVENTS_LOGGER_INITIALIZED :
3637 return
3738
38- if not HAS_OPENTELEMETRY_LOGGING :
39- logger .warning ("OpenTelemetry logging handler is not installed. Telemetry will not be sent." )
40- return
41-
4239 _event_logger .addHandler (LoggingHandler ())
4340 _event_logger .setLevel (INFO )
44- _EVENTS_LOGGER_INITIALIZED . append ( True )
41+ _EVENTS_LOGGER_INITIALIZED = True
4542
4643
4744FEATURE_NAME = "FeatureName"
@@ -54,6 +51,7 @@ def _initialize_event_logger() -> None:
5451VERSION = "Version"
5552VARIANT_ASSIGNMENT_PERCENTAGE = "VariantAssignmentPercentage"
5653MICROSOFT_TARGETING_ID = "Microsoft.TargetingId"
54+ CUSTOM_EVENT_NAME = "microsoft.custom_event.name"
5755
5856EVENT_NAME = "FeatureEvaluation"
5957
@@ -79,16 +77,11 @@ def track_event(event_name: str, user: str, event_properties: Optional[Dict[str,
7977 event_properties [TARGETING_ID ] = user
8078
8179 # Azure Monitor exporter maps this attribute to customEvent telemetry name.
82- custom_event_attributes = {** event_properties , "microsoft.custom_event.name" : event_name }
83-
84- # logging raises KeyError if an `extra` key overwrites a built-in LogRecord attribute (e.g. "name", "message").
85- reserved = logging .makeLogRecord ({}).__dict__
86- safe_attributes : Dict [str , Optional [str ]] = {}
87- for key , value in custom_event_attributes .items ():
88- safe_key = key if key not in reserved else f"telemetry.{ key } "
89- safe_attributes [safe_key ] = value
90-
91- _event_logger .info (event_name , extra = safe_attributes )
80+ custom_event_attributes = {
81+ ** event_properties ,
82+ CUSTOM_EVENT_NAME : event_name ,
83+ }
84+ _event_logger .info (event_name , extra = custom_event_attributes )
9285
9386
9487def publish_telemetry (evaluation_event : EvaluationEvent ) -> None :
@@ -166,7 +159,7 @@ def on_start(self, span: Any, parent_context: Optional[Any] = None) -> None: #
166159 :param parent_context: The parent context of the span.
167160 """
168161 if not HAS_OPENTELEMETRY_LOGGING :
169- logger .warning ("OpenTelemetry logging handler is not installed." )
162+ logger .info ("OpenTelemetry logging handler is not installed." )
170163 return
171164 if self ._targeting_context_accessor and callable (self ._targeting_context_accessor ):
172165 if inspect .iscoroutinefunction (self ._targeting_context_accessor ):
0 commit comments