Skip to content

Commit d0d509b

Browse files
committed
Addessed multiple minor comments
1 parent 3ec77fc commit d0d509b

2 files changed

Lines changed: 11 additions & 19 deletions

File tree

featuremanagement/azuremonitor/_send_telemetry.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import logging
99
import inspect
10+
from logging import INFO
1011
from typing import Any, Callable, Dict, Optional
1112
from .._models import VariantAssignmentReason, EvaluationEvent, TargetingContext
1213

@@ -17,7 +18,6 @@
1718
_event_logger.propagate = False
1819

1920
try:
20-
from logging import INFO
2121
from opentelemetry.sdk._logs import LoggingHandler
2222
from opentelemetry.sdk.trace import SpanProcessor
2323

@@ -28,20 +28,17 @@
2828
SpanProcessor = object # type: ignore
2929

3030

31-
_EVENTS_LOGGER_INITIALIZED: list[bool] = []
31+
_EVENTS_LOGGER_INITIALIZED: bool = False
3232

3333

3434
def _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

4744
FEATURE_NAME = "FeatureName"
@@ -54,6 +51,7 @@ def _initialize_event_logger() -> None:
5451
VERSION = "Version"
5552
VARIANT_ASSIGNMENT_PERCENTAGE = "VariantAssignmentPercentage"
5653
MICROSOFT_TARGETING_ID = "Microsoft.TargetingId"
54+
CUSTOM_EVENT_NAME = "microsoft.custom_event.name"
5755

5856
EVENT_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

9487
def 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):

tests/test_send_telemetry_appinsights.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def test_track_event_preserves_reserved_custom_event_name(self):
6464
with (
6565
patch("featuremanagement.azuremonitor._send_telemetry._initialize_event_logger"),
6666
patch("featuremanagement.azuremonitor._send_telemetry._event_logger.info") as mock_event_logger_info,
67-
patch("featuremanagement.azuremonitor._send_telemetry.HAS_OPENTELEMETRY_LOGGING", True),
6867
):
6968
featuremanagement.azuremonitor._send_telemetry.track_event( # pylint: disable=protected-access
7069
"FeatureEvaluation",

0 commit comments

Comments
 (0)