Skip to content

Commit 6ce4c3e

Browse files
committed
fix: Improved getting active tracer and current span mechanism
Signed-off-by: Cagri Yonca <[email protected]>
1 parent 4121d8d commit 6ce4c3e

File tree

2 files changed

+16
-59
lines changed

2 files changed

+16
-59
lines changed

src/instana/util/traceutils.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
)
1414

1515
from instana.log import logger
16-
from instana.singletons import agent, tracer
16+
from instana.singletons import agent, get_tracer
1717
from instana.span.span import get_current_span
18+
from instana.span.span import InstanaSpan
1819

1920
if TYPE_CHECKING:
20-
from instana.span.span import InstanaSpan
2121
from instana.tracer import InstanaTracer
2222

2323

@@ -61,22 +61,6 @@ def extract_custom_headers(
6161
logger.debug("extract_custom_headers: ", exc_info=True)
6262

6363

64-
def get_active_tracer() -> Optional["InstanaTracer"]:
65-
"""Get the currently active tracer if one exists."""
66-
try:
67-
current_span = get_current_span()
68-
if current_span:
69-
# asyncio Spans are used as NonRecording Spans solely for context propagation
70-
if current_span.is_recording() or current_span.name == "asyncio":
71-
return tracer
72-
return None
73-
return None
74-
except Exception:
75-
# Do not try to log this with instana, as there is no active tracer and there will be an infinite loop at least
76-
# for PY2
77-
return None
78-
79-
8064
def get_tracer_tuple() -> (
8165
Tuple[
8266
Optional["InstanaTracer"],
@@ -85,15 +69,17 @@ def get_tracer_tuple() -> (
8569
]
8670
):
8771
"""Get a tuple of (tracer, span, span_name) for the current context."""
88-
active_tracer = get_active_tracer()
89-
current_span = get_current_span()
90-
if active_tracer:
91-
return (active_tracer, current_span, current_span.name)
92-
elif agent.options.allow_exit_as_root:
93-
return (tracer, None, None)
94-
return (None, None, None)
95-
96-
97-
def tracing_is_off() -> bool:
98-
"""Check if tracing is currently disabled."""
99-
return not (bool(get_active_tracer()) or agent.options.allow_exit_as_root)
72+
try:
73+
active_tracer = get_tracer()
74+
current_span = get_current_span()
75+
# asyncio Spans are used as NonRecording Spans solely for context propagation
76+
if current_span and isinstance(current_span, InstanaSpan):
77+
if current_span.is_recording() or current_span.name == "asyncio":
78+
return (active_tracer, current_span, current_span.name)
79+
elif agent.options.allow_exit_as_root:
80+
return (active_tracer, None, None)
81+
return (None, None, None)
82+
except Exception:
83+
# Do not try to log this with instana, as there is no active tracer and there will be an infinite loop at least
84+
# for PY2
85+
return (None, None, None)

tests/util/test_traceutils.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
import pytest
44

55
from instana.singletons import agent, tracer
6-
from instana.tracer import InstanaTracer
76
from instana.util.traceutils import (
87
extract_custom_headers,
9-
get_active_tracer,
108
get_tracer_tuple,
11-
tracing_is_off,
129
)
1310

1411

@@ -57,19 +54,6 @@ def test_extract_custom_headers(span, custom_headers, format) -> None:
5754
assert span.attributes["http.header.X-Capture-That-Too"] == "that too"
5855

5956

60-
def test_get_activate_tracer(mocker) -> None:
61-
assert not get_active_tracer()
62-
63-
with tracer.start_as_current_span("test"):
64-
response = get_active_tracer()
65-
assert isinstance(response, InstanaTracer)
66-
assert response == tracer
67-
with mocker.patch(
68-
"instana.span.span.InstanaSpan.is_recording", return_value=False
69-
):
70-
assert not get_active_tracer()
71-
72-
7357
def test_get_tracer_tuple() -> None:
7458
response = get_tracer_tuple()
7559
assert response == (None, None, None)
@@ -82,16 +66,3 @@ def test_get_tracer_tuple() -> None:
8266
with tracer.start_as_current_span("test") as span:
8367
response = get_tracer_tuple()
8468
assert response == (tracer, span, span.name)
85-
86-
87-
def test_tracing_is_off() -> None:
88-
response = tracing_is_off()
89-
assert response
90-
with tracer.start_as_current_span("test"):
91-
response = tracing_is_off()
92-
assert not response
93-
94-
agent.options.allow_exit_as_root = True
95-
response = tracing_is_off()
96-
assert not response
97-
agent.options.allow_exit_as_root = False

0 commit comments

Comments
 (0)