Skip to content

fix(tracer): preserve wrapped function metadata - #10133

Draft
BridgeAR wants to merge 3 commits into
masterfrom
BridgeAR/2026-09-03-tracer-wrap-arity
Draft

fix(tracer): preserve wrapped function metadata#10133
BridgeAR wants to merge 3 commits into
masterfrom
BridgeAR/2026-09-03-tracer-wrap-arity

Conversation

@BridgeAR

@BridgeAR BridgeAR commented Sep 3, 2026

Copy link
Copy Markdown
Member

What does this PR do?

Preserves the wrapped function’s arity and other function metadata in tracer.wrap() by using the established datadog-shimmer.wrapFunction mechanism.

This allows Express to recognize wrapped error middleware, which requires a function length of 4.

Motivation

tracer.wrap() currently returns a rest-parameter function with length 0. Express therefore skips wrapped error handlers and returns its fallback 500 response.

Additional Notes

The implementation preserves length, name, prototype, own property descriptors, symbols, this binding, callback behavior, synchronous and promise returns, thrown errors, and legacy constructor behavior.

Validation:

  • Tracer specs: 38 passing
  • Real Express request/error middleware tests: 12 passing across Express 4.0.0–5.2.1
  • Changed-line/branch coverage: 100%
  • npm run lint: passing

The full Express suite still has three existing deterministic failures on Express 4.0.0–4.3.0 in its repeated-next() diagnostic test; these are unrelated to this change.

@dd-octo-sts

dd-octo-sts Bot commented Sep 3, 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

@datadog-prod-us1-4

datadog-prod-us1-4 Bot commented Sep 3, 2026

Copy link
Copy Markdown

Pipelines  Tests

Unblock PR with BitsAI

⚠️ Warnings

Your PR has failed checks. Please review the issues below and take necessary action before merging.

🚦 1 Pipeline job failed

DataDog/apm-reliability/dd-trace-js | benchmark: [26, 3] — 🔧 Needs a code fix, caused by this PR

View more details · View in GitLab

ℹ️ Info

No other issues found (see more)

🧪 All tests passed
❄️ No new flaky tests detected

🔄 Datadog retried 1 test - 1 passed on retry View in Datadog

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

Useful? React with 👍 / 👎

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

@pr-commenter

pr-commenter Bot commented Sep 3, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-09-03 14:24:21

Comparing candidate commit 3dd654e in PR branch BridgeAR/2026-09-03-tracer-wrap-arity with baseline commit ff59eae 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-appsec-enabled-24

  • unstable max_rss_usage [-27.063MB; +7.453MB] or [-9.533%; +2.625%]

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

  • unstable max_rss_usage [-30.110MB; +23.251MB] or [-12.730%; +9.830%]

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

  • unstable max_rss_usage [-26.795MB; +21.221MB] or [-7.084%; +5.610%]

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

  • unstable max_rss_usage [-20.343MB; +12.083MB] or [-8.077%; +4.798%]

scenario:dogstatsd-with-tags-20

  • unstable cpu_user_time [-368.649ms; +293.377ms] or [-7.260%; +5.777%]
  • unstable execution_time [-365.276ms; +295.991ms] or [-7.081%; +5.738%]
  • unstable throughput [-96878.314op/s; +119927.334op/s] or [-5.962%; +7.380%]

scenario:encoders-0.4-immediate-flush-20

  • unstable max_rss_usage [-11.582MB; +6.478MB] or [-14.619%; +8.177%]

scenario:llmobs-encode-unicode-mixed-20

  • unstable max_rss_usage [-12.608MB; +6.037MB] or [-16.524%; +7.912%]

scenario:log-with-debug-20

  • unstable max_rss_usage [-8.440MB; +5.103MB] or [-7.340%; +4.438%]

scenario:plugin-aws-sdk-lambda-inject-with-context-24

  • unstable cpu_user_time [-198.505ms; +216.958ms] or [-5.219%; +5.704%]
  • unstable execution_time [-197.153ms; +220.347ms] or [-5.149%; +5.755%]

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

  • unstable max_rss_usage [-11.531MB; +7.849MB] or [-7.991%; +5.439%]

scenario:plugin-mongodb-core-binary-hash-24

  • unstable execution_time [-133.977ms; +214.154ms] or [-4.583%; +7.326%]

scenario:test-optimization-large-suite-20

  • unstable max_rss_usage [-5567.258KB; +5196.591KB] or [-6.909%; +6.449%]

@codecov

