55
66from __future__ import annotations
77
8- import json
98from collections .abc import Mapping , MutableMapping
109from typing import Any , Literal
1110
12- TemporalLogExtraMode = Literal ["dict" , "flatten" , "json" ]
11+ TemporalLogExtraMode = Literal ["dict" , "flatten" ]
1312"""Mode controlling how Temporal context is added to log record extra.
1413
1514Values:
2019 namespaced prefix. Values that are not primitives (str/int/float/bool)
2120 are converted to strings. This mode is recommended for OpenTelemetry
2221 and other logging pipelines that require flat, scalar attributes.
23- json: Add context as a JSON string under a single key. Useful when
24- downstream systems expect string values but you want structured data.
2522"""
2623
2724
2825def _apply_temporal_context_to_extra (
2926 extra : MutableMapping [str , Any ],
3027 * ,
3128 key : str ,
32- prefix : str ,
3329 ctx : Mapping [str , Any ],
3430 mode : TemporalLogExtraMode ,
3531) -> None :
3632 """Apply temporal context to log record extra based on the configured mode.
3733
3834 Args:
3935 extra: The mutable extra dict to update.
40- key: The key to use for dict/json modes (e.g., "temporal_workflow").
41- prefix: The prefix to use for flatten mode keys (e.g., "temporal.workflow").
36+ key: The base key (e.g., "temporal_workflow"). In dict mode this is
37+ used directly. In flatten mode the prefix is derived by replacing
38+ underscores with dots (e.g., "temporal.workflow").
4239 ctx: The context mapping containing temporal fields.
4340 mode: The mode controlling how context is added.
4441 """
45- if mode == "dict" :
46- extra [key ] = dict (ctx )
47- elif mode == "json" :
48- extra [key ] = json .dumps (ctx , separators = ("," , ":" ), default = str )
49- elif mode == "flatten" :
42+ if mode == "flatten" :
43+ prefix = key .replace ("_" , "." )
5044 for k , v in ctx .items ():
5145 # Ensure value is a primitive type safe for OTel attributes
5246 if not isinstance (v , (str , int , float , bool , type (None ))):
5347 v = str (v )
5448 extra [f"{ prefix } .{ k } " ] = v
5549 else :
56- # Fallback to dict for any unknown mode (shouldn't happen with typing)
5750 extra [key ] = dict (ctx )
5851
5952
6053def _update_temporal_context_in_extra (
6154 extra : MutableMapping [str , Any ],
6255 * ,
6356 key : str ,
64- prefix : str ,
6557 update_ctx : Mapping [str , Any ],
6658 mode : TemporalLogExtraMode ,
6759) -> None :
@@ -71,38 +63,18 @@ def _update_temporal_context_in_extra(
7163
7264 Args:
7365 extra: The mutable extra dict to update.
74- key: The key used for dict/json modes (e.g., "temporal_workflow").
75- prefix: The prefix used for flatten mode keys (e.g., "temporal.workflow").
66+ key: The base key (e.g., "temporal_workflow"). In dict mode this is
67+ used directly. In flatten mode the prefix is derived by replacing
68+ underscores with dots (e.g., "temporal.workflow").
7669 update_ctx: Additional context fields to add/update.
7770 mode: The mode controlling how context is added.
7871 """
79- if mode == "dict" :
80- extra .setdefault (key , {}).update (update_ctx )
81- elif mode == "json" :
82- # For JSON mode, we need to parse, update, and re-serialize
83- existing = extra .get (key )
84- if existing is not None :
85- try :
86- existing_dict = json .loads (existing )
87- existing_dict .update (update_ctx )
88- extra [key ] = json .dumps (
89- existing_dict , separators = ("," , ":" ), default = str
90- )
91- except (json .JSONDecodeError , TypeError ):
92- # If parsing fails, just create a new JSON object with update_ctx
93- extra [key ] = json .dumps (
94- dict (update_ctx ), separators = ("," , ":" ), default = str
95- )
96- else :
97- extra [key ] = json .dumps (
98- dict (update_ctx ), separators = ("," , ":" ), default = str
99- )
100- elif mode == "flatten" :
72+ if mode == "flatten" :
73+ prefix = key .replace ("_" , "." )
10174 for k , v in update_ctx .items ():
10275 # Ensure value is a primitive type safe for OTel attributes
10376 if not isinstance (v , (str , int , float , bool , type (None ))):
10477 v = str (v )
10578 extra [f"{ prefix } .{ k } " ] = v
10679 else :
107- # Fallback to dict for any unknown mode
10880 extra .setdefault (key , {}).update (update_ctx )
0 commit comments