From 5e2fac8c3f0f704d9f126fb81d3414d4c98803d1 Mon Sep 17 00:00:00 2001 From: Cagri Yonca Date: Sat, 23 Aug 2025 16:03:08 +0200 Subject: [PATCH] fix(urllib3): ignore internal urllib3 span creation Signed-off-by: Cagri Yonca --- src/instana/instrumentation/urllib3.py | 24 ++++++++++++++-- tests/clients/test_urllib3.py | 38 ++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/instana/instrumentation/urllib3.py b/src/instana/instrumentation/urllib3.py index 4536d2be..52d3e9c8 100644 --- a/src/instana/instrumentation/urllib3.py +++ b/src/instana/instrumentation/urllib3.py @@ -11,7 +11,11 @@ from instana.propagators.format import Format from instana.singletons import agent from instana.util.secrets import strip_secrets_from_query -from instana.util.traceutils import get_tracer_tuple, tracing_is_off, extract_custom_headers +from instana.util.traceutils import ( + get_tracer_tuple, + tracing_is_off, + extract_custom_headers, +) if TYPE_CHECKING: from instana.span.span import InstanaSpan @@ -91,7 +95,23 @@ def urlopen_with_instana( tracer, parent_span, span_name = get_tracer_tuple() # If we're not tracing, just return; boto3 has it's own visibility - if tracing_is_off() or (span_name == "boto3"): + # Also, skip creating spans for internal Instana calls when + # 'com.instana' appears in either the full URL, the path argument, + # or the connection host. + request_url_or_path = ( + kwargs.get("request_url") + or kwargs.get("url") + or (args[1] if len(args) >= 2 else "") + or "" + ) + host = getattr(instance, "host", "") or "" + + if ( + tracing_is_off() + or span_name == "boto3" + or "com.instana" in request_url_or_path + or "com.instana" in host + ): return wrapped(*args, **kwargs) parent_context = parent_span.get_span_context() if parent_span else None diff --git a/tests/clients/test_urllib3.py b/tests/clients/test_urllib3.py index 62b07d49..6c5fc318 100644 --- a/tests/clients/test_urllib3.py +++ b/tests/clients/test_urllib3.py @@ -992,3 +992,41 @@ def test_collect_kvs_exception( caplog.set_level(logging.DEBUG, logger="instana") collect_kvs({}, (), {}) assert "urllib3 _collect_kvs error: " in caplog.messages + + def test_internal_span_creation_with_url_in_hostname(self) -> None: + internal_url = "https://com.instana.example.com/api/test" + + with tracer.start_as_current_span("test"): + try: + self.http.request("GET", internal_url, retries=False, timeout=1) + except Exception: + pass + + spans = self.recorder.queued_spans() + + assert len(spans) == 1 + + test_span = spans[0] + assert test_span.data["sdk"]["name"] == "test" + + urllib3_spans = [span for span in spans if span.n == "urllib3"] + assert len(urllib3_spans) == 0 + + def test_internal_span_creation_with_url_in_path(self) -> None: + internal_url_path = "https://example.com/com.instana/api/test" + + with tracer.start_as_current_span("test"): + try: + self.http.request("GET", internal_url_path, retries=False, timeout=1) + except Exception: + pass + + spans = self.recorder.queued_spans() + + assert len(spans) == 1 + + test_span = spans[0] + assert test_span.data["sdk"]["name"] == "test" + + urllib3_spans = [span for span in spans if span.n == "urllib3"] + assert len(urllib3_spans) == 0