-
Notifications
You must be signed in to change notification settings - Fork 381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DEBUG-2334 duplicate mutable values when serializing for dynamic inst… #4009
Conversation
a606869
to
604ed08
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #4009 +/- ##
==========================================
- Coverage 97.86% 97.85% -0.01%
==========================================
Files 1319 1319
Lines 79157 79195 +38
Branches 3929 3932 +3
==========================================
+ Hits 77466 77499 +33
- Misses 1691 1696 +5 ☔ View full report in Codecov by Sentry. |
604ed08
to
c52a21c
Compare
…rumentation When serializing parameter values at method entry, the resulting snapshot should not change if the parameter is mutated during method execution. This currently only applies to string values as other values used as serializer output are not mutable. This commit duplicates the string values which achieves immutability with no additional API complexity but at the cost of duplicating all strings, even if they are not entry parameters and thus can be stored without duplication.
c52a21c
to
95743ab
Compare
lib/datadog/di/serializer.rb
Outdated
when String, Symbol | ||
value = value.to_s | ||
value = case value | ||
when String | ||
# This is the only place where we duplicate the value, currently. | ||
# All other values are immutable primitives (e.g. numbers). | ||
value.dup | ||
else | ||
value.to_s | ||
end | ||
max = settings.dynamic_instrumentation.max_capture_string_length | ||
if value.length > max | ||
serialized.update(truncated: true, size: value.length) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be worth using https://github.com/DataDog/dd-trace-rb/blob/master/lib/datadog/core/utils/safe_dup.rb or -v
directly? 🤔
Also, it may be worth checking if truncation needs to happen first, otherwise we'll be creating yet another copy for the truncation 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, if the string is to be truncated there is no need to dup it, and for frozen strings there is also no need to dup since they can't be mutated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left a comment in case we can reuse the existing util.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏼
lib/datadog/di/serializer.rb
Outdated
value = case value | ||
when String | ||
# This is the only place where we duplicate the value, currently. | ||
# All other values are immutable primitives (e.g. numbers). | ||
# However, do not duplicate if the string is frozen, or if | ||
# it is later truncated. | ||
need_dup = !value.frozen? | ||
value | ||
else | ||
value.to_s | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can turn case
statement into if
because it's just it
value = case value | |
when String | |
# This is the only place where we duplicate the value, currently. | |
# All other values are immutable primitives (e.g. numbers). | |
# However, do not duplicate if the string is frozen, or if | |
# it is later truncated. | |
need_dup = !value.frozen? | |
value | |
else | |
value.to_s | |
end | |
value = if value.is_a?(String) | |
# This is the only place where we duplicate the value, currently. | |
# All other values are immutable primitives (e.g. numbers). | |
# However, do not duplicate if the string is frozen, or if | |
# it is later truncated. | |
need_dup = !value.frozen? | |
value | |
else | |
value.to_s | |
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced case with if.
* origin/master: (51 commits) DEBUG-2334 duplicate mutable values when serializing for dynamic inst… (DataDog#4009) DEBUG-2334 enforce probe type validity (DataDog#4013) [🤖] Update Latest Dependency: https://github.com/DataDog/dd-trace-rb/actions/runs/11421728295 Fix the argument to the telemetry forwarder command [🤖] Lock Dependency: https://github.com/DataDog/dd-trace-rb/actions/runs/11460992004 Add datadog gem to gemspec and remove from Gemfile Replace debase with datadog, and comment out gemspec tests Add datadog gem to Gemfile Remove debase gem from gemspec Use nix develop Use Ubuntu 24.04 by Arm Limited Fix vendored dependency case revert system-tests branch to main Changed RuleSampler initialization with ASM Standalone to Tracing::Component.build_sampler Rename AppSec::Event.add_tags to AppSec::Event.tag_and_keep! and move trace.keep in it Move appsec_standalone_reject? to AppSec namespace Replaced set_tag by set_metric for _dd.appsec.enabled and _dd.apm.enabled metrics Add correct sig to Datadog::AppSec::Event.add_tags and add_distributed_tags Update Unreleased Changelog Fix typo in AppSec::Event.add_tags spec ...
…rumentation
Change log entry
None needed, DI is not usable by customers yet.
What does this PR do?
When serializing parameter values at method entry, the resulting snapshot should not change if the parameter is mutated during method execution. This currently only applies to string values as other values used as serializer output are not mutable.
This PR duplicates the string values which achieves immutability with no additional API complexity but at the cost of duplicating all strings, even if they are not entry parameters and thus can be stored without duplication.
Motivation:
Correct capture of method arguments at method entry.
Additional Notes:
How to test the change?
Unit tests at this time.
Unsure? Have a question? Request a review!