Skip to content

test(opentelemetry): deflake OTLP metrics HTTP export test with fake timers#9287

Merged
BridgeAR merged 1 commit into
masterfrom
brian.marks/deflake-otel-metrics-http-export
Jul 10, 2026
Merged

test(opentelemetry): deflake OTLP metrics HTTP export test with fake timers#9287
BridgeAR merged 1 commit into
masterfrom
brian.marks/deflake-otel-metrics-http-export

Conversation

@bm1549

@bm1549 bm1549 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Rewrites the OpenTelemetry metrics test handles timeout and error without network to use sinon fake timers instead of racing real wall-clock timeouts. No production code changes.

Motivation

The test flaked on master (Node 24). It asserted results.timeout and results.error at a fixed 300ms deadline, but both flags depend on two real periodic-exporter ticks (100ms interval) plus each request's real +10ms timeout/error callback. The second export's error handler fires around ~251ms, leaving only ~50ms of slack, so a modest event-loop stall on a loaded runner (GC or CPU contention) pushed it past 300ms. With results.error still false, assert(results.error) threw inside Timeout._onTimeout.

The fix installs useFakeTimers() before setupMetrics() so the reader's export interval is faked, then advances the clock explicitly for each export round-trip. This matches the repo guidance to never rely on real time passing in unit tests. The HTTP stub is unchanged.

Additional Notes

Verified: 25/25 clean runs and 8/8 under CPU contention. A 1.3s real event-loop stall injected into the window that broke the original still passes (the original flaked at ~150ms). Full metrics.spec.js: 58 passing.

🤖 Generated with Claude Code

@bm1549 bm1549 added the AI Generated Largely based on code generated by an AI or LLM. This label is the same across all dd-trace-* repos label Jul 10, 2026
@dd-octo-sts

dd-octo-sts Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Overall package size

Self size: 6.71 MB
Deduped: 7.36 MB
No deduping: 7.36 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.3.1 | 122.62 kB | 437.94 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-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Jul 10, 2026

Copy link
Copy Markdown

Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 77.16% (-19.37%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 8c7d789 | Docs | Datadog PR Page | Give us feedback!

@bm1549
bm1549 force-pushed the brian.marks/deflake-otel-metrics-http-export branch from 38874fc to 2f3f1af Compare July 10, 2026 17:45
@pr-commenter

pr-commenter Bot commented Jul 10, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-07-10 18:14:38

Comparing candidate commit 8c7d789 in PR branch brian.marks/deflake-otel-metrics-http-export with baseline commit b40ce55 in branch master.

📊 Benchmarking dashboard

Found 0 performance improvements and 0 performance regressions! Performance is the same for 2315 metrics, 43 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 execution_time [-218020.716µs; +216802.882µs] or [-8.214%; +8.169%]

scenario:appsec-appsec-enabled-26

  • unstable execution_time [-237.616ms; +232.934ms] or [-9.265%; +9.082%]

scenario:appsec-appsec-enabled-with-attacks-24

  • unstable execution_time [-167.588ms; +152.326ms] or [-5.423%; +4.929%]

scenario:appsec-appsec-enabled-with-attacks-26

  • unstable execution_time [-194.700ms; +199.964ms] or [-6.693%; +6.874%]

scenario:appsec-control-20

  • unstable execution_time [-116.772ms; +131.989ms] or [-7.079%; +8.001%]

scenario:appsec-control-24

  • unstable execution_time [-113.628ms; +116.241ms] or [-9.193%; +9.404%]

scenario:appsec-control-26

  • unstable execution_time [-125.319ms; +121.932ms] or [-10.089%; +9.817%]

scenario:appsec-iast-no-vulnerability-iast-enabled-always-active-20

  • unstable execution_time [-14.399ms; +22.108ms] or [-5.446%; +8.362%]

scenario:child_process-shell-string-24

  • unstable execution_time [-14.408ms; +18.419ms] or [-4.447%; +5.685%]

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

  • unstable cpu_user_time [-2.580s; +0.418s] or [-28.556%; +4.626%]
  • unstable execution_time [-2.647s; +0.462s] or [-27.123%; +4.733%]
  • unstable instructions [-22.1G instructions; +3.5G instructions] or [-29.748%; +4.669%]
  • unstable throughput [-154.963op/s; +712.205op/s] or [-4.516%; +20.754%]

scenario:debugger-line-probe-with-snapshot-default-26

  • unstable cpu_user_time [-2655.154ms; +4194.203ms] or [-27.822%; +43.949%]
  • unstable execution_time [-2646.617ms; +4201.282ms] or [-25.761%; +40.894%]
  • unstable instructions [-23.2G instructions; +36.9G instructions] or [-29.067%; +46.260%]
  • unstable max_rss_usage [-8.149MB; +12.507MB] or [-5.092%; +7.815%]
  • unstable throughput [-820.521op/s; +518.366op/s] or [-25.424%; +16.061%]

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

  • unstable cpu_user_time [-596.305ms; +280.400ms] or [-7.339%; +3.451%]
  • unstable execution_time [-673.689ms; +323.134ms] or [-7.624%; +3.657%]
  • unstable instructions [-4.5G instructions; +2.2G instructions] or [-6.862%; +3.301%]
  • unstable throughput [-134.498op/s; +283.446op/s] or [-3.696%; +7.789%]

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

  • unstable cpu_user_time [-1803.838ms; +586.742ms] or [-19.230%; +6.255%]
  • unstable execution_time [-1817.613ms; +599.768ms] or [-17.958%; +5.926%]
  • unstable instructions [-16.0G instructions; +5.4G instructions] or [-20.460%; +6.845%]
  • unstable throughput [-137.269op/s; +403.960op/s] or [-4.236%; +12.467%]

scenario:debugger-line-probe-without-snapshot-24

  • unstable cpu_user_time [-2.561s; +0.424s] or [-28.313%; +4.692%]
  • unstable execution_time [-2.626s; +0.480s] or [-26.890%; +4.914%]
  • unstable instructions [-22.1G instructions; +3.5G instructions] or [-29.745%; +4.674%]
  • unstable throughput [-162.321op/s; +704.827op/s] or [-4.735%; +20.561%]

scenario:dogstatsd-with-tags-20

  • unstable cpu_user_time [-332.090ms; +324.984ms] or [-7.059%; +6.908%]
  • unstable execution_time [-333.939ms; +319.138ms] or [-6.980%; +6.671%]
  • unstable throughput [-112176.910op/s; +114513.092op/s] or [-6.395%; +6.528%]

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

  • unstable cpu_usage_percentage [-4.906%; +5.342%]

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

  • unstable cpu_user_time [-704.778ms; +731.928ms] or [-5.505%; +5.717%]
  • unstable execution_time [-713.707ms; +733.679ms] or [-5.452%; +5.604%]
  • unstable throughput [-3.386op/s; +3.296op/s] or [-5.507%; +5.361%]

scenario:plugin-pg-service-26

  • unstable cpu_usage_percentage [-10.438%; +8.367%]
  • unstable execution_time [-118.008ms; +172.987ms] or [-12.602%; +18.474%]
  • unstable throughput [-814280.693op/s; +517605.259op/s] or [-12.330%; +7.838%]

scenario:plugin-pino-json-log-injection-24

  • unstable cpu_user_time [-160.213ms; +267.715ms] or [-5.553%; +9.279%]
  • unstable execution_time [-198.429ms; +287.750ms] or [-6.716%; +9.740%]
  • unstable throughput [-139023.731op/s; +90396.202op/s] or [-8.122%; +5.281%]

…timers

`handles timeout and error without network` asserted `results.timeout` and
`results.error` at a fixed 300ms wall-clock deadline, but both flags depend on
two real periodic-exporter ticks (100ms interval) plus each request's real
+10ms timeout/error callback. The second export's error handler fires around
~251ms, leaving only ~50ms of slack, so a modest event-loop stall on a loaded
CI runner (GC/CPU contention) pushed it past 300ms and left `results.error`
false — throwing `assert(results.error)` inside `Timeout._onTimeout`.

Drive the two exports deterministically with sinon fake timers instead of
racing wall-clock time: install `useFakeTimers()` before `setupMetrics()` so
the reader's export interval is faked, then advance the clock explicitly for
each export round-trip. The HTTP stub is unchanged.

Verified: 25/25 clean runs and 8/8 under CPU contention; a 1.3s real
event-loop stall injected into the window that broke the original still
passes (it flaked at ~150ms). Full metrics.spec.js: 58 passing.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@bm1549
bm1549 force-pushed the brian.marks/deflake-otel-metrics-http-export branch from 2f3f1af to 8c7d789 Compare July 10, 2026 18:01
@bm1549
bm1549 marked this pull request as ready for review July 10, 2026 18:05
@bm1549
bm1549 requested a review from a team as a code owner July 10, 2026 18:05
@bm1549
bm1549 requested review from BridgeAR and removed request for a team July 10, 2026 18:05
@BridgeAR
BridgeAR enabled auto-merge (squash) July 10, 2026 18:09
@BridgeAR
BridgeAR merged commit f542182 into master Jul 10, 2026
655 checks passed
@BridgeAR
BridgeAR deleted the brian.marks/deflake-otel-metrics-http-export branch July 10, 2026 18:16
@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.57%. Comparing base (b40ce55) to head (8c7d789).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #9287    +/-   ##
========================================
  Coverage   96.56%   96.57%            
========================================
  Files         918      918            
  Lines      121564   121564            
  Branches    20634    20887   +253     
========================================
+ Hits       117394   117396     +2     
+ Misses       4170     4168     -2     
Flag Coverage Δ
aiguard 53.55% <ø> (-0.05%) ⬇️
aiguard-integration 57.00% <ø> (ø)
apm-bucket-0 53.78% <ø> (-0.05%) ⬇️
apm-bucket-1 58.07% <ø> (-0.04%) ⬇️
apm-bucket-2 56.11% <ø> (-0.05%) ⬇️
apm-capabilities-tracing 60.32% <ø> (ø)
apm-integrations-aerospike 52.93% <ø> (-0.05%) ⬇️
apm-integrations-confluentinc-kafka-javascript 57.05% <ø> (-0.05%) ⬇️
apm-integrations-couchbase 53.26% <ø> (-0.05%) ⬇️
apm-integrations-http 57.95% <ø> (-0.05%) ⬇️
apm-integrations-kafkajs 57.71% <ø> (-0.05%) ⬇️
apm-integrations-next 54.23% <ø> (-0.05%) ⬇️
apm-integrations-prisma 54.17% <ø> (-0.05%) ⬇️
apm-integrations-tedious 53.04% <ø> (-0.05%) ⬇️
appsec 69.02% <ø> (-0.04%) ⬇️
appsec-express_fastify_graphql 65.60% <ø> (-0.03%) ⬇️
appsec-integration 45.58% <ø> (ø)
appsec-kafka_ldapjs_lodash 59.09% <ø> (-0.04%) ⬇️
appsec-mongodb-core_mongoose_mysql 62.24% <ø> (-0.04%) ⬇️
appsec-next 52.84% <ø> (-0.04%) ⬇️
appsec-node-serialize_passport_postgres 61.87% <ø> (-0.04%) ⬇️
appsec-sourcing_stripe_template 60.23% <ø> (-0.04%) ⬇️
debugger 65.71% <ø> (-0.04%) ⬇️
instrumentations-bucket-0 48.74% <ø> (-0.05%) ⬇️
instrumentations-bucket-1 54.95% <ø> (-0.05%) ⬇️
instrumentations-bucket-10 56.61% <ø> (-0.05%) ⬇️
instrumentations-bucket-11 48.73% <ø> (-0.05%) ⬇️
instrumentations-bucket-12 48.84% <ø> (-0.05%) ⬇️
instrumentations-bucket-13 48.67% <ø> (-0.05%) ⬇️
instrumentations-bucket-2 49.73% <ø> (-0.05%) ⬇️
instrumentations-bucket-3 53.82% <ø> (-0.05%) ⬇️
instrumentations-bucket-4 49.15% <ø> (-0.05%) ⬇️
instrumentations-bucket-5 53.00% <ø> (-0.05%) ⬇️
instrumentations-bucket-6 55.60% <ø> (-0.05%) ⬇️
instrumentations-bucket-7 53.59% <ø> (-0.05%) ⬇️
instrumentations-bucket-8 54.63% <ø> (-0.05%) ⬇️
instrumentations-bucket-9 56.09% <ø> (-0.04%) ⬇️
instrumentations-instrumentation-couchbase 48.17% <ø> (-0.05%) ⬇️
instrumentations-integration-esbuild 33.96% <ø> (ø)
llmobs-ai_anthropic_bedrock 57.72% <ø> (-0.04%) ⬇️
llmobs-bucket-1 56.99% <ø> (-0.04%) ⬇️
llmobs-openai 57.55% <ø> (-0.04%) ⬇️
llmobs-sdk 59.94% <ø> (-0.05%) ⬇️
llmobs-vertex-ai 54.20% <ø> (-0.05%) ⬇️
master-coverage 96.57% <ø> (?)
openfeature 54.61% <ø> (-0.01%) ⬇️
openfeature-unit 49.82% <ø> (-0.05%) ⬇️
platform-core_esbuild_instrumentations-misc 38.45% <ø> (-0.03%) ⬇️
platform-integration 62.23% <ø> (ø)
platform-shimmer_unit-guardrails_webpack 37.28% <ø> (-0.03%) ⬇️
plugins-bucket-0 53.17% <ø> (-0.05%) ⬇️
plugins-bucket-1 55.07% <ø> (ø)
plugins-bucket-11 55.59% <ø> (-0.02%) ⬇️
plugins-bucket-18 57.06% <ø> (-0.05%) ⬇️
plugins-bucket-19 55.37% <ø> (-0.03%) ⬇️
plugins-bucket-20 57.44% <ø> (-0.05%) ⬇️
plugins-bucket-4 53.84% <ø> (-0.05%) ⬇️
plugins-bullmq_cassandra_cookie 57.39% <ø> (-0.05%) ⬇️
plugins-cookie-parser_crypto_dd-trace-api 52.31% <ø> (-0.05%) ⬇️
plugins-fetch_fs_generic-pool 54.46% <ø> (-0.05%) ⬇️
plugins-google-cloud-pubsub_grpc_handlebars 59.86% <ø> (-0.05%) ⬇️
plugins-hapi_hono_ioredis 55.85% <ø> (-0.05%) ⬇️
plugins-jest_knex_langgraph 51.66% <ø> (-0.05%) ⬇️
plugins-ldapjs_light-my-request_limitd-client 53.43% <ø> (-0.03%) ⬇️
plugins-lodash_mariadb_memcached 54.20% <ø> (-0.05%) ⬇️
plugins-moleculer_mongodb_mongodb-core 57.28% <ø> (-0.05%) ⬇️
plugins-mongoose_multer_mysql 54.70% <ø> (-0.05%) ⬇️
plugins-mysql2_nats_node-serialize 56.20% <ø> (-0.05%) ⬇️
plugins-opensearch_passport-http_pino 55.05% <ø> (-0.05%) ⬇️
plugins-postgres_process_pug 54.28% <ø> (-0.05%) ⬇️
plugins-redis_router_sequelize 57.01% <ø> (-0.07%) ⬇️
plugins-test-and-upstream-rhea_undici_url 57.02% <ø> (-0.05%) ⬇️
plugins-valkey_vm_winston 53.92% <ø> (-0.05%) ⬇️
plugins-ws 54.79% <ø> (-0.05%) ⬇️
profiling 58.19% <ø> (-0.05%) ⬇️
serverless-aws-sdk-aws-sdk 50.66% <ø> (-0.04%) ⬇️
serverless-aws-sdk-bedrockruntime 50.68% <ø> (-0.05%) ⬇️
serverless-aws-sdk-client 51.97% <ø> (-0.05%) ⬇️
serverless-aws-sdk-dynamodb 51.74% <ø> (-0.07%) ⬇️
serverless-aws-sdk-eventbridge 46.24% <ø> (-0.05%) ⬇️
serverless-aws-sdk-kinesis 54.71% <ø> (-0.05%) ⬇️
serverless-aws-sdk-lambda 52.78% <ø> (-0.05%) ⬇️
serverless-aws-sdk-s3 51.53% <ø> (-0.05%) ⬇️
serverless-aws-sdk-serverless-peer-service 54.72% <ø> (-0.05%) ⬇️
serverless-aws-sdk-sns 55.48% <ø> (-0.05%) ⬇️
serverless-aws-sdk-sqs 55.91% <ø> (-0.05%) ⬇️
serverless-aws-sdk-stepfunctions 51.25% <ø> (-0.05%) ⬇️
serverless-aws-sdk-util 48.47% <ø> (-0.05%) ⬇️
serverless-bucket-0 55.22% <ø> (ø)
serverless-bucket-1 55.99% <ø> (-0.05%) ⬇️
test-optimization-cucumber 72.97% <ø> (+0.02%) ⬆️
test-optimization-cypress 66.33% <ø> (+0.05%) ⬆️
test-optimization-jest 73.94% <ø> (+0.02%) ⬆️
test-optimization-mocha 74.29% <ø> (+0.05%) ⬆️
test-optimization-playwright-playwright-atr 61.56% <ø> (+0.02%) ⬆️
test-optimization-playwright-playwright-efd 61.75% <ø> (+0.03%) ⬆️
test-optimization-playwright-playwright-final-status 61.71% <ø> (+0.02%) ⬆️
test-optimization-playwright-playwright-impacted-tests 61.43% <ø> (+0.19%) ⬆️
test-optimization-playwright-playwright-reporting 61.33% <ø> (+0.02%) ⬆️
test-optimization-playwright-playwright-test-management 62.24% <ø> (-0.09%) ⬇️
test-optimization-playwright-playwright-test-span 61.48% <ø> (-0.03%) ⬇️
test-optimization-selenium 60.67% <ø> (-0.13%) ⬇️
test-optimization-testopt 59.15% <ø> (+0.08%) ⬆️
test-optimization-vitest 71.57% <ø> (+0.04%) ⬆️

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 pushed a commit that referenced this pull request Jul 14, 2026
…timers (#9287)

`handles timeout and error without network` asserted `results.timeout` and
`results.error` at a fixed 300ms wall-clock deadline, but both flags depend on
two real periodic-exporter ticks (100ms interval) plus each request's real
+10ms timeout/error callback. The second export's error handler fires around
~251ms, leaving only ~50ms of slack, so a modest event-loop stall on a loaded
CI runner (GC/CPU contention) pushed it past 300ms and left `results.error`
false — throwing `assert(results.error)` inside `Timeout._onTimeout`.

Drive the two exports deterministically with sinon fake timers instead of
racing wall-clock time: install `useFakeTimers()` before `setupMetrics()` so
the reader's export interval is faked, then advance the clock explicitly for
each export round-trip. The HTTP stub is unchanged.

Verified: 25/25 clean runs and 8/8 under CPU contention; a 1.3s real
event-loop stall injected into the window that broke the original still
passes (it flaked at ~150ms). Full metrics.spec.js: 58 passing.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
juan-fernandez pushed a commit that referenced this pull request Jul 14, 2026
…timers (#9287)

`handles timeout and error without network` asserted `results.timeout` and
`results.error` at a fixed 300ms wall-clock deadline, but both flags depend on
two real periodic-exporter ticks (100ms interval) plus each request's real
+10ms timeout/error callback. The second export's error handler fires around
~251ms, leaving only ~50ms of slack, so a modest event-loop stall on a loaded
CI runner (GC/CPU contention) pushed it past 300ms and left `results.error`
false — throwing `assert(results.error)` inside `Timeout._onTimeout`.

Drive the two exports deterministically with sinon fake timers instead of
racing wall-clock time: install `useFakeTimers()` before `setupMetrics()` so
the reader's export interval is faked, then advance the clock explicitly for
each export round-trip. The HTTP stub is unchanged.

Verified: 25/25 clean runs and 8/8 under CPU contention; a 1.3s real
event-loop stall injected into the window that broke the original still
passes (it flaked at ~150ms). Full metrics.spec.js: 58 passing.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI Generated Largely based on code generated by an AI or LLM. This label is the same across all dd-trace-* repos semver-patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants