diff --git a/lib/instrumentation/index.spec.ts b/lib/instrumentation/index.spec.ts index d8482d0490a..49e55521f52 100644 --- a/lib/instrumentation/index.spec.ts +++ b/lib/instrumentation/index.spec.ts @@ -1,3 +1,4 @@ +import { createRequire } from 'node:module'; import * as api from '@opentelemetry/api'; import { ProxyTracerProvider } from '@opentelemetry/api'; import { @@ -153,6 +154,25 @@ describe('instrumentation/index', () => { }); }); + describe('BunyanInstrumentation', () => { + // OpenTelemetry's context propagation currently uses `AsyncLocalStorage`, which does not behave the same way in vitest worker threads as in a real Node.js process, so we cannot write a full end-to-end here to validate the `span_id`, `trace_id` and `trace_flags` are set + // + // Claude Sonnet 4.6 suggests that we instead create an (admittedly brittle) test to validate that this is marked as `__wrapped`. + it('patches bunyan Logger._emit when tracing is enabled', () => { + process.env.RENOVATE_TRACING_CONSOLE_EXPORTER = 'true'; + init(); + + const bunyan = createRequire(import.meta.url)( + 'bunyan', + ) as typeof import('bunyan'); + + // shimmer marks wrapped functions with __wrapped = true + expect( + (bunyan.prototype as unknown as Record)._emit, + ).toHaveProperty('__wrapped', true); + }); + }); + describe('instrument', () => { it('should return result', () => { const value = 'testResult'; diff --git a/lib/logger/bunyan.ts b/lib/logger/bunyan.ts index c3035c80db4..b2b195fd1d1 100644 --- a/lib/logger/bunyan.ts +++ b/lib/logger/bunyan.ts @@ -1,9 +1,16 @@ +import { createRequire } from 'node:module'; import { isNonEmptyStringAndNotWhitespace, isString, isUndefined, } from '@sindresorhus/is'; -import * as bunyan from 'bunyan'; + +// Use createRequire so that @opentelemetry/instrumentation-bunyan can patch +// bunyan via require-in-the-middle. A plain ESM `import` bypasses the hook. +const bunyan = createRequire(import.meta.url)( + 'bunyan', +) as typeof import('bunyan'); + import fs from 'fs-extra'; import upath from 'upath'; import cmdSerializer from './cmd-serializer.ts';