aboutsummaryrefslogtreecommitdiffstats
path: root/vanilla/node_modules/vitest/dist/chunks/traces.CCmnQaNT.js
blob: b94a0c4bf5276c02f760b96ab19cf8f19c5f76d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
class Traces {
	/**
	* otel stands for OpenTelemetry
	*/
	#otel = null;
	#sdk = null;
	#init = null;
	#noopSpan = createNoopSpan();
	#noopContext = createNoopContext();
	#initStartTime = performance.now();
	#initEndTime = 0;
	#initRecorded = false;
	constructor(options) {
		if (options.enabled) {
			const apiInit = import('@opentelemetry/api').then((api) => {
				this.#otel = {
					tracer: api.trace.getTracer(options.tracerName || "vitest"),
					context: api.context,
					propagation: api.propagation,
					trace: api.trace,
					SpanKind: api.SpanKind,
					SpanStatusCode: api.SpanStatusCode
				};
			}).catch(() => {
				throw new Error(`"@opentelemetry/api" is not installed locally. Make sure you have setup OpenTelemetry instrumentation: https://vitest.dev/guide/open-telemetry`);
			});
			const sdkInit = (options.sdkPath ? import(
				/* @vite-ignore */
				options.sdkPath
) : Promise.resolve()).catch((cause) => {
				throw new Error(`Failed to import custom OpenTelemetry SDK script (${options.sdkPath}): ${cause.message}`);
			});
			this.#init = Promise.all([sdkInit, apiInit]).then(([sdk]) => {
				if (sdk != null) {
					if (sdk.default != null && typeof sdk.default === "object" && typeof sdk.default.shutdown === "function") this.#sdk = sdk.default;
					else if (options.watchMode !== true && process.env.VITEST_MODE !== "watch") console.warn(`OpenTelemetry instrumentation module (${options.sdkPath}) does not have a default export with a "shutdown" method. Vitest won't be able to ensure that all traces are processed in time. Try running Vitest in watch mode instead.`);
				}
			}).finally(() => {
				this.#initEndTime = performance.now();
				this.#init = null;
			});
		}
	}
	isEnabled() {
		return !!this.#otel;
	}
	/**
	* @internal
	*/
	async waitInit() {
		if (this.#init) await this.#init;
		return this;
	}
	/**
	* @internal
	*/
	recordInitSpan(context) {
		if (this.#initRecorded) return;
		this.#initRecorded = true;
		this.startSpan("vitest.runtime.traces", { startTime: this.#initStartTime }, context).end(this.#initEndTime);
	}
	/**
	* @internal
	*/
	startContextSpan(name, currentContext) {
		if (!this.#otel) return {
			span: this.#noopSpan,
			context: this.#noopContext
		};
		const activeContext = currentContext || this.#otel.context.active();
		const span = this.#otel.tracer.startSpan(name, {}, activeContext);
		return {
			span,
			context: this.#otel.trace.setSpan(activeContext, span)
		};
	}
	/**
	* @internal
	*/
	getContextFromCarrier(carrier) {
		if (!this.#otel) return this.#noopContext;
		const activeContext = this.#otel.context.active();
		if (!carrier) return activeContext;
		return this.#otel.propagation.extract(activeContext, carrier);
	}
	/**
	* @internal
	*/
	getContextFromEnv(env) {
		if (!this.#otel) return this.#noopContext;
		// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/env-carriers.md
		// some tools sets only `TRACEPARENT` but not `TRACESTATE`
		const carrier = {};
		if (typeof env.TRACEPARENT === "string") carrier.traceparent = env.TRACEPARENT;
		if (typeof env.TRACESTATE === "string") carrier.tracestate = env.TRACESTATE;
		return this.getContextFromCarrier(carrier);
	}
	/**
	* @internal
	*/
	getContextCarrier(context) {
		if (!this.#otel) return;
		const carrier = {};
		this.#otel.propagation.inject(context || this.#otel.context.active(), carrier);
		return carrier;
	}
	#callActiveSpan(span, callback) {
		const otel = this.#otel;
		let result;
		try {
			result = callback(span);
			if (result instanceof Promise) return result.catch((error) => {
				span.recordException({
					name: error.name,
					message: error.message,
					stack: error.stack
				});
				span.setStatus({ code: otel.SpanStatusCode.ERROR });
				throw error;
			}).finally(() => span.end());
			return result;
		} catch (error) {
			if (error instanceof Error) {
				span.recordException({
					name: error.name,
					message: error.message,
					stack: error.stack
				});
				span.setStatus({ code: otel.SpanStatusCode.ERROR });
			}
			throw error;
		} finally {
			// end sync callbcak
			if (!(result instanceof Promise)) span.end();
		}
	}
	/**
	* @internal
	*/
	$(name, optionsOrFn, fn) {
		const callback = typeof optionsOrFn === "function" ? optionsOrFn : fn;
		if (!this.#otel) return callback(this.#noopSpan);
		const otel = this.#otel;
		const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
		const context = options.context;
		if (context) return otel.tracer.startActiveSpan(name, options, context, (span) => this.#callActiveSpan(span, callback));
		return otel.tracer.startActiveSpan(name, options, (span) => this.#callActiveSpan(span, callback));
	}
	/**
	* @internal
	*/
	startSpan(name, options, context) {
		if (!this.#otel) return this.#noopSpan;
		const { tracer } = this.#otel;
		return tracer.startSpan(name, options, context);
	}
	// On browser mode, async context is not automatically propagated,
	// so we manually bind the `$` calls to the provided context.
	// TODO: this doesn't bind to user land's `@optelemetry/api` calls
	/**
	* @internal
	*/
	bind(context) {
		if (!this.#otel) return;
		const original = this.$.__original ?? this.$;
		this.$ = this.#otel.context.bind(context, original);
		this.$.__original = original;
	}
	/**
	* @internal
	*/
	async finish() {
		await this.#sdk?.shutdown();
	}
	/**
	* @internal
	*/
	async flush() {
		await this.#sdk?.forceFlush?.();
	}
}
function noopSpan() {
	return this;
}
function createNoopSpan() {
	return {
		setAttribute: noopSpan,
		setStatus: noopSpan,
		addEvent: noopSpan,
		addLink: noopSpan,
		addLinks: noopSpan,
		setAttributes: noopSpan,
		updateName: noopSpan,
		end: () => {},
		isRecording: () => false,
		recordException: noopSpan,
		spanContext() {
			return {
				spanId: "",
				traceFlags: 0,
				traceId: ""
			};
		}
	};
}
function noopContext() {
	return this;
}
function createNoopContext() {
	return {
		getValue: noopContext,
		setValue: noopContext,
		deleteValue: noopContext
	};
}

export { Traces as T };