Skip to content

test_runner: publish to TracingChannel node.test for OTel instrumentation#62502

Open
MoLow wants to merge 1 commit intomainfrom
test-tracing-channel
Open

test_runner: publish to TracingChannel node.test for OTel instrumentation#62502
MoLow wants to merge 1 commit intomainfrom
test-tracing-channel

Conversation

@MoLow
Copy link
Copy Markdown
Member

@MoLow MoLow commented Mar 30, 2026

No description provided.

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/test_runner

@MoLow MoLow requested a review from Copilot March 30, 2026 10:54
@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem. labels Mar 30, 2026
Copy link
Copy Markdown
Member

@benjamingr benjamingr left a comment

Choose a reason for hiding this comment

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

really cool

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds diagnostics channel tracing hooks to the built-in Node.js test runner to enable OpenTelemetry-style instrumentation via diagnostics_channel.tracingChannel('node.test').

Changes:

  • Introduce a shared TracingChannel('node.test') for the internal test runner and export it from test-runner utils.
  • Wrap test/suite execution with start.runStores(...) and publish end/error lifecycle events.
  • Add documentation and new tests/fixtures validating lifecycle events and AsyncLocalStorage propagation via bindStore().

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
test/parallel/test-runner-diagnostics-channel.js New parallel test covering start/end/error events and bindStore propagation.
test/fixtures/test-runner/diagnostics-channel-error-test.js Fixture that intentionally fails to validate error event emission.
lib/internal/test_runner/utils.js Creates and exports testChannel via diagnostics_channel.tracingChannel('node.test').
lib/internal/test_runner/test.js Instruments Test.run() and Suite.createBuild() to emit tracing lifecycle events and run stores.
doc/api/test.md Documents the new tracing channels and provides an OpenTelemetry integration example.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +3622 to +3628
import { diagnosticsChannel } from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';
import { context, trace } from '@opentelemetry/api';

const testStorage = new AsyncLocalStorage();
const testChannel = diagnosticsChannel.tracingChannel('node.test');
const tracer = trace.getTracer('test-tracer');
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

The OpenTelemetry example imports diagnosticsChannel from node:diagnostics_channel, but that module's public API exports functions like tracingChannel (and does not export a diagnosticsChannel binding). As written, the example code will throw at import time / diagnosticsChannel.tracingChannel(...) will be undefined. Update the example to use the actual node:diagnostics_channel exports (e.g., import tracingChannel directly or import the module namespace and call .tracingChannel).

Copilot uses AI. Check for mistakes.
Comment on lines +1636 to 1644
if (channelContext !== null && testChannel.end.hasSubscribers) {
testChannel.end.publish(channelContext);
}
} catch (err) {
if (channelContext !== null && testChannel.error.hasSubscribers) {
testChannel.error.publish({ __proto__: null, ...channelContext, error: err });
}
this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
}
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

In Suite.createBuild(), the tracing:node.test:end event is only published on the success path. If the suite function throws, tracing:node.test:error is published but end is never emitted, which makes lifecycle instrumentation incomplete/inconsistent with Test.run() (where end is always emitted in finally). Consider publishing end in a finally block so it fires for both success and error cases.

Copilot uses AI. Check for mistakes.
Comment on lines +1624 to +1628
let suiteFn = this.fn;
if (channelContext !== null && testChannel.start.hasSubscribers) {
suiteFn = (...fnArgs) => testChannel.start.runStores(channelContext,
() => ReflectApply(this.fn, this, fnArgs),
);
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

The wrapped suiteFn calls ReflectApply(this.fn, ...), but Suite mutates this.fn to noop in the constructor after kicking off createBuild(). This makes the wrapper fragile and easy to break if execution order changes. Prefer invoking the originally captured suite function (e.g., via a local baseFn) inside the wrapper instead of referencing this.fn.

Copilot uses AI. Check for mistakes.
* `data` {Object}
* `name` {string} The name of the test.
* `nesting` {number} The nesting level of the test.
* `file` {string} The path to the test file.
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.

in most places we also say something around the lines of "or undefined when in REPL", I think the 3 occurrences here should too?

```mjs
import { diagnosticsChannel } from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';
import { context, trace } from '@opentelemetry/api';
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.

Is it common/customary for the node docs to show examples with external libraries?

@MoLow MoLow force-pushed the test-tracing-channel branch from c3b10e5 to 75fe3a3 Compare March 30, 2026 11:08
Comment on lines +65 to +66
test('bindStore propagates into test body via start channel', async () => {
const expectedName = 'bindStore propagates into test body via start channel';
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.

why not:

Suggested change
test('bindStore propagates into test body via start channel', async () => {
const expectedName = 'bindStore propagates into test body via start channel';
const expectedName = 'bindStore propagates into test body via start channel';
test(expectedName, async () => {

});
});

const { describe, it } = require('node:test');
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.

Any reason for this to be down here and not alongside the test in line 5?

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 30, 2026

Codecov Report

❌ Patch coverage is 95.52239% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.70%. Comparing base (d9645d7) to head (75fe3a3).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/test_runner/test.js 95.31% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #62502   +/-   ##
=======================================
  Coverage   89.69%   89.70%           
=======================================
  Files         692      692           
  Lines      214039   214104   +65     
  Branches    41064    41078   +14     
=======================================
+ Hits       191985   192062   +77     
  Misses      14134    14134           
+ Partials     7920     7908   -12     
Files with missing lines Coverage Δ
lib/internal/test_runner/utils.js 62.36% <100.00%> (+0.17%) ⬆️
lib/internal/test_runner/test.js 96.74% <95.31%> (-0.06%) ⬇️

... and 32 files with indirect coverage changes

🚀 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.

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

Labels

needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants