Skip to content

Commit 876cb84

Browse files
Copilothi-ogawa
andauthored
feat: support TRACEPARENT and TRACESTATE environment variables for OpenTelemetry context propagation (#9295)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: hi-ogawa <4232207+hi-ogawa@users.noreply.github.com> Co-authored-by: Hiroshi Ogawa <hi.ogawa.zz@gmail.com>
1 parent caa7d73 commit 876cb84

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

docs/guide/open-telemetry.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,7 @@ You can view traces using any of the open source or commercial products that sup
150150
Vitest declares `@opentelemetry/api` as an optional peer dependency, which it uses internally to generate spans. When trace collection is not enabled, Vitest will not attempt to use this dependency.
151151

152152
When configuring Vitest to use OpenTelemetry, you will typically install `@opentelemetry/sdk-node`, which includes `@opentelemetry/api` as a transitive dependency, thereby satisfying Vitest's peer dependency requirement. If you encounter an error indicating that `@opentelemetry/api` cannot be found, this typically means trace collection has not been enabled. If the error persists after proper configuration, you may need to install `@opentelemetry/api` explicitly.
153+
154+
## Inter-Process Context Propagation
155+
156+
Vitest supports automatic context propagation from parent processes via the `TRACEPARENT` and `TRACESTATE` environment variables as defined in the [OpenTelemetry specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/env-carriers.md). This is particularly useful when running Vitest as part of a larger distributed tracing system (e.g., CI/CD pipelines with OpenTelemetry instrumentation).

packages/vitest/src/node/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ export class Vitest {
651651
* @param filters String filters to match the test files
652652
*/
653653
async start(filters?: string[]): Promise<TestRunResult> {
654-
return this._traces.$('vitest.start', async (startSpan) => {
654+
return this._traces.$('vitest.start', { context: this._traces.getContextFromEnv(process.env) }, async (startSpan) => {
655655
startSpan.setAttributes({
656656
config: this.vite.config.configFile,
657657
})

packages/vitest/src/utils/traces.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,25 @@ export class Traces {
151151
return this.#otel.propagation.extract(activeContext, carrier)
152152
}
153153

154+
/**
155+
* @internal
156+
*/
157+
getContextFromEnv(env: Record<string, unknown>): Context {
158+
if (!this.#otel) {
159+
return this.#noopContext
160+
}
161+
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/env-carriers.md
162+
// some tools sets only `TRACEPARENT` but not `TRACESTATE`
163+
const carrier: OTELCarrier = {}
164+
if (typeof env.TRACEPARENT === 'string') {
165+
carrier.traceparent = env.TRACEPARENT
166+
}
167+
if (typeof env.TRACESTATE === 'string') {
168+
carrier.tracestate = env.TRACESTATE
169+
}
170+
return this.getContextFromCarrier(carrier)
171+
}
172+
154173
/**
155174
* @internal
156175
*/

0 commit comments

Comments
 (0)