Skip to content

[DI] Add support for probe condition#5488

Merged
watson merged 33 commits intomasterfrom
watson/DEBUG-2626/probe-condition
Apr 1, 2025
Merged

[DI] Add support for probe condition#5488
watson merged 33 commits intomasterfrom
watson/DEBUG-2626/probe-condition

Conversation

@watson
Copy link
Copy Markdown
Collaborator

@watson watson commented Mar 26, 2025

What does this PR do?

Add support for having a condition on a probe.

A condition should return either true or false. It can however also throw an error. The error can be thrown in two places:

  1. When compiling the AST down to the JavaScript source code (an issue was detected at compilation-time)
  2. When running the condition in the context of the breakpoint (an issue was detected at run-time)

Compilation-time exceptions are reported as an error event on the probe to the DI diagnostics backend. Run-time exceptions are currently just swallowed and the result of the condition is going to be the same as if it evaluated false (i.e. it's not going to tigger the breakpoint).

To be 100% compatible with how conditions work in the other tracers, we should technically record the run-time errors and expose those to the user (as a help in debugging the condition). However, due to how conditions are implemented in this PR (by using the condition property on the Debugger.setBreakpoint Chrome DevTools Protocol API), it's not possible to know if a condition failed, or just returned false.

For now, this is an ok compromise, due to the increased performance gained by using this API.

Motivation

  1. Feature parity with the other tracers
  2. Improved performance for probes on hot code-paths that doesn't need to trigger all the time

Plugin Checklist

Additional Notes

@watson watson self-assigned this Mar 26, 2025
Copy link
Copy Markdown
Collaborator Author

watson commented Mar 26, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@watson watson added semver-minor debugger Dynamic Instrumentation & Live Debugger labels Mar 26, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 26, 2025

Overall package size

Self size: 9.19 MB
Deduped: 101.72 MB
No deduping: 102.23 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | @datadog/libdatadog | 0.5.0 | 29.83 MB | 29.83 MB | | @datadog/native-appsec | 8.5.1 | 19.26 MB | 19.27 MB | | @datadog/native-iast-taint-tracking | 3.3.0 | 13.77 MB | 13.78 MB | | @datadog/pprof | 5.6.0 | 9.79 MB | 10.16 MB | | @opentelemetry/core | 1.30.1 | 908.66 kB | 7.16 MB | | protobufjs | 7.4.0 | 2.77 MB | 5.42 MB | | @datadog/native-iast-rewriter | 2.8.0 | 2.6 MB | 2.74 MB | | @datadog/native-metrics | 3.1.0 | 1.06 MB | 1.46 MB | | @opentelemetry/api | 1.8.0 | 1.21 MB | 1.21 MB | | import-in-the-middle | 1.13.1 | 117.64 kB | 839.26 kB | | source-map | 0.7.4 | 226 kB | 226 kB | | opentracing | 0.14.7 | 194.81 kB | 194.81 kB | | lru-cache | 7.18.3 | 133.92 kB | 133.92 kB | | pprof-format | 2.1.0 | 111.69 kB | 111.69 kB | | @datadog/sketches-js | 2.1.1 | 109.9 kB | 109.9 kB | | lodash.sortby | 4.7.0 | 75.76 kB | 75.76 kB | | ignore | 5.3.2 | 53.63 kB | 53.63 kB | | istanbul-lib-coverage | 3.2.0 | 29.34 kB | 29.34 kB | | rfdc | 1.4.1 | 27.15 kB | 27.15 kB | | @isaacs/ttlcache | 1.4.1 | 25.2 kB | 25.2 kB | | tlhunter-sorted-set | 0.1.0 | 24.94 kB | 24.94 kB | | dc-polyfill | 0.1.6 | 24.56 kB | 24.56 kB | | shell-quote | 1.8.2 | 23.54 kB | 23.54 kB | | limiter | 1.1.5 | 23.17 kB | 23.17 kB | | retry | 0.13.1 | 18.85 kB | 18.85 kB | | semifies | 1.0.0 | 15.84 kB | 15.84 kB | | jest-docblock | 29.7.0 | 8.99 kB | 12.76 kB | | crypto-randomuuid | 1.0.0 | 11.18 kB | 11.18 kB | | ttl-set | 1.0.0 | 4.61 kB | 9.69 kB | | path-to-regexp | 0.1.12 | 6.6 kB | 6.6 kB | | koalas | 1.0.2 | 6.47 kB | 6.47 kB | | module-details-from-path | 1.0.3 | 4.47 kB | 4.47 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 26, 2025

Codecov Report

Attention: Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 79.18%. Comparing base (b4b2969) to head (6a8f5f0).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
packages/dd-trace/src/debugger/index.js 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5488      +/-   ##
==========================================
- Coverage   79.18%   79.18%   -0.01%     
==========================================
  Files         512      512              
  Lines       23156    23158       +2     
==========================================
+ Hits        18337    18338       +1     
- Misses       4819     4820       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Fixed
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Fixed
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Fixed
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Fixed
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Fixed
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Fixed
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Fixed
@datadog-datadog-prod-us1
Copy link
Copy Markdown

datadog-datadog-prod-us1 Bot commented Mar 26, 2025

Datadog Report

Branch report: watson/DEBUG-2626/probe-condition
Commit report: 93248f1
Test service: dd-trace-js-integration-tests

✅ 0 Failed, 919 Passed, 0 Skipped, 14m 6.04s Total Time

@pr-commenter
Copy link
Copy Markdown

pr-commenter Bot commented Mar 26, 2025

Benchmarks

Benchmark execution time: 2025-04-01 11:53:56

Comparing candidate commit 6a8f5f0 in PR branch watson/DEBUG-2626/probe-condition with baseline commit b4b2969 in branch master.

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

shatzi
shatzi previously approved these changes Mar 26, 2025
Copy link
Copy Markdown

@shatzi shatzi left a comment

Choose a reason for hiding this comment

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

awesome progress

Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Outdated
Comment thread packages/dd-trace/src/debugger/devtools_client/breakpoints.js Outdated
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Outdated
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Outdated
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

should this be under Array.isArray(value) at line 39 ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Is it because value[1] should also be compiled?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nope. because you access it as an array...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Under the Array.isArray(value) if-block below, I also compile the second element of the value array. If we don't want that in this case, it shouldn't be part of that if-block

Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Outdated
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

does the spec allow the user to have '@it' iterator over entities? not sure if that something we need to support..

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've been wondering the same thing. I came to the conclusion that shouldn't be supported based on this discussion from January, but maybe I misunderstood it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

open to interpretation. I would say: @it = [@key, @value] as entires return pairs.

another thing I don't like about the implementation is that @key get encoded as key. so if someone would like to write something: contains(keys, { @key == key }) it will fail. can we make @it, @key and @value unique so they can't override existing locals? or maybe add a long prefix like $__it or something crazy like that... I wish we can name variables using symbols but I don't think this is a thing....

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Consensus from Zoom: We'll keep the current implementation

Comment thread packages/dd-trace/test/debugger/devtools_client/condition.spec.js Outdated
Comment thread packages/dd-trace/test/debugger/devtools_client/condition.spec.js Outdated
Comment thread packages/dd-trace/test/debugger/devtools_client/condition.spec.js Outdated
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Fixed
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Fixed
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Fixed
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Fixed
@watson watson force-pushed the watson/DEBUG-2626/probe-condition branch 2 times, most recently from d5189b6 to 948d0fb Compare March 26, 2025 21:17
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Outdated
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

all those errors should be more helpful for the user to understand the problem.
something like Possibility of side effect accessing when evaluate ${stringifyExpression(node)}

However, we might want to address this on future PRs

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This only truly becomes relevant when implementing template strings, as any errors thrown in conditions will just treat the condition as "not met" - but the error itself will be swallowed.

I'll see if I can improve it in this PR, or I will defer it till I implement support for template strings.

@watson watson force-pushed the watson/DEBUG-2626/probe-condition branch from 948d0fb to 625ff93 Compare March 29, 2025 12:17
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Dismissed
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Dismissed
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Dismissed
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Fixed
@watson watson requested a review from shatzi March 30, 2025 10:09
@watson watson marked this pull request as ready for review March 30, 2025 16:22
@watson watson requested review from a team as code owners March 30, 2025 16:22
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Outdated
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Outdated
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Outdated
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Outdated
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Outdated
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Outdated
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Outdated
shatzi
shatzi previously approved these changes Mar 31, 2025
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Outdated
@watson watson force-pushed the watson/DEBUG-2626/probe-condition branch from 50bb8a6 to 69d6713 Compare April 1, 2025 06:15
Comment thread packages/dd-trace/src/debugger/devtools_client/condition.js Dismissed
})

it('should report error if condition is invalid', function (done) {
t.agent.on('debugger-diagnostics', ({ payload }) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For future reference: using EventEmitter's once() would allow the tests to likely be nicer to read :)

@watson watson requested review from BridgeAR and shatzi April 1, 2025 13:06
@watson watson merged commit d8677ee into master Apr 1, 2025
427 checks passed
@watson watson deleted the watson/DEBUG-2626/probe-condition branch April 1, 2025 15:29
wconti27 pushed a commit that referenced this pull request Apr 8, 2025
Add support for having a condition on a probe.

A condition should return either `true` or `false`. It can however also throw
an error. The error can be thrown in two places:

1. When compiling the AST down to the JavaScript source code (an issue was
   detected at compilation-time)
2. When running the condition in the context of the breakpoint (an issue was
   detected at run-time)

Compilation-time exceptions are reported as an error event on the probe to the
DI diagnostics backend. Run-time exceptions are currently just swallowed and
the result of the condition is going to be the same as if it evaluated `false`
(i.e. it's not going to tigger the breakpoint).

To be 100% compatible with how conditions work in the other tracers, we should
technically record the run-time errors and expose those to the user (as a help
in debugging the condition). However, due to how conditions are implemented in
this PR (by using the `condition` property on the `Debugger.setBreakpoint`
Chrome DevTools Protocol API), it's not possible to know if a condition failed,
or just returned `false`.

For now, this is an ok compromise, due to the increased performance gained by
using this API.
@wconti27 wconti27 mentioned this pull request Apr 8, 2025
wconti27 pushed a commit that referenced this pull request Apr 9, 2025
Add support for having a condition on a probe.

A condition should return either `true` or `false`. It can however also throw
an error. The error can be thrown in two places:

1. When compiling the AST down to the JavaScript source code (an issue was
   detected at compilation-time)
2. When running the condition in the context of the breakpoint (an issue was
   detected at run-time)

Compilation-time exceptions are reported as an error event on the probe to the
DI diagnostics backend. Run-time exceptions are currently just swallowed and
the result of the condition is going to be the same as if it evaluated `false`
(i.e. it's not going to tigger the breakpoint).

To be 100% compatible with how conditions work in the other tracers, we should
technically record the run-time errors and expose those to the user (as a help
in debugging the condition). However, due to how conditions are implemented in
this PR (by using the `condition` property on the `Debugger.setBreakpoint`
Chrome DevTools Protocol API), it's not possible to know if a condition failed,
or just returned `false`.

For now, this is an ok compromise, due to the increased performance gained by
using this API.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

debugger Dynamic Instrumentation & Live Debugger semver-minor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants