@@ -515,10 +515,12 @@ def get_dd_trace_context():
515515 automatically, but this function can be used to manually inject the trace
516516 context to an outgoing request.
517517 """
518- global dd_trace_context
518+ if dd_tracing_enabled :
519+ dd_trace_py_context = _get_dd_trace_py_context ()
520+ if dd_trace_py_context is not None :
521+ return _context_obj_to_headers (dd_trace_py_context )
519522
520- context = None
521- xray_context = None
523+ global dd_trace_context
522524
523525 try :
524526 xray_context = _get_xray_trace_context () # xray (sub)segment
@@ -527,22 +529,17 @@ def get_dd_trace_context():
527529 "get_dd_trace_context couldn't read from segment from x-ray, with error %s"
528530 % e
529531 )
532+ if not xray_context :
533+ return {}
530534
531- if xray_context and not dd_trace_context :
532- context = xray_context
533- elif xray_context and dd_trace_context :
534- context = dd_trace_context .copy ()
535- context ["parent-id" ] = xray_context .get ("parent-id" )
536- logger .debug (
537- "Set parent id from xray trace context: %s" , context .get ("parent-id" )
538- )
535+ if not dd_trace_context :
536+ return _context_obj_to_headers (xray_context )
539537
540- if dd_tracing_enabled :
541- dd_trace_py_context = _get_dd_trace_py_context ()
542- if dd_trace_py_context is not None :
543- context = dd_trace_py_context
538+ context = dd_trace_context .copy ()
539+ context ["parent-id" ] = xray_context .get ("parent-id" )
540+ logger .debug ("Set parent id from xray trace context: %s" , context .get ("parent-id" ))
544541
545- return _context_obj_to_headers (context ) if context is not None else {}
542+ return _context_obj_to_headers (context )
546543
547544
548545def set_correlation_ids ():
@@ -561,6 +558,8 @@ def set_correlation_ids():
561558 return
562559
563560 context = get_dd_trace_context ()
561+ if not context :
562+ return
564563
565564 span = tracer .trace ("dummy.span" )
566565 span .trace_id = int (context [TraceHeader .TRACE_ID ])
0 commit comments