@@ -487,6 +487,30 @@ def test_format_with_json_fields(self):
487487 self .assertEqual (result ["hello" ], "world" )
488488 self .assertEqual (result ["number" ], 12 )
489489
490+ def test_format_with_nested_json (self ):
491+ """
492+ JSON can contain nested dictionaries of data
493+ """
494+ import logging
495+ import json
496+
497+ handler = self ._make_one ()
498+ json_fields = {"outer" : {"inner" : {"hello" : "world" }}}
499+ record = logging .LogRecord (
500+ None ,
501+ logging .INFO ,
502+ None ,
503+ None ,
504+ None ,
505+ None ,
506+ None ,
507+ )
508+ record .created = None
509+ setattr (record , "json_fields" , json_fields )
510+ handler .filter (record )
511+ result = json .loads (handler .format (record ))
512+ self .assertEqual (result ["outer" ], json_fields ["outer" ])
513+
490514 def test_emits_instrumentation_info (self ):
491515 import logging
492516 import mock
@@ -510,3 +534,51 @@ def side_effect():
510534
511535 # emit_instrumentation_info should be called once
512536 emit_info .assert_called_once ()
537+
538+ def test_valid_instrumentation_info (self ):
539+ import logging
540+ import mock
541+ import json
542+
543+ with mock .patch .object (logging , "info" ) as mock_log :
544+ handler = self ._make_one ()
545+ handler .emit_instrumentation_info ()
546+ mock_log .assert_called_once ()
547+ # ensure instrumentaiton payload is formatted as expected
548+ called_payload = mock_log .call_args .args [0 ]
549+ self .assertEqual (len (called_payload .keys ()), 1 )
550+ self .assertIn ("logging.googleapis.com/diagnostic" , called_payload .keys ())
551+ inst_source_dict = called_payload ["logging.googleapis.com/diagnostic" ]
552+ self .assertEqual (len (inst_source_dict .keys ()), 1 )
553+ self .assertIn ("instrumentation_source" , inst_source_dict .keys ())
554+ source_list = inst_source_dict ["instrumentation_source" ]
555+ self .assertEqual (
556+ len (source_list ), 1 , "expected single instrumentation source"
557+ )
558+ for source_dict in source_list :
559+ self .assertEqual (
560+ len (source_dict .keys ()),
561+ 2 ,
562+ f"expected two keys in payload: { source_dict .keys ()} " ,
563+ )
564+ self .assertIn ("name" , source_dict .keys ())
565+ self .assertIn ("version" , source_dict .keys ())
566+ self .assertEqual (source_dict ["name" ], "python" )
567+ # ensure it is parsed properly by handler
568+ record = logging .LogRecord (
569+ None ,
570+ logging .INFO ,
571+ None ,
572+ None ,
573+ called_payload ,
574+ None ,
575+ None ,
576+ )
577+ record .created = None
578+ handler .filter (record )
579+ result = json .loads (handler .format (record ))
580+ self .assertEqual (
581+ result ["logging.googleapis.com/diagnostic" ],
582+ inst_source_dict ,
583+ "instrumentation payload not logged properly" ,
584+ )
0 commit comments