Skip to content
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

Merged
merged 4 commits into from
Oct 23, 2024

Conversation

p-datadog
Copy link
Member

…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!

@p-datadog p-datadog requested a review from a team as a code owner October 17, 2024 16:06
@codecov-commenter
Copy link

codecov-commenter commented Oct 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.85%. Comparing base (f8d7b5e) to head (6b1ee84).

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.
📢 Have feedback on the report? Share it here.

@pr-commenter
Copy link

pr-commenter bot commented Oct 17, 2024

Benchmarks

Benchmark execution time: 2024-10-23 14:45:58

Comparing candidate commit 6b1ee84 in PR branch di-serializer-copy with baseline commit f8d7b5e in branch master.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 24 metrics, 2 unstable metrics.

…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.
Comment on lines 104 to 115
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)
Copy link
Member

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 👀

Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member

@marcotc marcotc left a 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.

Copy link
Member

@Strech Strech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

Comment on lines 106 to 116
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
Copy link
Member

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

Suggested change
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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced case with if.

@p-datadog p-datadog merged commit ee50bd2 into master Oct 23, 2024
269 of 270 checks passed
@p-datadog p-datadog deleted the di-serializer-copy branch October 23, 2024 15:06
@github-actions github-actions bot added this to the 2.5.0 milestone Oct 23, 2024
p-datadog pushed a commit to p-datadog/dd-trace-rb that referenced this pull request Oct 23, 2024
* 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
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants