88from collections .abc import Mapping
99
1010from ..log import logger
11- from ..util .traceutils import get_active_tracer
11+ from ..util .traceutils import get_tracer_tuple , tracing_is_off
1212
1313
1414@wrapt .patch_function_wrapper ('logging' , 'Logger._log' )
@@ -17,33 +17,34 @@ def log_with_instana(wrapped, instance, argv, kwargs):
1717 # argv[1] = message
1818 # argv[2] = args for message
1919 try :
20- active_tracer = get_active_tracer ()
20+ tracer , parent_span , _ = get_tracer_tuple ()
2121
2222 # Only needed if we're tracing and serious log
23- if active_tracer and argv [0 ] >= logging .WARN :
24-
25- msg = str (argv [1 ])
26- args = argv [2 ]
27- if args and len (args ) == 1 and isinstance (args [0 ], Mapping ) and args [0 ]:
28- args = args [0 ]
29-
30- # get the formatted log message
31- msg = msg % args
32-
33- # get additional information if an exception is being handled
34- parameters = None
35- (t , v , tb ) = sys .exc_info ()
36- if t is not None and v is not None :
37- parameters = '{} {}' .format (t , v )
38-
39- # create logging span
40- with active_tracer .start_active_span ('log' , child_of = active_tracer .active_span ) as scope :
41- scope .span .log_kv ({ 'message' : msg })
42- if parameters is not None :
43- scope .span .log_kv ({ 'parameters' : parameters })
44- # extra tags for an error
45- if argv [0 ] >= logging .ERROR :
46- scope .span .mark_as_errored ()
23+ if tracing_is_off () or argv [0 ] < logging .WARN :
24+ return wrapped (* argv , ** kwargs )
25+
26+ msg = str (argv [1 ])
27+ args = argv [2 ]
28+ if args and len (args ) == 1 and isinstance (args [0 ], Mapping ) and args [0 ]:
29+ args = args [0 ]
30+
31+ # get the formatted log message
32+ msg = msg % args
33+
34+ # get additional information if an exception is being handled
35+ parameters = None
36+ (t , v , tb ) = sys .exc_info ()
37+ if t is not None and v is not None :
38+ parameters = '{} {}' .format (t , v )
39+
40+ # create logging span
41+ with tracer .start_active_span ('log' , child_of = parent_span ) as scope :
42+ scope .span .log_kv ({ 'message' : msg })
43+ if parameters is not None :
44+ scope .span .log_kv ({ 'parameters' : parameters })
45+ # extra tags for an error
46+ if argv [0 ] >= logging .ERROR :
47+ scope .span .mark_as_errored ()
4748 except Exception :
4849 logger .debug ('log_with_instana:' , exc_info = True )
4950
0 commit comments