Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
drewcorlin1 committed Dec 21, 2023
1 parent 359d726 commit 96e2986
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
11 changes: 11 additions & 0 deletions plugins/node/opentelemetry-instrumentation-pino/test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export interface TestInstrumentation {
pino: typeof Pino;
}

export type TestInstrumentationAndContext = TestContext & TestInstrumentation;

export function assertRecord(
record: any,
span: Span,
Expand Down Expand Up @@ -108,3 +110,12 @@ export function setupInstrumentation(
const pino = require('pino');
return { instrumentation, pino };
}

export function setupInstrumentationAndInitTestContext(
config?: PinoInstrumentationConfig,
importType: 'global' | 'default' | 'pino' = 'global'
) {
const instrumentation = setupInstrumentation(config);
const context = initTestContext(instrumentation, importType);
return { ...instrumentation, ...context };
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ import {
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import {
TestContext,
TestInstrumentation,
TestInstrumentationAndContext,
assertInjection,
assertRecord,
initTestContext,
kMessage,
setupInstrumentation,
setupInstrumentationAndInitTestContext,
testInjection,
testNoInjection,
} from './common';
Expand All @@ -46,12 +45,11 @@ provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
context.setGlobalContextManager(new AsyncHooksContextManager());

describe('PinoInstrumentation', () => {
let testInstrumentation: TestInstrumentation;
let testContext: TestContext;
let testContext: TestInstrumentationAndContext;

describe('enabled instrumentation', () => {
beforeEach(() => {
testInstrumentation = setupInstrumentation();
testContext = initTestContext(testInstrumentation);
testContext = setupInstrumentationAndInitTestContext();
});

it('injects span context to records', () => {
Expand All @@ -68,8 +66,7 @@ describe('PinoInstrumentation', () => {
traceFlags: 'traceFlags',
};

testInstrumentation = setupInstrumentation({ logKeys });
testContext = initTestContext(testInstrumentation);
testContext = setupInstrumentationAndInitTestContext({ logKeys });

const span = tracer.startSpan('abc');
context.with(trace.setSpan(context.active(), span), () => {
Expand All @@ -79,10 +76,10 @@ describe('PinoInstrumentation', () => {

it('injects span context to records in default export', function () {
// @ts-expect-error the same function reexported
if (!testInstrumentation.pino.default) {
if (!testContext.pino.default) {
this.skip();
}
initTestContext(testInstrumentation, 'default');
initTestContext(testContext, 'default');
const span = tracer.startSpan('abc');
context.with(trace.setSpan(context.active(), span), () => {
testInjection(span, testContext);
Expand All @@ -91,10 +88,10 @@ describe('PinoInstrumentation', () => {

it('injects span context to records in named export', function () {
// @ts-expect-error the same function reexported
if (!testInstrumentation.pino.pino) {
if (!testContext.pino.pino) {
this.skip();
}
initTestContext(testInstrumentation, 'pino');
initTestContext(testContext, 'pino');
const span = tracer.startSpan('abc');
context.with(trace.setSpan(context.active(), span), () => {
testInjection(span, testContext);
Expand All @@ -112,11 +109,11 @@ describe('PinoInstrumentation', () => {

it('calls the users log hook', () => {
const span = tracer.startSpan('abc');
testInstrumentation.instrumentation.setConfig({
testContext.instrumentation.setConfig({
enabled: true,
logHook: (_span, record, level) => {
record['resource.service.name'] = 'test-service';
if (semver.satisfies(testInstrumentation.pino.version, '>= 7.9.0')) {
if (semver.satisfies(testContext.pino.version, '>= 7.9.0')) {
assert.strictEqual(level, 30);
}
},
Expand All @@ -141,7 +138,7 @@ describe('PinoInstrumentation', () => {

it('does not propagate exceptions from user hooks', () => {
const span = tracer.startSpan('abc');
testInstrumentation.instrumentation.setConfig({
testContext.instrumentation.setConfig({
enabled: true,
logHook: () => {
throw new Error('Oops');
Expand All @@ -157,8 +154,7 @@ describe('PinoInstrumentation', () => {
let stdoutSpy: sinon.SinonSpy;

beforeEach(() => {
testInstrumentation = setupInstrumentation();
testContext = initTestContext(testInstrumentation);
testContext = setupInstrumentationAndInitTestContext();

stdoutSpy = sinon.spy(process.stdout, 'write');
});
Expand All @@ -168,7 +164,7 @@ describe('PinoInstrumentation', () => {
});

it('does not fail when constructing logger without arguments', () => {
testContext.logger = testInstrumentation.pino();
testContext.logger = testContext.pino();
const span = tracer.startSpan('abc');
context.with(trace.setSpan(context.active(), span), () => {
testContext.logger.info(kMessage);
Expand All @@ -178,7 +174,7 @@ describe('PinoInstrumentation', () => {
});

it('preserves user options and adds a mixin', () => {
testContext.logger = testInstrumentation.pino(
testContext.logger = testContext.pino(
{ name: 'LogLog' },
testContext.stream
);
Expand All @@ -192,7 +188,7 @@ describe('PinoInstrumentation', () => {

describe('binary arguments', () => {
it('is possible to construct logger with undefined options', () => {
testContext.logger = testInstrumentation.pino(
testContext.logger = testContext.pino(
undefined as unknown as Pino.LoggerOptions,
testContext.stream
);
Expand All @@ -203,7 +199,7 @@ describe('PinoInstrumentation', () => {
});

it('preserves user mixins', () => {
testContext.logger = testInstrumentation.pino(
testContext.logger = testContext.pino(
{ name: 'LogLog', mixin: () => ({ a: 2, b: 'bar' }) },
testContext.stream
);
Expand All @@ -218,7 +214,7 @@ describe('PinoInstrumentation', () => {
});

it('ensures user mixin values take precedence', () => {
testContext.logger = testInstrumentation.pino(
testContext.logger = testContext.pino(
{
mixin() {
return { trace_id: '123' };
Expand Down

0 comments on commit 96e2986

Please sign in to comment.