codecov Bot commented Sep 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.56%. Comparing base (ff59eae) to head (4c3d227).
⚠️ Report is 12 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10133      +/-   ##
==========================================
- Coverage   98.63%   98.56%   -0.07%     
==========================================
  Files        1002     1003       +1     
  Lines      154897   154902       +5     
  Branches    13523    13244     -279     
==========================================
- Hits       152780   152677     -103     
- Misses       2117     2225     +108     
Flag Coverage Δ
aiguard 63.72% <80.00%> (-0.14%) ⬇️
aiguard-integration 58.80% <70.00%> (-0.14%) ⬇️
apm-capabilities-tracing 62.90% <100.00%> (+0.04%) ⬆️
apm-integrations-aerospike 61.13% <80.00%> (-0.14%) ⬇️
apm-integrations-aerospike-_integrat-1d185c06 63.10% <80.00%> (-0.14%) ⬇️
apm-integrations-confluentinc-kafka--d0bf0045 67.32% <80.00%> (-0.02%) ⬇️
apm-integrations-couchbase 61.63% <80.00%> (-0.14%) ⬇️
apm-integrations-elasticsearch_integ-31b69669 68.86% <100.00%> (+0.26%) ⬆️
apm-integrations-http 66.88% <80.00%> (+0.01%) ⬆️
apm-integrations-http2_integrations--9aea12f5 68.22% <80.00%> (+0.01%) ⬆️
apm-integrations-kafkajs 67.94% <80.00%> (+0.05%) ⬆️
apm-integrations-next 64.79% <80.00%> (-0.13%) ⬇️
apm-integrations-prisma 62.40% <80.00%> (-0.12%) ⬇️
apm-integrations-restify_integration-b312dd16 64.92% <80.00%> (-0.13%) ⬇️
appsec 77.34% <80.00%> (+0.01%) ⬆️
appsec-express_fastify_graphql 72.72% <80.00%> (+0.01%) ⬆️
appsec-integration 49.52% <70.00%> (-0.03%) ⬇️
appsec-kafka_ldapjs_lodash 68.05% <80.00%> (+0.04%) ⬆️
appsec-mongodb-core_mongoose_mysql 71.15% <80.00%> (+0.01%) ⬆️
appsec-next 57.61% <80.00%> (-0.10%) ⬇️
appsec-node-serialize_passport_postgres 70.64% <80.00%> (+0.01%) ⬆️
appsec-sourcing_stripe_template 69.09% <80.00%> (+0.01%) ⬆️
debugger 69.71% <80.00%> (-0.08%) ⬇️
instr-ai_instrumentation-anthropic-l-e862da55 56.49% <80.00%> (-0.14%) ⬇️
instr-bluebird_instrumentation-body--02ae4b41 64.73% <80.00%> (-0.13%) ⬇️
instr-confluentinc-kafka-javascript 55.89% <80.00%> (-0.15%) ⬇️
instr-connect_instrumentation-cookie-0bbb44d9 57.60% <80.00%> (-0.13%) ⬇️
instr-couchbase 55.11% <80.00%> (-0.15%) ⬇️
instr-cypress-config_instrumentation-4db1de46 57.92% <80.00%> (-0.13%) ⬇️
instr-express-multi-version_instrume-f5caf487 64.20% <80.00%> (-0.13%) ⬇️
instr-fetch_instrumentation-fs_instr-43dfe6a2 50.46% <80.00%> (-0.11%) ⬇️
instr-hono_instrumentation-http_inst-026a5d4d 66.21% <80.00%> (+0.02%) ⬆️
instr-kafkajs_instrumentation-knex_i-ca6f5b19 56.55% <80.00%> (-0.14%) ⬇️
instr-light-my-request_instrumentati-8c9fceca 63.51% <80.00%> (-0.13%) ⬇️
instr-mysql_instrumentation-mysql2_i-05f94c42 59.01% <80.00%> (-0.12%) ⬇️
instr-openai-lifecycle_instrumentati-783580bd 65.87% <80.00%> (-0.12%) ⬇️
instr-passport-http_instrumentation--2b6c8eb6 66.48% <80.00%> (-0.12%) ⬇️
instr-promise_instrumentation-promis-8479bae3 56.32% <80.00%> (-0.14%) ⬇️
instr-restify_instrumentation-router-5641467c 56.47% <80.00%> (-0.13%) ⬇️
instr-url_instrumentation-when_instr-22ca7078 56.35% <80.00%> (-0.14%) ⬇️
instrumentations-integration-esbuild 34.65% <50.00%> (-0.04%) ⬇️
llmobs-ai_anthropic_bedrock 66.84% <80.00%> (-0.02%) ⬇️
llmobs-claude-agent-sdk_google-genai-da2c851e 65.03% <95.00%> (+0.03%) ⬆️
llmobs-openai 66.86% <80.00%> (+0.03%) ⬆️
llmobs-openai-agents_vertex-ai 64.07% <80.00%> (-0.10%) ⬇️
llmobs-sdk 76.62% <100.00%> (+0.01%) ⬆️
master-coverage 98.56% <100.00%> (?)
openfeature 60.06% <70.00%> (-0.07%) ⬇️
openfeature-unit 58.74% <80.00%> (-0.14%) ⬇️
platform-core_esbuild_instrumentations-misc 41.31% <80.00%> (-0.06%) ⬇️
platform-integration 65.39% <70.00%> (+0.16%) ⬆️
platform-shimmer_unit-guardrails_webpack 39.06% <80.00%> (-0.06%) ⬇️
plugins-aws-durable-execution-sdk-js-2bc88c07 61.17% <80.00%> (+0.01%) ⬆️
plugins-azure-event-hubs_azure-servi-50d5b0fc 58.46% <70.00%> (-0.08%) ⬇️
plugins-browser-bunyan_bullmq_cassandra 66.41% <80.00%> (+0.01%) ⬆️
plugins-cookie_cookie-parser_crypto 56.09% <80.00%> (-0.15%) ⬇️
plugins-dd-trace-api_express-mongo-s-83827a5c 61.68% <80.00%> (-0.14%) ⬇️
plugins-fastify_fetch_fs 66.05% <80.00%> (+0.05%) ⬆️
plugins-generic-pool_google-cloud-pubsub_grpc 69.59% <80.00%> (+0.01%) ⬆️
plugins-handlebars_hapi_hono 64.17% <80.00%> (-0.14%) ⬇️
plugins-ioredis_knex_langgraph 61.87% <80.00%> (-0.14%) ⬇️
plugins-ldapjs_light-my-request_limitd-client 63.89% <80.00%> (-0.14%) ⬇️
plugins-lodash_mariadb_memcached 64.35% <80.00%> (-0.13%) ⬇️
plugins-mercurius_microgateway-core_-b9bd83c4 67.88% <80.00%> (+0.01%) ⬆️
plugins-moleculer_mongodb_mongodb-core 66.76% <80.00%> (+0.01%) ⬆️
plugins-mongoose_multer_mysql 64.46% <80.00%> (-0.13%) ⬇️
plugins-mysql2_nats_node-serialize 66.84% <80.00%> (+0.02%) ⬆️
plugins-opensearch_passport-http_pino 64.74% <80.00%> (+0.02%) ⬆️
plugins-postgres_process_pug 63.64% <80.00%> (-0.14%) ⬇️
plugins-redis_router_sequelize 67.32% <80.00%> (+0.01%) ⬆️
plugins-test-and-upstream-amqp10_tes-914101e2 65.54% <80.00%> (+0.01%) ⬆️
plugins-test-and-upstream-avsc_test--408e8d3a 65.51% <80.00%> (-0.14%) ⬇️
plugins-test-and-upstream-graphql_te-59dd94de 68.74% <80.00%> (-0.13%) ⬇️
plugins-test-and-upstream-rhea_undici_url 66.64% <80.00%> (+0.01%) ⬆️
plugins-valkey_vm_winston 62.96% <80.00%> (-0.14%) ⬇️
plugins-ws 65.17% <80.00%> (+0.02%) ⬆️
profiling 66.59% <80.00%> (-0.13%) ⬇️
serverless-aws-sdk-aws-sdk 56.00% <80.00%> (+0.01%) ⬆️
serverless-aws-sdk-base-inject-field 55.66% <80.00%> (-0.15%) ⬇️
serverless-aws-sdk-bedrockruntime 59.21% <80.00%> (-0.12%) ⬇️
serverless-aws-sdk-client 60.72% <80.00%> (+0.02%) ⬆️
serverless-aws-sdk-dynamodb 59.65% <80.00%> (-0.12%) ⬇️
serverless-aws-sdk-eventbridge 58.18% <80.00%> (+0.01%) ⬆️
serverless-aws-sdk-kinesis 63.73% <80.00%> (+0.01%) ⬆️
serverless-aws-sdk-lambda 61.72% <80.00%> (+0.02%) ⬆️
serverless-aws-sdk-s3 59.69% <80.00%> (-0.12%) ⬇️
serverless-aws-sdk-serverless-peer-service ?
serverless-aws-sdk-sns 64.62% <80.00%> (+0.01%) ⬆️
serverless-aws-sdk-sqs ?
serverless-aws-sdk-stepfunctions ?
serverless-aws-sdk-util 56.29% <80.00%> (-0.15%) ⬇️
serverless-azure-durable-functions_a-776237ee 57.62% <70.00%> (-0.07%) ⬇️
serverless-azure-functions-eventhubs-61ee6313 64.26% <100.00%> (-0.09%) ⬇️
test-optimization-cucumber 70.38% <80.00%> (-0.05%) ⬇️
test-optimization-cypress 64.76% <70.00%> (-0.04%) ⬇️
test-optimization-jest 72.06% <80.00%> (+0.16%) ⬆️
test-optimization-mocha 71.86% <80.00%> (+<0.01%) ⬆️
test-optimization-playwright-playwri-363e40a6 59.66% <70.00%> (+0.02%) ⬆️
test-optimization-playwright-playwri-5141d36d 61.17% <70.00%> (-0.30%) ⬇️
test-optimization-playwright-playwri-5b35813b 59.54% <70.00%> (-0.10%) ⬇️
test-optimization-playwright-playwri-924f4516 60.83% <70.00%> (-0.10%) ⬇️
test-optimization-playwright-playwri-d6f5a99b 59.42% <70.00%> (-0.06%) ⬇️
test-optimization-playwright-playwright-atr 59.15% <70.00%> (-0.10%) ⬇️
test-optimization-playwright-playwright-efd 60.07% <70.00%> (-0.10%) ⬇️
test-optimization-selenium 58.76% <70.00%> (-0.11%) ⬇️
test-optimization-testopt 61.44% <80.00%> (+0.01%) ⬆️
test-optimization-vitest 72.53% <80.00%> (+0.06%) ⬆️
test-optimization-vitest-browser 58.83% <70.00%> (-0.06%) ⬇️
test-optimization-webdriverio 65.86% <80.00%> (+0.02%) ⬆️

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.

