Skip to content

Commit 716b940

Browse files
fix(asm): fix manual keep for user event [backport 1.16] (#7300)
Backport from #7296 to 1.16. Partial backport of #6633 to fix user events being dropped by the tracer ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [x] Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) - [x] If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from `@DataDog/security-design-and-guidance`. - [x] This PR doesn't touch any of that.
1 parent 49240ca commit 716b940

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

ddtrace/appsec/trace_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _track_user_login_common(tracer, user_id, success, metadata=None, login_even
4242
for k, v in six.iteritems(metadata):
4343
span.set_tag_str("%s.%s.%s" % (APPSEC.USER_LOGIN_EVENT_PREFIX, success_str, k), str(v))
4444

45-
span.set_tag_str(constants.MANUAL_KEEP_KEY, "true")
45+
span.set_tag(constants.MANUAL_KEEP_KEY)
4646
return span
4747
else:
4848
log.warning(
@@ -137,7 +137,7 @@ def track_custom_event(tracer, event_name, metadata):
137137

138138
for k, v in six.iteritems(metadata):
139139
span.set_tag_str("%s.%s.%s" % (APPSEC.CUSTOM_EVENT_PREFIX, event_name, k), str(v))
140-
span.set_tag_str(constants.MANUAL_KEEP_KEY, "true")
140+
span.set_tag(constants.MANUAL_KEEP_KEY)
141141

142142

143143
def should_block_user(tracer, userid): # type: (Tracer, str) -> bool
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
ASM: This fix resolves an issue where user events spans could be sampled erroneously.

tests/appsec/test_appsec_trace_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_track_user_login_event_success_without_metadata(self):
5050
assert root_span.get_tag("%s.sdk" % success_prefix) == "true"
5151
assert not root_span.get_tag("%s.auto.mode" % success_prefix)
5252
assert not root_span.get_tag("%s.track" % failure_prefix)
53-
assert root_span.get_tag(constants.MANUAL_KEEP_KEY) == "true"
53+
assert root_span.context.sampling_priority == constants.USER_KEEP
5454
# set_user tags
5555
assert root_span.get_tag(user.ID) == "1234"
5656
assert root_span.get_tag(user.NAME) == "John"
@@ -75,7 +75,7 @@ def test_track_user_login_event_success_auto_mode_safe(self):
7575

7676
root_span = self.tracer.current_root_span()
7777
success_prefix = "%s.success" % APPSEC.USER_LOGIN_EVENT_PREFIX
78-
assert root_span.get_tag("%s.track" % success_prefix) == "true"
78+
assert root_span.context.sampling_priority == constants.USER_KEEP
7979
assert not root_span.get_tag("%s.sdk" % success_prefix)
8080
assert root_span.get_tag("%s.auto.mode" % success_prefix) == str(LOGIN_EVENTS_MODE.SAFE)
8181

@@ -108,7 +108,7 @@ def test_track_user_login_event_success_with_metadata(self):
108108
assert root_span.get_tag("%s.sdk" % success_prefix) == "true"
109109
assert not root_span.get_tag("%s.auto.mode" % success_prefix)
110110
assert root_span.get_tag("%s.foo" % success_prefix) == "bar"
111-
assert root_span.get_tag(constants.MANUAL_KEEP_KEY) == "true"
111+
assert root_span.context.sampling_priority == constants.USER_KEEP
112112
# set_user tags
113113
assert root_span.get_tag(user.ID) == "1234"
114114
assert not root_span.get_tag(user.NAME)
@@ -139,7 +139,7 @@ def test_track_user_login_event_failure_user_exists(self):
139139
assert root_span.get_tag("%s.%s" % (failure_prefix, user.ID)) == "1234"
140140
assert root_span.get_tag("%s.%s" % (failure_prefix, user.EXISTS)) == "true"
141141
assert root_span.get_tag("%s.foo" % failure_prefix) == "bar"
142-
assert root_span.get_tag(constants.MANUAL_KEEP_KEY) == "true"
142+
assert root_span.context.sampling_priority == constants.USER_KEEP
143143
# set_user tags: shouldn't have been called
144144
assert not root_span.get_tag(user.ID)
145145
assert not root_span.get_tag(user.NAME)

0 commit comments

Comments
 (0)