Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(instrumentation-pino): instrument pino used in ESM #1831

Merged
merged 6 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/node/opentelemetry-instrumentation-pino/.tav.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pino:
- versions: "^8.0.0 || ^7.11.0 || 7.8.0 || 7.2.0 || ^6.13.1 || 5.17.0 || 5.14.0"
- versions: "^8.16.2 || 8.12.1 || 8.8.0 || 8.4.0 || 8.0.0 || ^7.11.0 || 7.8.0 || 7.2.0 || ^6.13.3 || 5.17.0 || 5.14.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for reviewers: This reduces the number of tested versions from 42 to 13

Before:

{"name":"pino","semver":"^8.0.0 || ^7.11.0 || 7.8.0 || 7.2.0 || ^6.13.1 || 5.17.0 || 5.14.0","cmds":["npm run test"],"versions":["5.14.0","5.17.0","6.13.1","6.13.2","6.13.3","7.2.0","6.13.4","7.8.0","6.14.0","7.11.0","8.0.0","8.1.0","8.2.0","8.3.0","8.3.1","8.4.0","8.4.1","8.4.2","8.5.0","8.6.0","8.6.1","8.7.0","8.8.0","8.9.0","8.10.0","8.11.0","8.12.0","8.12.1","8.14.0","8.14.1","8.14.2","8.15.0","8.15.1","8.15.2","8.15.3","8.15.4","8.15.5","8.15.6","8.15.7","8.16.0","8.16.1","8.16.2"],"numVersions":42}

after:

{"name":"pino","semver":"^8.16.2 || 8.12.1 || 8.8.0 || 8.4.0 || 8.0.0 || ^7.11.0 || 7.8.0 || 7.2.0 || ^6.13.3 || 5.17.0 || 5.14.0","cmds":["npm run test"],"versions":["5.14.0","5.17.0","6.13.3","7.2.0","6.13.4","7.8.0","6.14.0","7.11.0","8.0.0","8.4.0","8.8.0","8.12.1","8.16.2"],"numVersions":13}

node: ">=14"
commands: npm run test
- versions: "^7.11.0 || 7.8.0 || 7.2.0 || ^6.13.1 || 5.17.0 || 5.14.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"devDependencies": {
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/context-async-hooks": "^1.8.0",
"@opentelemetry/contrib-test-utils": "^0.35.0",
"@opentelemetry/sdk-trace-base": "^1.8.0",
"@opentelemetry/sdk-trace-node": "^1.8.0",
"@types/mocha": "7.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ export class PinoInstrumentation extends InstrumentationBase {
new InstrumentationNodeModuleDefinition<any>(
'pino',
pinoVersions,
(pinoModule, moduleVersion) => {
(module, moduleVersion?: string) => {
diag.debug(`Applying patch for pino@${moduleVersion}`);
const isESM = module[Symbol.toStringTag] === 'Module';
const moduleExports = isESM ? module.default : module;
const instrumentation = this;
const patchedPino = Object.assign((...args: unknown[]) => {
if (args.length === 0) {
return pinoModule({
return moduleExports({
mixin: instrumentation._getMixinFunction(),
});
}
Expand All @@ -61,21 +63,25 @@ export class PinoInstrumentation extends InstrumentationBase {
args.splice(0, 0, {
mixin: instrumentation._getMixinFunction(),
});
return pinoModule(...args);
return moduleExports(...args);
}
}

args[0] = instrumentation._combineOptions(args[0]);

return pinoModule(...args);
}, pinoModule);
return moduleExports(...args);
}, moduleExports);

if (typeof patchedPino.pino === 'function') {
patchedPino.pino = patchedPino;
}
if (typeof patchedPino.default === 'function') {
patchedPino.default = patchedPino;
}
if (isESM) {
module.pino = patchedPino;
module.default = patchedPino;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codecov / codecov/patch
plugins/node/opentelemetry-instrumentation-pino/src/instrumentation.ts#L82-L83
Added lines #L82 - L83 were not covered by tests

These lines actually are covered by the test, but I guess the out of process runTestFixture doesn't catch that.


return patchedPino;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Use pino from an ES module:
// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-pino.mjs

import { trace } from '@opentelemetry/api';
import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils';

import { PinoInstrumentation } from '../../build/src/index.js';

const sdk = createTestNodeSdk({
serviceName: 'use-pino',
instrumentations: [
new PinoInstrumentation()
]
})
sdk.start();

// Test that both `import pino from 'pino'` and named import work.
// Using a named export requires pino >=6.
import pino, { pino as pinoNamedImport } from 'pino';
const logger = pino();
const loggerNamedImport = pinoNamedImport();

const tracer = trace.getTracer();
await tracer.startActiveSpan('manual', async (span) => {
logger.info('hi from logger')
loggerNamedImport.info('hi from loggerNamedImport')
span.end();
});

await sdk.shutdown();
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import {
import { context, trace, Span, INVALID_SPAN_CONTEXT } from '@opentelemetry/api';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import {
runTestFixture,
TestCollector,
} from '@opentelemetry/contrib-test-utils';
import { Writable } from 'stream';
import * as assert from 'assert';
import * as sinon from 'sinon';
Expand Down Expand Up @@ -303,4 +307,39 @@ describe('PinoInstrumentation', () => {
});
});
});

it('should work with ESM usage', async function () {
if (semver.lt(pino.version, '6.0.0')) {
// Pino v5 did not support named exports.
trentm marked this conversation as resolved.
Show resolved Hide resolved
this.skip();
} else {
let logRecords: any[];
await runTestFixture({
cwd: __dirname,
argv: ['fixtures/use-pino.mjs'],
env: {
NODE_OPTIONS:
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
NODE_NO_WARNINGS: '1',
},
checkResult: (err, stdout, _stderr) => {
assert.ifError(err);
logRecords = stdout
.trim()
.split('\n')
.map(ln => JSON.parse(ln));
assert.strictEqual(logRecords.length, 2);
},
checkCollector: (collector: TestCollector) => {
// Check that both log records had the trace-context of the span injected.
const spans = collector.sortedSpans;
assert.strictEqual(spans.length, 1);
logRecords.forEach(rec => {
assert.strictEqual(rec.trace_id, spans[0].traceId);
assert.strictEqual(rec.span_id, spans[0].spanId);
});
},
});
}
});
});
Loading