Skip to content

Commit f9f34c5

Browse files
committed
fix: Improved getting active tracer and current span mechanism
Signed-off-by: Cagri Yonca <cagri@ibm.com>
1 parent aff617e commit f9f34c5

File tree

2 files changed

+25
-67
lines changed

2 files changed

+25
-67
lines changed

src/instana/util/traceutils.py

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
from instana.log import logger
1717
from instana.singletons import agent, get_tracer
1818
from instana.span.span import get_current_span
19+
from instana.span.span import InstanaSpan
1920

2021
if TYPE_CHECKING:
21-
from instana.span.span import InstanaSpan
2222
from instana.tracer import InstanaTracer
2323

2424

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

6464

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

tests/util/test_traceutils.py

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44
from typing import Generator
55
import pytest
66

7-
from instana.singletons import agent, get_tracer
8-
from instana.tracer import InstanaTracer
7+
from instana.singletons import agent, tracer
98
from instana.util.traceutils import (
109
extract_custom_headers,
11-
get_active_tracer,
1210
get_tracer_tuple,
13-
tracing_is_off,
1411
)
1512

1613

@@ -66,41 +63,16 @@ def test_extract_custom_headers(self, span, custom_headers, format) -> None:
6663
assert span.attributes["http.header.X-Capture-This-Too"] == "this too"
6764
assert span.attributes["http.header.X-Capture-That-Too"] == "that too"
6865

69-
def test_get_activate_tracer(self, mocker) -> None:
70-
assert not get_active_tracer()
7166

72-
with self.tracer.start_as_current_span("test"):
73-
response = get_active_tracer()
74-
assert isinstance(response, InstanaTracer)
75-
assert response == self.tracer
76-
with mocker.patch(
77-
"instana.span.span.InstanaSpan.is_recording", return_value=False
78-
):
79-
assert not get_active_tracer()
67+
def test_get_tracer_tuple() -> None:
68+
response = get_tracer_tuple()
69+
assert response == (None, None, None)
8070

81-
def test_get_tracer_tuple(
82-
self,
83-
) -> None:
84-
response = get_tracer_tuple()
85-
assert response == (None, None, None)
71+
agent.options.allow_exit_as_root = True
72+
response = get_tracer_tuple()
73+
assert response == (tracer, None, None)
74+
agent.options.allow_exit_as_root = False
8675

87-
agent.options.allow_exit_as_root = True
76+
with tracer.start_as_current_span("test") as span:
8877
response = get_tracer_tuple()
89-
assert response == (self.tracer, None, None)
90-
agent.options.allow_exit_as_root = False
91-
92-
with self.tracer.start_as_current_span("test") as span:
93-
response = get_tracer_tuple()
94-
assert response == (self.tracer, span, span.name)
95-
96-
def test_tracing_is_off(self) -> None:
97-
response = tracing_is_off()
98-
assert response
99-
with self.tracer.start_as_current_span("test"):
100-
response = tracing_is_off()
101-
assert not response
102-
103-
agent.options.allow_exit_as_root = True
104-
response = tracing_is_off()
105-
assert not response
106-
agent.options.allow_exit_as_root = False
78+
assert response == (tracer, span, span.name)

0 commit comments

Comments
 (0)