Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/instana/instrumentation/urllib3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions tests/clients/test_urllib3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading