Skip to content

Commit ab2003e

Browse files
yuanqu72Copilot
andauthored
Apply suggestions from code review
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent df9272b commit ab2003e

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

featuremanagement/azuremonitor/_send_telemetry.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,16 @@ def track_event(event_name: str, user: str, event_properties: Optional[Dict[str,
8080
event_properties[TARGETING_ID] = user
8181

8282
# Azure Monitor exporter maps this attribute to customEvent telemetry name.
83-
custom_event_attributes = {
84-
**event_properties,
85-
"microsoft.custom_event.name": event_name,
86-
}
87-
_event_logger.info(event_name, extra=custom_event_attributes)
83+
custom_event_attributes = {**event_properties, "microsoft.custom_event.name": event_name}
84+
85+
# logging raises KeyError if an `extra` key overwrites a built-in LogRecord attribute (e.g. "name", "message").
86+
reserved = logging.makeLogRecord({}).__dict__
87+
safe_attributes: Dict[str, Optional[str]] = {}
88+
for key, value in custom_event_attributes.items():
89+
safe_key = key if key not in reserved else f"telemetry.{key}"
90+
safe_attributes[safe_key] = value
91+
92+
_event_logger.info(event_name, extra=safe_attributes)
8893

8994

9095
def publish_telemetry(evaluation_event: EvaluationEvent) -> None:

0 commit comments

Comments
 (0)