| 50 | #initRecorded = false |
| 51 | |
| 52 | constructor(options: TracesOptions) { |
| 53 | if (options.enabled) { |
| 54 | const apiInit = import('@opentelemetry/api').then((api) => { |
| 55 | const otel = { |
| 56 | tracer: api.trace.getTracer(options.tracerName || 'vitest'), |
| 57 | context: api.context, |
| 58 | propagation: api.propagation, |
| 59 | trace: api.trace, |
| 60 | SpanKind: api.SpanKind, |
| 61 | SpanStatusCode: api.SpanStatusCode, |
| 62 | } |
| 63 | this.#otel = otel |
| 64 | }).catch(() => { |
| 65 | throw new Error(`"@opentelemetry/api" is not installed locally. Make sure you have setup OpenTelemetry instrumentation: https://vitest.dev/guide/open-telemetry`) |
| 66 | }) |
| 67 | const sdkInit = (options.sdkPath ? import(/* @vite-ignore */ options.sdkPath!) : Promise.resolve()).catch((cause) => { |
| 68 | throw new Error(`Failed to import custom OpenTelemetry SDK script (${options.sdkPath}): ${cause.message}`) |
| 69 | }) |
| 70 | this.#init = Promise.all([sdkInit, apiInit]).then(([sdk]) => { |
| 71 | if (sdk != null) { |
| 72 | if (sdk.default != null && typeof sdk.default === 'object' && typeof sdk.default.shutdown === 'function') { |
| 73 | this.#sdk = sdk.default |
| 74 | } |
| 75 | else if (options.watchMode !== true && process.env.VITEST_MODE !== 'watch') { |
| 76 | 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.`) |
| 77 | } |
| 78 | } |
| 79 | }).finally(() => { |
| 80 | this.#initEndTime = performance.now() |
| 81 | this.#init = null |
| 82 | }) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public isEnabled(): boolean { |
| 87 | return !!this.#otel |