Skip to content

Commit 7b8ade6

Browse files
committed
Bug fix and comment on test
1 parent e9a88d6 commit 7b8ade6

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

newrelic/api/opentelemetry.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
from opentelemetry import trace as otel_api_trace
1919
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
20-
from opentelemetry import metrics as otel_api_metric
2120
from opentelemetry.baggage.propagation import W3CBaggagePropagator
2221
from opentelemetry.propagators.composite import CompositePropagator
2322
from 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,

tests/hybridagent_opentelemetry/test_hybrid_cross_agent.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
from testing_support.validators.validate_transaction_count import validate_transaction_count
2525
from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics
26+
from testing_support.validators.validate_span_events import validate_span_events
27+
from testing_support.validators.validate_error_event_attributes import validate_error_event_attributes
28+
from testing_support.fixtures import override_application_settings
2629

2730
from newrelic.api.application import application_instance
2831
from newrelic.api.background_task import BackgroundTask
@@ -296,16 +299,27 @@ def test_Starting_transaction_tests():
296299
# Inbound distributed tracing tests
297300
@validate_transaction_metrics(name="Foo")
298301
@validate_span_events(count=0)
302+
@override_application_settings(
303+
{
304+
"trusted_account_key": "1",
305+
"account_id": "1",
306+
}
307+
)
299308
def test_Inbound_distributed_tracing_tests():
309+
"""
310+
This test intends to check for a scenario where an external call
311+
span is made outside the context of an existing transaction.
312+
By flagging that span as not sampled in OTel, it means that the OTel
313+
api will reflect that our agent is also ignoring that span. In
314+
this case, it will create the server transaction, but no spans.
315+
"""
300316
with tracer.start_as_current_span(name="Foo", kind=otel_api_trace.SpanKind.SERVER):
301317
carrier = {
302318
"traceparent": "00-da8bc8cc6d062849b0efcf3c169afb5a-7d3efb1b173fecfa-00",
303-
"tracestate": "11159259@nr=0-0-1-12345678-7d3efb1b173fecfa-da8bc8cc6d062849-0-0.23456-1011121314151", # REMOVE FROM HERE.
319+
"tracestate": "1@nr=0-0-1-12345678-7d3efb1b173fecfa-da8bc8cc6d062849-0-0.23456-1011121314151",
304320
}
305321
PROPAGATOR.extract(carrier=carrier)
306322

307323
current_span = otel_api_trace.get_current_span()
308324

309325
assert current_span.get_span_context().trace_id == 0xda8bc8cc6d062849b0efcf3c169afb5a
310-
311-

0 commit comments

Comments
 (0)