Skip to content

Commit 6c4c825

Browse files
committed
Conditional settings logic for instrumentation
1 parent bd937a0 commit 6c4c825

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

newrelic/hooks/hybridagent_opentelemetry.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from newrelic.api.transaction import Sentinel, current_transaction
2121
from newrelic.common.object_wrapper import wrap_function_wrapper
2222
from newrelic.common.signature import bind_args
23+
from newrelic.core.config import global_settings
2324

2425
_logger = logging.getLogger(__name__)
2526
_TRACER_PROVIDER = None
@@ -34,6 +35,11 @@
3435

3536

3637
def wrap_set_tracer_provider(wrapped, instance, args, kwargs):
38+
settings = global_settings()
39+
40+
if not settings.otel_bridge.enabled:
41+
return wrapped(*args, **kwargs)
42+
3743
global _TRACER_PROVIDER
3844
bound_args = bind_args(wrapped, args, kwargs)
3945
tracer_provider = bound_args.get("tracer_provider", None)
@@ -44,6 +50,11 @@ def wrap_set_tracer_provider(wrapped, instance, args, kwargs):
4450

4551

4652
def wrap_get_tracer_provider(wrapped, instance, args, kwargs):
53+
settings = global_settings()
54+
55+
if not settings.otel_bridge.enabled:
56+
return wrapped(*args, **kwargs)
57+
4758
from newrelic.api.opentelemetry import TracerProvider
4859

4960
# This needs to act as a singleton, like the agent instance.
@@ -70,6 +81,13 @@ def wrap_get_current_span(wrapped, instance, args, kwargs):
7081
if not transaction or isinstance(trace, Sentinel):
7182
return wrapped(*args, **kwargs)
7283

84+
# Do not allow the wrapper to continue if
85+
# the Hybrid Agent setting is not enabled
86+
settings = transaction.settings or global_settings()
87+
88+
if not settings.otel_bridge.enabled:
89+
return wrapped(*args, **kwargs)
90+
7391
# If a NR trace does exist, check to see if the current
7492
# OTel span corresponds to the current NR trace. If so,
7593
# return the original function's result.
@@ -119,6 +137,13 @@ def wrap_start_internal_or_server_span(wrapped, instance, args, kwargs):
119137
# and put that into the attributes. Keep the original
120138
# context_carrier intact.
121139

140+
# Do not allow the wrapper to continue if
141+
# the Hybrid Agent setting is not enabled
142+
settings = global_settings()
143+
144+
if not settings.otel_bridge.enabled:
145+
return wrapped(*args, **kwargs)
146+
122147
bound_args = bind_args(wrapped, args, kwargs)
123148
context_carrier = bound_args.get("context_carrier", None)
124149
attributes = bound_args.get("attributes", {})

tests/hybridagent_opentelemetry/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"transaction_tracer.stack_trace_threshold": 0.0,
2525
"debug.log_data_collector_payloads": True,
2626
"debug.record_transaction_failure": True,
27+
"otel_bridge.enabled": True,
2728
}
2829

2930
collector_agent_registration = collector_agent_registration_fixture(

0 commit comments

Comments
 (0)