Skip to content

feat(instrumentation): await callbacks at function start - #10123

Merged
juan-fernandez merged 3 commits into
masterfrom
juan-fernandez/orchestrion-async-context-callback-at-function-start
Sep 4, 2026
Merged

juan-fernandez merged 3 commits into
masterfrom
juan-fernandez/orchestrion-async-context-callback-at-function-start

Conversation

@juan-fernandez

Copy link
Copy Markdown
Collaborator

What does this PR do?

Adds awaitContextCallbackAtFunctionStart, a rewriter transform that awaits a tracing-context callback at the start of an instrumented async function.

It uses the same callback validation, tracing-wrapper validation, callbackThis handling, idempotency check, and failure-isolated invocation as the existing callback transforms.

Motivation

The existing transforms only support conditional branches and the start of a try block. They cannot insert a callback before statements that appear before that try.

For example:

await runBeforeHooks()
try {
  await runTest()
} catch (error) {
  // existing error handling
}

awaitContextCallbackAtTryStart runs after runBeforeHooks(). The new transform can run before runBeforeHooks() while keeping the generated callback isolated from application failures:

try {
  await contextCallback()
} catch {}

await runBeforeHooks()

Additional Notes

This PR contains generic rewriter functionality only. The WebdriverIO and RUM use case remains in stacked draft PR #10049.

Validation:

  • ./node_modules/.bin/mocha packages/datadog-instrumentations/test/helpers/rewriter/index.spec.js (40 passing)
  • Focused ESLint for the changed rewriter files

@dd-octo-sts

dd-octo-sts Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Overall package size

Self size: 8.74 MB
Deduped: 9.41 MB
No deduping: 9.41 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.4.0 | 127.33 kB | 447.04 kB | | opentracing | 0.14.7 | 194.81 kB | 194.81 kB | | dc-polyfill | 0.1.11 | 25.74 kB | 25.74 kB |

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

@pr-commenter

pr-commenter Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-09-03 08:18:15

Comparing candidate commit 35e6b9c in PR branch juan-fernandez/orchestrion-async-context-callback-at-function-start with baseline commit 6e65a91 in branch master.

📊 Benchmarking dashboard

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

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

Unstable benchmarks

These benchmarks have a confidence interval too wide to call a change; treat them as noise rather than signal.

scenario:appsec-iast-no-vulnerability-iast-enabled-default-config-20

  • unstable max_rss_usage [-20.021MB; +24.786MB] or [-8.803%; +10.897%]

scenario:appsec-iast-no-vulnerability-iast-enabled-default-config-24

  • unstable max_rss_usage [-26.589MB; +42.823MB] or [-8.075%; +13.006%]

scenario:appsec-iast-with-vulnerability-iast-enabled-default-config-20

  • unstable max_rss_usage [-27973.690KB; +28652.090KB] or [-7.650%; +7.835%]

scenario:debugger-line-probe-with-snapshot-minimal-24

  • unstable max_rss_usage [-11.730MB; +15.519MB] or [-4.659%; +6.164%]

scenario:dogstatsd-with-tags-20

  • unstable cpu_user_time [-518.591ms; +131.662ms] or [-10.373%; +2.634%]
  • unstable execution_time [-522.153ms; +129.516ms] or [-10.274%; +2.548%]
  • unstable throughput [-44469.160op/s; +170967.351op/s] or [-2.692%; +10.352%]

scenario:encoders-0.4-immediate-flush-20

  • unstable max_rss_usage [-10.317MB; +7.503MB] or [-13.248%; +9.633%]

scenario:iast-sink-check-20

  • unstable execution_time [-195.939ms; +263.413ms] or [-4.594%; +6.177%]

scenario:llmobs-encode-unicode-mixed-20

  • unstable max_rss_usage [-10.983MB; +7.681MB] or [-14.707%; +10.285%]

scenario:plugin-claude-agent-sdk-compact-stream-scan-26

  • unstable cpu_usage_percentage [-8.123%; +3.004%]

scenario:plugin-graphql-long-with-depth-off-20

  • unstable max_rss_usage [-4.298MB; +12.807MB] or [-3.398%; +10.126%]

scenario:plugin-graphql-long-with-depth-on-max-20

  • unstable max_rss_usage [-4.228MB; +11.203MB] or [-2.955%; +7.830%]

scenario:plugin-graphql-long-with-depth-on-max-26

  • unstable max_rss_usage [-16.987MB; +11.777MB] or [-7.383%; +5.119%]

scenario:test-optimization-large-suite-20

  • unstable max_rss_usage [-4.213MB; +7.837MB] or [-5.204%; +9.682%]

