Skip to content

Commit 4fffa77

Browse files
ZStriker19juanjuxbrettlangdon
authored
feat(asm): add the DD_TRACE_CLIENT_IP_ENABLED env var to fetch request IPs with ASM disabled (#4971)
Backport of #4949 to 1.8 +tests. Signed-off-by: Juanjo Alvarez <[email protected]> Signed-off-by: Juanjo Alvarez <[email protected]> Co-authored-by: Brett Langdon <[email protected]> ## Description <!-- Briefly describe the change and why it was required. --> <!-- If this is a breaking change, explain why it is necessary. Breaking changes must append `!` after the type/scope. See https://ddtrace.readthedocs.io/en/stable/contributing.html for more details. --> ## Checklist - [ ] Followed the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines) when writing a release note. - [ ] Add additional sections for `feat` and `fix` pull requests. - [ ] [Library documentation](https://github.com/DataDog/dd-trace-py/tree/1.x/docs) and/or [Datadog's documentation site](https://github.com/DataDog/documentation/) is updated. Link to doc PR in description. <!-- Copy and paste the relevant snippet based on the type of pull request --> <!-- START feat --> ## Motivation <!-- Expand on why the change is required, include relevant context for reviewers --> ## Design <!-- Include benefits from the change as well as possible drawbacks and trade-offs --> ## Testing strategy <!-- Describe the automated tests and/or the steps for manual testing. <!-- END feat --> <!-- START fix --> ## Relevant issue(s) <!-- Link the pull request to any issues related to the fix. Use keywords for links to automate closing the issues once the pull request is merged. --> ## Testing strategy <!-- Describe any added regression tests and/or the manual testing performed. --> <!-- END fix --> ## Reviewer Checklist - [ ] Title is accurate. - [ ] Description motivates each change. - [ ] No unnecessary changes were introduced in this PR. - [ ] Avoid breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [ ] Tests provided or description of manual testing performed is included in the code or PR. - [ ] Release note has been added and follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines), or else `changelog/no-changelog` label added. - [ ] All relevant GitHub issues are correctly linked. - [ ] Change contains telemetry where appropriate (logs, metrics, etc.). - [ ] Telemetry is meaningful, actionable and does not have the potential to leak sensitive data. Signed-off-by: Juanjo Alvarez <[email protected]> Co-authored-by: Juanjo Alvarez Martinez <[email protected]> Co-authored-by: Brett Langdon <[email protected]>
1 parent 8cf9dd9 commit 4fffa77

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

ddtrace/contrib/trace_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ def set_http_meta(
480480

481481
# We always collect the IP if appsec is enabled to report it on potential vulnerabilities.
482482
# https://datadoghq.atlassian.net/wiki/spaces/APS/pages/2118779066/Client+IP+addresses+resolution
483-
if config._appsec_enabled:
483+
if config._appsec_enabled or config.retrieve_client_ip:
484484
ip = _get_request_header_client_ip(span, request_headers, peer_ip, headers_are_case_sensitive)
485485
if ip:
486486
span.set_tag_str(http.CLIENT_IP, ip)

ddtrace/settings/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ def __init__(self):
195195
legacy_config_value = os.getenv("DD_ANALYTICS_ENABLED", default=False)
196196

197197
self.analytics_enabled = asbool(os.getenv("DD_TRACE_ANALYTICS_ENABLED", default=legacy_config_value))
198+
self.retrieve_client_ip = asbool(os.getenv("DD_TRACE_CLIENT_IP_ENABLED", default=False))
198199

199200
self.tags = parse_tags_str(os.getenv("DD_TAGS") or "")
200201

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
features:
2+
- |
3+
tracing: Add support for enabling collecting of HTTP request client IP addresses as the ``http.client_ip`` span tag. You can set the ``DD_TRACE_CLIENT_IP_ENABLED`` environment variable to ``true`` to enable. This feature is disabled by default.

tests/tracer/test_trace_utils.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,49 @@ def test_set_http_meta_headers_ip(
580580
mock_store_headers.assert_called()
581581

582582

583+
def test_set_http_meta_headers_ip_asm_disabled_env_default_false(span, int_config):
584+
with override_global_config(dict(_appsec_enabled=False)):
585+
int_config.myint.http._header_tags = {"enabled": True}
586+
assert int_config.myint.is_header_tracing_configured is True
587+
trace_utils.set_http_meta(
588+
span,
589+
int_config.myint,
590+
request_headers={"via": "8.8.8.8"},
591+
)
592+
result_keys = list(span.get_tags().keys())
593+
result_keys.sort(reverse=True)
594+
assert result_keys == ["runtime-id"]
595+
596+
597+
def test_set_http_meta_headers_ip_asm_disabled_env_false(span, int_config):
598+
with override_global_config(dict(_appsec_enabled=False, retrieve_client_ip=False)):
599+
int_config.myint.http._header_tags = {"enabled": True}
600+
assert int_config.myint.is_header_tracing_configured is True
601+
trace_utils.set_http_meta(
602+
span,
603+
int_config.myint,
604+
request_headers={"via": "8.8.8.8"},
605+
)
606+
result_keys = list(span.get_tags().keys())
607+
result_keys.sort(reverse=True)
608+
assert result_keys == ["runtime-id"]
609+
610+
611+
def test_set_http_meta_headers_ip_asm_disabled_env_true(span, int_config):
612+
with override_global_config(dict(_appsec_enabled=False, retrieve_client_ip=True)):
613+
int_config.myint.http._header_tags = {"enabled": True}
614+
assert int_config.myint.is_header_tracing_configured is True
615+
trace_utils.set_http_meta(
616+
span,
617+
int_config.myint,
618+
request_headers={"via": "8.8.8.8"},
619+
)
620+
result_keys = list(span.get_tags().keys())
621+
result_keys.sort(reverse=True)
622+
assert result_keys == ["runtime-id", "network.client.ip", http.CLIENT_IP]
623+
assert span.get_tag(http.CLIENT_IP) == "8.8.8.8"
624+
625+
583626
def test_ip_subnet_regression():
584627
del_ip = "1.2.3.4/32"
585628
req_ip = "10.2.3.4"

tests/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def override_global_config(values):
8686
# DEV: We do not do `ddtrace.config.keys()` because we have all of our integrations
8787
global_config_keys = [
8888
"analytics_enabled",
89+
"retrieve_client_ip",
8990
"report_hostname",
9091
"health_metrics_enabled",
9192
"_propagation_style_extract",

0 commit comments

Comments
 (0)