Skip to content

Commit bdc1fd3

Browse files
committed
fix(urllib3): ignore internal urllib3 span creation
Signed-off-by: Cagri Yonca <[email protected]>
1 parent ea9c383 commit bdc1fd3

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/instana/instrumentation/urllib3.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
from instana.propagators.format import Format
1212
from instana.singletons import agent
1313
from instana.util.secrets import strip_secrets_from_query
14-
from instana.util.traceutils import get_tracer_tuple, tracing_is_off, extract_custom_headers
14+
from instana.util.traceutils import (
15+
get_tracer_tuple,
16+
tracing_is_off,
17+
extract_custom_headers,
18+
)
1519

1620
if TYPE_CHECKING:
1721
from instana.span.span import InstanaSpan
@@ -91,7 +95,23 @@ def urlopen_with_instana(
9195
tracer, parent_span, span_name = get_tracer_tuple()
9296

9397
# If we're not tracing, just return; boto3 has it's own visibility
94-
if tracing_is_off() or (span_name == "boto3"):
98+
# Also, skip creating spans for internal Instana calls when
99+
# 'com.instana' appears in either the full URL, the path argument,
100+
# or the connection host.
101+
request_url_or_path = (
102+
kwargs.get("request_url")
103+
or kwargs.get("url")
104+
or (args[1] if len(args) >= 2 else "")
105+
or ""
106+
)
107+
host = getattr(instance, "host", "") or ""
108+
109+
if (
110+
tracing_is_off()
111+
or span_name == "boto3"
112+
or "com.instana" in request_url_or_path
113+
or "com.instana" in host
114+
):
95115
return wrapped(*args, **kwargs)
96116

97117
parent_context = parent_span.get_span_context() if parent_span else None

tests/clients/test_urllib3.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,3 +992,41 @@ def test_collect_kvs_exception(
992992
caplog.set_level(logging.DEBUG, logger="instana")
993993
collect_kvs({}, (), {})
994994
assert "urllib3 _collect_kvs error: " in caplog.messages
995+
996+
def test_internal_span_creation_with_url_in_hostname(self) -> None:
997+
internal_url = "https://com.instana.example.com/api/test"
998+
999+
with tracer.start_as_current_span("test"):
1000+
try:
1001+
self.http.request("GET", internal_url, retries=False, timeout=1)
1002+
except Exception:
1003+
pass
1004+
1005+
spans = self.recorder.queued_spans()
1006+
1007+
assert len(spans) == 1
1008+
1009+
test_span = spans[0]
1010+
assert test_span.data["sdk"]["name"] == "test"
1011+
1012+
urllib3_spans = [span for span in spans if span.n == "urllib3"]
1013+
assert len(urllib3_spans) == 0
1014+
1015+
def test_internal_span_creation_with_url_in_path(self) -> None:
1016+
internal_url_path = "https://example.com/com.instana/api/test"
1017+
1018+
with tracer.start_as_current_span("test"):
1019+
try:
1020+
self.http.request("GET", internal_url_path, retries=False, timeout=1)
1021+
except Exception:
1022+
pass
1023+
1024+
spans = self.recorder.queued_spans()
1025+
1026+
assert len(spans) == 1
1027+
1028+
test_span = spans[0]
1029+
assert test_span.data["sdk"]["name"] == "test"
1030+
1031+
urllib3_spans = [span for span in spans if span.n == "urllib3"]
1032+
assert len(urllib3_spans) == 0

0 commit comments

Comments
 (0)