Skip to content

Commit 36fd507

Browse files
committed
feat:Modified internal span filtering logic according to new span-filtering mechanism
Signed-off-by: Cagri Yonca <cagri@ibm.com>
1 parent ea4735e commit 36fd507

File tree

5 files changed

+55
-29
lines changed

5 files changed

+55
-29
lines changed

src/instana/agent/host.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,14 +399,20 @@ def __is_endpoint_ignored(self, span_attributes: dict) -> bool:
399399
return False
400400

401401
# Check include rules
402-
for rule in filters.get("include", [{}]):
403-
if matches_rule(rule.get("attributes", []), span_attributes):
404-
return False
402+
include_rules = filters.get("include", [])
403+
if any(
404+
matches_rule(rule.get("attributes", []), span_attributes)
405+
for rule in include_rules
406+
):
407+
return False
405408

406409
# Check exclude rules
407-
for rule in filters.get("exclude", [{}]):
408-
if matches_rule(rule.get("attributes", []), span_attributes):
409-
return True
410+
exclude_rules = filters.get("exclude", [])
411+
if any(
412+
matches_rule(rule.get("attributes", []), span_attributes)
413+
for rule in exclude_rules
414+
):
415+
return True
410416

411417
return False
412418

src/instana/instrumentation/urllib3.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,7 @@ def urlopen_with_instana(
9494
tracer, parent_span, span_name = get_tracer_tuple()
9595

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

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

src/instana/options.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,36 @@ def set_trace_configurations(self) -> None:
134134

135135
self.set_disable_trace_configurations()
136136
self.set_stack_trace_configurations()
137+
if "INSTANA_ALLOW_INTERNAL_SPANS" not in os.environ:
138+
self.add_internal_span_filter()
139+
140+
def add_internal_span_filter(self) -> None:
141+
if "exclude" not in self.span_filters:
142+
self.span_filters["exclude"] = []
143+
self.span_filters["exclude"].extend(
144+
[
145+
{
146+
"name": "filter-internal-spans-by-url",
147+
"attributes": [
148+
{
149+
"key": "http.url",
150+
"values": ["com.instana"],
151+
"match_type": "contains",
152+
}
153+
],
154+
},
155+
{
156+
"name": "filter-internal-spans-by-host",
157+
"attributes": [
158+
{
159+
"key": "http.host",
160+
"values": ["com.instana"],
161+
"match_type": "contains",
162+
}
163+
],
164+
},
165+
]
166+
)
137167

138168
def _apply_env_stack_trace_config(self) -> None:
139169
"""Apply stack trace configuration from environment variables."""

tests/clients/test_urllib3.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,12 +1006,15 @@ def test_internal_span_creation_with_url_in_hostname(self) -> None:
10061006

10071007
spans = self.recorder.queued_spans()
10081008

1009-
assert len(spans) == 1
1009+
assert len(spans) == 2
1010+
1011+
filtered_spans = agent.filter_spans(spans)
1012+
assert len(filtered_spans) == 1
10101013

1011-
test_span = spans[0]
1014+
test_span = filtered_spans[0]
10121015
assert test_span.data["sdk"]["name"] == "test"
10131016

1014-
urllib3_spans = [span for span in spans if span.n == "urllib3"]
1017+
urllib3_spans = [span for span in filtered_spans if span.n == "urllib3"]
10151018
assert len(urllib3_spans) == 0
10161019

10171020
def test_internal_span_creation_with_url_in_path(self) -> None:
@@ -1024,11 +1027,13 @@ def test_internal_span_creation_with_url_in_path(self) -> None:
10241027
pass
10251028

10261029
spans = self.recorder.queued_spans()
1030+
assert len(spans) == 2
10271031

1028-
assert len(spans) == 1
1032+
filtered_spans = agent.filter_spans(spans)
1033+
assert len(filtered_spans) == 1
10291034

1030-
test_span = spans[0]
1035+
test_span = filtered_spans[0]
10311036
assert test_span.data["sdk"]["name"] == "test"
10321037

1033-
urllib3_spans = [span for span in spans if span.n == "urllib3"]
1038+
urllib3_spans = [span for span in filtered_spans if span.n == "urllib3"]
10341039
assert len(urllib3_spans) == 0

tests/test_options.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class TestBaseOptions:
2323
@pytest.fixture(autouse=True)
2424
def _resource(self) -> Generator[None, None, None]:
2525
self.base_options = None
26+
os.environ["INSTANA_ALLOW_INTERNAL_SPANS"] = "True"
2627
yield
2728
if "tracing" in config.keys():
2829
del config["tracing"]

0 commit comments

Comments
 (0)