Skip to content

Commit 66e6ce4

Browse files
test(instrumentation-pino): add test for ESM (#2176)
1 parent ba0de30 commit 66e6ce4

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

plugins/node/opentelemetry-instrumentation-pino/test/pino-enabled.test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ import {
3333
testInjection,
3434
testNoInjection,
3535
} from './common';
36+
import {
37+
runTestFixture,
38+
TestCollector,
39+
} from '@opentelemetry/contrib-test-utils';
3640

3741
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
3842
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
@@ -235,4 +239,74 @@ describe('PinoInstrumentation', () => {
235239
});
236240
});
237241
});
242+
describe('ESM usage', () => {
243+
beforeEach(() => {
244+
testContext = setupInstrumentationAndInitTestContext();
245+
});
246+
it('should work with ESM default import', async function () {
247+
testContext = setupInstrumentationAndInitTestContext();
248+
let logRecords: any[];
249+
await runTestFixture({
250+
cwd: __dirname,
251+
argv: ['fixtures/use-pino-default-import.mjs'],
252+
env: {
253+
NODE_OPTIONS:
254+
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
255+
NODE_NO_WARNINGS: '1',
256+
},
257+
checkResult: (err, stdout, _stderr) => {
258+
assert.ifError(err);
259+
logRecords = stdout
260+
.trim()
261+
.split('\n')
262+
.map(ln => JSON.parse(ln));
263+
assert.strictEqual(logRecords.length, 1);
264+
},
265+
checkCollector: (collector: TestCollector) => {
266+
// Check that both log records had the trace-context of the span injected.
267+
const spans = collector.sortedSpans;
268+
assert.strictEqual(spans.length, 1);
269+
logRecords.forEach(rec => {
270+
assert.strictEqual(rec.trace_id, spans[0].traceId);
271+
assert.strictEqual(rec.span_id, spans[0].spanId);
272+
});
273+
},
274+
});
275+
});
276+
277+
it('should work with ESM named import', async function () {
278+
if (semver.lt(testContext.pino.version, '6.8.0')) {
279+
// Pino 6.8.0 added named ESM exports (https://github.com/pinojs/pino/pull/936).
280+
this.skip();
281+
} else {
282+
let logRecords: any[];
283+
await runTestFixture({
284+
cwd: __dirname,
285+
argv: ['fixtures/use-pino-named-import.mjs'],
286+
env: {
287+
NODE_OPTIONS:
288+
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
289+
NODE_NO_WARNINGS: '1',
290+
},
291+
checkResult: (err, stdout, _stderr) => {
292+
assert.ifError(err);
293+
logRecords = stdout
294+
.trim()
295+
.split('\n')
296+
.map(ln => JSON.parse(ln));
297+
assert.strictEqual(logRecords.length, 1);
298+
},
299+
checkCollector: (collector: TestCollector) => {
300+
// Check that both log records had the trace-context of the span injected.
301+
const spans = collector.sortedSpans;
302+
assert.strictEqual(spans.length, 1);
303+
logRecords.forEach(rec => {
304+
assert.strictEqual(rec.trace_id, spans[0].traceId);
305+
assert.strictEqual(rec.span_id, spans[0].spanId);
306+
});
307+
},
308+
});
309+
}
310+
});
311+
});
238312
});

0 commit comments

Comments
 (0)