Skip to content

Commit f583dd9

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 3a0affb commit f583dd9

File tree

5 files changed

+379
-68
lines changed

5 files changed

+379
-68
lines changed

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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,35 @@ def set_trace_configurations(self) -> None:
134134

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

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

src/instana/util/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def parse_filtered_endpoints_string(params: Union[str, os.PathLike]) -> List[str
8181
return span_filters
8282

8383

84-
def parse_filtered_endpoints_dict(filter_dict: dict[str, Any]) -> dict[str, list[Any]]:
84+
def parse_filtered_endpoints_dict(filter_dict: Dict[str, Any]) -> Dict[str, List[Any]]:
8585
"""
8686
Parses 'exclude' and 'include' blocks from the filter dict.
8787
@@ -134,7 +134,7 @@ def parse_filtered_endpoints_dict(filter_dict: dict[str, Any]) -> dict[str, list
134134

135135
def parse_filtered_endpoints(
136136
params: Union[Dict[str, Any], str],
137-
) -> Union[List[str], dict[str, list[Any]]]:
137+
) -> Union[List[str], Dict[str, List[Any]]]:
138138
"""
139139
Parses input to prepare a list for ignored endpoints.
140140
@@ -157,7 +157,7 @@ def parse_filtered_endpoints(
157157

158158
def parse_filtered_endpoints_from_yaml(
159159
file_path: str,
160-
) -> Union[List[str], dict[str, list[Any]]]:
160+
) -> Union[List[str], Dict[str, List[Any]]]:
161161
"""
162162
Parses configuration yaml file and prepares a list of ignored endpoints.
163163
@@ -175,7 +175,7 @@ def parse_filtered_endpoints_from_yaml(
175175
span_filters = parse_filtered_endpoints(span_filters_dict)
176176
return span_filters
177177
else:
178-
return []
178+
return {}
179179

180180

181181
def parse_span_filter_env_vars() -> Dict[str, List[Any]]:

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

0 commit comments

Comments
 (0)