@@ -320,6 +320,120 @@ def test_flattened_keys_do_not_conflict_with_logrecord_attrs(self) -> None:
320320 ), f"Key { key } doesn't have temporal prefix"
321321
322322
323+ class TestFlattenModeOTelSafety :
324+ """Critical tests to verify flatten mode is fully OTel-safe."""
325+
326+ def test_flatten_mode_produces_zero_dicts_for_temporal_keys (self ) -> None :
327+ """Verify flatten mode has zero dict values for any temporal keys.
328+
329+ This is the critical OTel compatibility requirement - nested dicts
330+ cause OTel pipelines to reject or drop the attributes.
331+ """
332+ ctx = {
333+ "workflow_id" : "wf-001" ,
334+ "workflow_type" : "TestWorkflow" ,
335+ "run_id" : "run-001" ,
336+ "namespace" : "default" ,
337+ "task_queue" : "test-queue" ,
338+ "attempt" : 1 ,
339+ }
340+ extra : dict [str , Any ] = {}
341+ _apply_temporal_context_to_extra (
342+ extra ,
343+ key = "temporal_workflow" ,
344+ prefix = "temporal.workflow" ,
345+ ctx = ctx ,
346+ mode = "flatten" ,
347+ )
348+
349+ # Assert no dict-valued keys exist at all
350+ for key , value in extra .items ():
351+ assert not isinstance (value , dict ), (
352+ f"Flatten mode violation: { key } ={ type (value ).__name__ } "
353+ f"(expected primitive, got dict)"
354+ )
355+
356+ # Assert legacy nested keys don't exist in flatten mode
357+ assert (
358+ "temporal_workflow" not in extra
359+ ), "Legacy nested key 'temporal_workflow' should not exist in flatten mode"
360+ assert (
361+ "temporal_activity" not in extra
362+ ), "Legacy nested key 'temporal_activity' should not exist in flatten mode"
363+
364+ def test_flatten_mode_activity_produces_zero_dicts (self ) -> None :
365+ """Verify activity flatten mode has zero dict values."""
366+ ctx = {
367+ "activity_id" : "act-001" ,
368+ "activity_type" : "TestActivity" ,
369+ "attempt" : 1 ,
370+ "namespace" : "default" ,
371+ "task_queue" : "test-queue" ,
372+ "workflow_id" : "wf-001" ,
373+ "workflow_run_id" : "run-001" ,
374+ "workflow_type" : "TestWorkflow" ,
375+ }
376+ extra : dict [str , Any ] = {}
377+ _apply_temporal_context_to_extra (
378+ extra ,
379+ key = "temporal_activity" ,
380+ prefix = "temporal.activity" ,
381+ ctx = ctx ,
382+ mode = "flatten" ,
383+ )
384+
385+ # Assert no dict-valued keys exist
386+ for key , value in extra .items ():
387+ assert not isinstance (
388+ value , dict
389+ ), f"Flatten mode violation: { key } ={ type (value ).__name__ } "
390+
391+ # Assert legacy nested key doesn't exist
392+ assert "temporal_activity" not in extra
393+
394+ def test_flatten_mode_with_update_produces_zero_dicts (self ) -> None :
395+ """Verify flatten mode with update info still produces zero dicts."""
396+ workflow_ctx = {
397+ "workflow_id" : "wf-001" ,
398+ "workflow_type" : "TestWorkflow" ,
399+ }
400+ update_ctx = {
401+ "update_id" : "upd-001" ,
402+ "update_name" : "my_update" ,
403+ }
404+ extra : dict [str , Any ] = {}
405+
406+ # Apply workflow context
407+ _apply_temporal_context_to_extra (
408+ extra ,
409+ key = "temporal_workflow" ,
410+ prefix = "temporal.workflow" ,
411+ ctx = workflow_ctx ,
412+ mode = "flatten" ,
413+ )
414+ # Apply update context
415+ _update_temporal_context_in_extra (
416+ extra ,
417+ key = "temporal_workflow" ,
418+ prefix = "temporal.workflow" ,
419+ update_ctx = update_ctx ,
420+ mode = "flatten" ,
421+ )
422+
423+ # Assert no dict-valued keys exist after both operations
424+ for key , value in extra .items ():
425+ assert not isinstance (
426+ value , dict
427+ ), f"Flatten mode violation after update: { key } ={ type (value ).__name__ } "
428+
429+ # Assert legacy nested key doesn't exist
430+ assert "temporal_workflow" not in extra
431+
432+ # Verify all expected keys are present as flat primitives
433+ assert extra ["temporal.workflow.workflow_id" ] == "wf-001"
434+ assert extra ["temporal.workflow.update_id" ] == "upd-001"
435+
436+
323437class TestLogRecordAccessibility :
324438 """Tests to verify flattened attributes are accessible on LogRecord.__dict__."""
325439
0 commit comments