@BridgeAR
BridgeAR marked this pull request as ready for review September 3, 2026 12:25
@BridgeAR
BridgeAR requested review from a team as code owners September 3, 2026 12:25
@BridgeAR
BridgeAR requested a review from wconti27 September 3, 2026 12:25

@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: 2447b6984c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/dd-trace/src/tracer.js
Express classifies middleware by function arity, so tracer.wrap() turns four-argument error handlers into length-0 functions that are skipped. Preserve the wrapped function shape through the existing shimmer helper so callback, return, throw, and constructor behavior remain transparent.
Frozen functions have the same non-writable prototype descriptor as native classes, so assertNotClass rejects valid tracer.wrap() inputs. Let frozen targets through and retain descriptor-copy failures only for immutable wrapper properties.
@BridgeAR
BridgeAR force-pushed the BridgeAR/2026-09-03-tracer-wrap-arity branch from 2447b69 to 3dd654e Compare September 3, 2026 14:12

@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: 3dd654eb9a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

const shimmer = require('../../datadog-shimmer')

return function (...args) {
return shimmer.wrapFunction(fn, original => function (...args) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Finish spans for terminal Express error middleware

When the newly supported four-argument Express error middleware handles the response without calling next—the normal terminal-error-handler pattern—the last argument is still a function, so this wrapper treats next as a completion callback and never calls done. The unfinished middleware span prevents the trace from flushing under the normal started.length === finished.length path in packages/dd-trace/src/span_processor.js:53; the added integration test masks this by explicitly calling next() before sending its response. Account for middleware completion without requiring next so the motivating Express use case does not lose traces.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is a pre existing issue as it seems. That should be handled separately

const shimmer = require('../../datadog-shimmer')

return function (...args) {
return shimmer.wrapFunction(fn, original => function (...args) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve tracing through custom promisifiers

When fn defines util.promisify.custom, wrapFunction copies that symbol's descriptor unchanged, so util.promisify(wrapped) returns the original custom promisifier and never executes this wrapper or tracer.trace; before this change, promisifying the metadata-free wrapper still invoked the traced function. The explicit wrapping of the same hook in packages/datadog-instrumentations/src/child_process.js:226-238 confirms that copying it verbatim bypasses instrumentation. Wrap the custom promisifier as well, or avoid exposing the unwrapped hook on the returned function.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This seems to be a pre-existing issue that is something that should be fixed independently

@BridgeAR
BridgeAR marked this pull request as draft September 3, 2026 19:15
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.

1 participant