@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

Tests

✅ All CI checks and tests passed.

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
• Patch Coverage: 100.00%
• Overall Coverage: 98.63% (+0.00%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 35e6b9c | Docs | View more details | Give us feedback!

@codecov

codecov Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.63%. Comparing base (6e65a91) to head (35e6b9c).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master   #10123    +/-   ##
========================================
  Coverage   98.63%   98.63%            
========================================
  Files        1002     1002            
  Lines      154897   154945    +48     
  Branches    13358    13607   +249     
========================================
+ Hits       152779   152827    +48     
  Misses       2118     2118            
Flag Coverage Δ
aiguard 63.83% <0.00%> (-0.03%) ⬇️
aiguard-integration 58.91% <27.08%> (-0.04%) ⬇️
apm-capabilities-tracing 62.85% <0.00%> (-0.01%) ⬇️
apm-integrations-aerospike 61.24% <0.00%> (-0.03%) ⬇️
apm-integrations-aerospike-_integrat-1d185c06 63.21% <0.00%> (-0.03%) ⬇️
apm-integrations-confluentinc-kafka--d0bf0045 67.31% <0.00%> (-0.03%) ⬇️
apm-integrations-couchbase 61.73% <0.00%> (-0.03%) ⬇️
apm-integrations-elasticsearch_integ-31b69669 68.56% <0.00%> (-0.03%) ⬇️
apm-integrations-http 66.84% <0.00%> (-0.03%) ⬇️
apm-integrations-http2_integrations--9aea12f5 68.17% <0.00%> (-0.03%) ⬇️
apm-integrations-kafkajs 67.89% <0.00%> (-0.03%) ⬇️
apm-integrations-next 64.89% <0.00%> (-0.03%) ⬇️
apm-integrations-prisma 62.49% <0.00%> (-0.03%) ⬇️
apm-integrations-restify_integration-b312dd16 65.02% <0.00%> (-0.03%) ⬇️
appsec 77.30% <0.00%> (-0.03%) ⬇️
appsec-express_fastify_graphql 72.65% <27.08%> (-0.06%) ⬇️
appsec-integration 49.53% <27.08%> (-0.02%) ⬇️
appsec-kafka_ldapjs_lodash 67.98% <0.00%> (-0.03%) ⬇️
appsec-mongodb-core_mongoose_mysql 71.12% <0.00%> (-0.03%) ⬇️
appsec-next 57.69% <0.00%> (-0.02%) ⬇️
appsec-node-serialize_passport_postgres 70.60% <0.00%> (-0.03%) ⬇️
appsec-sourcing_stripe_template 69.05% <0.00%> (-0.03%) ⬇️
debugger 69.76% <0.00%> (-0.03%) ⬇️
instr-ai_instrumentation-anthropic-l-e862da55 56.60% <0.00%> (-0.03%) ⬇️
instr-bluebird_instrumentation-body--02ae4b41 64.84% <0.00%> (-0.03%) ⬇️
instr-confluentinc-kafka-javascript 56.00% <0.00%> (-0.03%) ⬇️
instr-connect_instrumentation-cookie-0bbb44d9 57.70% <0.00%> (-0.03%) ⬇️
instr-couchbase 55.22% <0.00%> (-0.03%) ⬇️
instr-cypress-config_instrumentation-4db1de46 58.02% <0.00%> (-0.03%) ⬇️
instr-express-multi-version_instrume-f5caf487 64.30% <0.00%> (-0.03%) ⬇️
instr-fetch_instrumentation-fs_instr-43dfe6a2 50.55% <0.00%> (-0.03%) ⬇️
instr-hono_instrumentation-http_inst-026a5d4d 66.16% <0.00%> (-0.03%) ⬇️
instr-kafkajs_instrumentation-knex_i-ca6f5b19 56.65% <0.00%> (-0.03%) ⬇️
instr-light-my-request_instrumentati-8c9fceca 63.62% <0.00%> (-0.03%) ⬇️
instr-mysql_instrumentation-mysql2_i-05f94c42 59.11% <0.00%> (-0.03%) ⬇️
instr-openai-lifecycle_instrumentati-783580bd 65.96% <0.00%> (-0.03%) ⬇️
instr-passport-http_instrumentation--2b6c8eb6 66.58% <0.00%> (-0.03%) ⬇️
instr-promise_instrumentation-promis-8479bae3 56.43% <0.00%> (-0.03%) ⬇️
instr-restify_instrumentation-router-5641467c 56.58% <0.00%> (-0.03%) ⬇️
instr-url_instrumentation-when_instr-22ca7078 56.46% <0.00%> (-0.03%) ⬇️
instrumentations-integration-esbuild 34.68% <0.00%> (-0.01%) ⬇️
llmobs-ai_anthropic_bedrock 66.79% <27.08%> (-0.06%) ⬇️
llmobs-claude-agent-sdk_google-genai-da2c851e 64.95% <27.08%> (-0.06%) ⬇️
llmobs-openai 66.76% <27.08%> (-0.07%) ⬇️
llmobs-openai-agents_vertex-ai 64.09% <27.08%> (-0.08%) ⬇️
llmobs-sdk 76.58% <0.00%> (-0.03%) ⬇️
master-coverage 98.63% <100.00%> (?)
openfeature 60.12% <0.00%> (-0.01%) ⬇️
openfeature-unit 58.85% <0.00%> (-0.03%) ⬇️
platform-core_esbuild_instrumentations-misc 41.38% <100.00%> (+0.02%) ⬆️
platform-integration 65.22% <0.00%> (-0.01%) ⬇️
platform-shimmer_unit-guardrails_webpack 39.10% <0.00%> (-0.02%) ⬇️
plugins-aws-durable-execution-sdk-js-2bc88c07 61.09% <27.08%> (-0.06%) ⬇️
plugins-azure-event-hubs_azure-servi-50d5b0fc 58.53% <0.00%> (-0.01%) ⬇️
plugins-browser-bunyan_bullmq_cassandra 66.32% <27.08%> (-0.08%) ⬇️
plugins-cookie_cookie-parser_crypto 56.20% <0.00%> (-0.03%) ⬇️
plugins-dd-trace-api_express-mongo-s-83827a5c 61.79% <0.00%> (-0.03%) ⬇️
plugins-fastify_fetch_fs 66.01% <0.00%> (+<0.01%) ⬆️
plugins-generic-pool_google-cloud-pubsub_grpc 69.55% <0.00%> (-0.03%) ⬇️
plugins-handlebars_hapi_hono 64.27% <0.00%> (-0.03%) ⬇️
plugins-ioredis_knex_langgraph 61.93% <27.08%> (-0.07%) ⬇️
plugins-ldapjs_light-my-request_limitd-client 64.00% <0.00%> (-0.02%) ⬇️
plugins-lodash_mariadb_memcached 64.45% <0.00%> (-0.03%) ⬇️
plugins-mercurius_microgateway-core_-b9bd83c4 67.79% <27.08%> (-0.07%) ⬇️
plugins-moleculer_mongodb_mongodb-core 66.71% <0.00%> (-0.03%) ⬇️
plugins-mongoose_multer_mysql 64.56% <0.00%> (-0.03%) ⬇️
plugins-mysql2_nats_node-serialize 66.79% <0.00%> (-0.03%) ⬇️
plugins-opensearch_passport-http_pino 64.69% <0.00%> (-0.03%) ⬇️
plugins-postgres_process_pug 63.74% <0.00%> (-0.03%) ⬇️
plugins-redis_router_sequelize 67.28% <0.00%> (-0.03%) ⬇️
plugins-test-and-upstream-amqp10_tes-914101e2 65.46% <27.08%> (-0.07%) ⬇️
plugins-test-and-upstream-avsc_test--408e8d3a 65.61% <0.00%> (-0.03%) ⬇️
plugins-test-and-upstream-graphql_te-59dd94de 68.80% <27.08%> (-0.07%) ⬇️
plugins-test-and-upstream-rhea_undici_url 66.60% <0.00%> (-0.03%) ⬇️
plugins-valkey_vm_winston 63.07% <0.00%> (-0.03%) ⬇️
plugins-ws 65.12% <0.00%> (-0.03%) ⬇️
profiling 66.68% <0.00%> (-0.03%) ⬇️
serverless-aws-sdk-aws-sdk 55.96% <0.00%> (-0.03%) ⬇️
serverless-aws-sdk-base-inject-field 55.77% <0.00%> (-0.03%) ⬇️
serverless-aws-sdk-bedrockruntime 59.31% <0.00%> (-0.03%) ⬇️
serverless-aws-sdk-client 60.67% <0.00%> (-0.03%) ⬇️
serverless-aws-sdk-dynamodb 59.74% <0.00%> (-0.03%) ⬇️
serverless-aws-sdk-eventbridge 58.14% <0.00%> (-0.03%) ⬇️
serverless-aws-sdk-kinesis 63.69% <0.00%> (-0.03%) ⬇️
serverless-aws-sdk-lambda 61.67% <0.00%> (-0.03%) ⬇️
serverless-aws-sdk-s3 59.79% <0.00%> (-0.03%) ⬇️
serverless-aws-sdk-serverless-peer-service 64.25% <0.00%> (-0.03%) ⬇️
serverless-aws-sdk-sns 64.57% <0.00%> (-0.03%) ⬇️
serverless-aws-sdk-sqs 65.03% <0.00%> (-0.03%) ⬇️
serverless-aws-sdk-stepfunctions 59.65% <0.00%> (-0.03%) ⬇️
serverless-aws-sdk-util 56.40% <0.00%> (-0.03%) ⬇️
serverless-azure-durable-functions_a-776237ee 57.65% <27.08%> (-0.04%) ⬇️
serverless-azure-functions-eventhubs-61ee6313 64.32% <0.00%> (-0.03%) ⬇️
test-optimization-cucumber 70.41% <0.00%> (+0.01%) ⬆️
test-optimization-cypress 64.82% <0.00%> (-0.12%) ⬇️
test-optimization-jest 71.90% <0.00%> (+0.01%) ⬆️
test-optimization-mocha 71.90% <0.00%> (+0.05%) ⬆️
test-optimization-playwright-playwri-363e40a6 59.69% <27.08%> (+0.08%) ⬆️
test-optimization-playwright-playwri-5141d36d 61.20% <27.08%> (-0.05%) ⬇️
test-optimization-playwright-playwri-5b35813b 59.57% <27.08%> (-0.05%) ⬇️
test-optimization-playwright-playwri-924f4516 60.90% <27.08%> (+<0.01%) ⬆️
test-optimization-playwright-playwri-d6f5a99b 59.45% <27.08%> (+<0.01%) ⬆️
test-optimization-playwright-playwright-atr 59.18% <27.08%> (-0.05%) ⬇️
test-optimization-playwright-playwright-efd 60.10% <27.08%> (-0.05%) ⬇️
test-optimization-selenium 58.79% <0.00%> (-0.06%) ⬇️
test-optimization-testopt 61.42% <27.08%> (-0.01%) ⬇️
test-optimization-vitest 72.48% <0.00%> (+0.08%) ⬆️
test-optimization-vitest-browser 58.83% <27.08%> (+0.47%) ⬆️
test-optimization-webdriverio 65.85% <27.08%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.

@juan-fernandez
juan-fernandez marked this pull request as ready for review September 3, 2026 07:49
@juan-fernandez
juan-fernandez requested review from a team as code owners September 3, 2026 07:49
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-03T08:11:41.597317Z 35e6b9c Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@juan-fernandez

Copy link
Copy Markdown
Collaborator Author

/datadog autotest review
@codex review

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Datadog Autotest: FAIL

The new transform inserts callback statements before a function directive. This action can disable local strict mode.

Open Bits AI session

🤖 Datadog Autotest · Commit 572c8ff · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

Comment thread packages/datadog-instrumentations/src/helpers/rewriter/transforms.js Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 572c8ff620

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/datadog-instrumentations/src/helpers/rewriter/transforms.js Outdated
Comment thread packages/datadog-instrumentations/src/helpers/rewriter/transforms.js Outdated
@juan-fernandez

Copy link
Copy Markdown
Collaborator Author

/datadog autotest review
@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 35e6b9c1af

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Datadog Autotest: PASS

More details

The PR adds a transform that awaits a context callback at function start.
The latest commit handles direct function-node queries.
It also preserves directive prologues before callback insertion.
The earlier strict-mode and skipped-rewrite issues are fixed.
No open findings remain.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Datadog Autotest · Commit 35e6b9c · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

@rochdev rochdev left a comment

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.

Approving to unblock, but could you look into a follow-up to see if maybe all of the different awaitContextX could be combined somehow? Maybe instead of targeting the wrapping function it could target the block or statement itself? Then a single transform would be usable in any case instead of needing special casing for every blocking scenario.

@juan-fernandez
juan-fernandez merged commit c4423eb into master Sep 4, 2026
693 checks passed
@juan-fernandez
juan-fernandez deleted the juan-fernandez/orchestrion-async-context-callback-at-function-start branch September 4, 2026 08:13
@juan-fernandez

Copy link
Copy Markdown
Collaborator Author

Follow-up opened as draft PR #10157, starting from master. It replaces the function-start and try-start variants with one awaitContextCallback transform that targets the actual BlockStatement, while preserving the IfStatement condition recheck behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants