1717
1818from opentelemetry import trace as otel_api_trace
1919from opentelemetry .trace .propagation .tracecontext import TraceContextTextMapPropagator
20- from opentelemetry import metrics as otel_api_metric
2120from opentelemetry .baggage .propagation import W3CBaggagePropagator
2221from opentelemetry .propagators .composite import CompositePropagator
2322from opentelemetry .propagate import set_global_textmap
@@ -234,6 +233,11 @@ def __init__(
234233 # for debug purposes but eventually take this out.
235234 if not self .nr_transaction :
236235 raise ValueError ("HOW DID WE GET HERE??" )
236+
237+ # Do not create a New Relic trace if parent
238+ # is a remote span and it is not sampled
239+ if self ._is_remote () and not self ._is_sampled ():
240+ return
237241
238242 self .nr_parent = None
239243 current_nr_trace = current_trace ()
@@ -330,7 +334,7 @@ def get_span_context(self):
330334 (key , str (value )) for key , value in nr_tracestate_headers .items ()
331335 ]
332336 else :
333- otel_tracestate_headers = None
337+ otel_tracestate_headers = None
334338
335339 return otel_api_trace .SpanContext (
336340 trace_id = int (self .nr_transaction ._trace_id , 16 ),
@@ -433,17 +437,6 @@ class Tracer(otel_api_trace.Tracer):
433437 def __init__ (self , resource = None , instrumentation_library = None , * args , ** kwargs ):
434438 self .resource = resource
435439 self .instrumentation_library = instrumentation_library .split ("." )[- 1 ].capitalize ()
436- self .nr_application = application_instance (activate = False ) or register_application ("OtelTracer" )
437- self .global_settings = self .nr_application and self .nr_application .global_settings
438-
439- if self .nr_application and self .global_settings .enabled and self .nr_application .enabled :
440- self ._settings = self .nr_application .settings
441- if not self ._settings :
442- self .nr_application .activate ()
443- self ._settings = self .nr_application .settings
444- else :
445- # Unable to find or register application. We should log this.
446- pass
447440
448441 def start_span (
449442 self ,
@@ -465,13 +458,14 @@ def start_span(
465458 parent_span_context = None
466459
467460 nr_trace_type = FunctionTrace
461+ self .nr_application = application_instance ()
468462 transaction = current_transaction ()
469463 self .attributes = attributes or {}
470464
471465 # If parent_span_context exists, we can create traceparent
472466 # and tracestate headers
473467 _headers = {}
474- if parent_span_context and application_instance ( activate = False ) .settings .distributed_tracing .enabled :
468+ if parent_span_context and self . nr_application .settings .distributed_tracing .enabled :
475469 parent_span_trace_id = parent_span_context .trace_id
476470 parent_span_span_id = parent_span_context .span_id
477471 parent_span_trace_flags = parent_span_context .trace_flags
@@ -489,9 +483,9 @@ def start_span(
489483 request_method = self .attributes .get ("http.method" )
490484 request_path = self .attributes .get ("http.route" )
491485
492- if not headers :
493- headers = _headers
494- update_sampled_flag = True
486+ if not headers :
487+ headers = _headers
488+ update_sampled_flag = True
495489
496490 transaction = WebTransaction (
497491 self .nr_application ,
@@ -504,8 +498,8 @@ def start_span(
504498 headers = headers ,
505499 )
506500
507- if update_sampled_flag and parent_span_context :
508- transaction ._sampled = bool (parent_span_trace_flags )
501+ if update_sampled_flag and parent_span_context :
502+ transaction ._sampled = bool (parent_span_trace_flags )
509503 elif kind in (
510504 otel_api_trace .SpanKind .PRODUCER ,
511505 otel_api_trace .SpanKind .INTERNAL ,
0 commit comments