Skip to content

Commit 4e4d22e

Browse files
UgzuzgFlarnarauno56
authored
feat: improve pino instrumentation by patching additional exports of the same function (open-telemetry#1108)
Co-authored-by: Gerhard Stöbich <[email protected]> Co-authored-by: Rauno Viskus <[email protected]>
1 parent d3cb60d commit 4e4d22e

File tree

7 files changed

+62
-24
lines changed

7 files changed

+62
-24
lines changed

.github/workflows/unit-test.yml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
- node: "12"
2929
lerna-extra-args: >-
3030
--ignore @opentelemetry/instrumentation-redis-4
31+
--ignore @opentelemetry/instrumentation-pino
3132
runs-on: ubuntu-latest
3233
services:
3334
memcached:
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
pino:
2-
versions: "^7.11.0 || 7.8.0 || 7.2.0 || ^6.13.1 || 5.17.0 || 5.14.0"
3-
commands: npm run test
2+
- versions: "^8.0.0 || ^7.11.0 || 7.8.0 || 7.2.0 || ^6.13.1 || 5.17.0 || 5.14.0"
3+
node: ">=14"
4+
commands: npm run test
45

5-
# Fix missing `contrib-test-utils` package
6-
pretest: npm run --prefix ../../../ lerna:link
6+
# Fix missing `contrib-test-utils` package
7+
pretest: npm run --prefix ../../../ lerna:link
8+
- versions: "^7.11.0 || 7.8.0 || 7.2.0 || ^6.13.1 || 5.17.0 || 5.14.0"
9+
node: ">=8 <14"
10+
commands: npm run test
11+
12+
# Fix missing `contrib-test-utils` package
13+
pretest: npm run --prefix ../../../ lerna:link

plugins/node/opentelemetry-instrumentation-pino/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ When no span context is active or the span context is invalid, injection is skip
5555

5656
### Supported versions
5757

58-
`>=5.14.0 <8`
58+
`>=5.14.0 <9`
5959

6060
## Useful links
6161

plugins/node/opentelemetry-instrumentation-pino/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@
5858
"gts": "3.1.0",
5959
"mocha": "7.2.0",
6060
"nyc": "15.1.0",
61+
"pino": "8.3.1",
6162
"rimraf": "3.0.2",
63+
"semver": "7.3.5",
6264
"sinon": "14.0.0",
6365
"test-all-versions": "5.0.1",
6466
"ts-mocha": "10.0.0",
6567
"typescript": "4.3.5"
6668
},
6769
"dependencies": {
68-
"@opentelemetry/instrumentation": "^0.32.0",
69-
"pino": "7.10.0",
70-
"semver": "^7.3.5"
70+
"@opentelemetry/instrumentation": "^0.32.0"
7171
},
7272
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-pino#readme"
7373
}

plugins/node/opentelemetry-instrumentation-pino/src/instrumentation.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ import {
2626
InstrumentationNodeModuleDefinition,
2727
safeExecuteInTheMiddle,
2828
} from '@opentelemetry/instrumentation';
29-
import { Pino, PinoInstrumentationConfig } from './types';
29+
import { PinoInstrumentationConfig } from './types';
3030
import { VERSION } from './version';
31-
import type { pino } from 'pino';
3231

33-
const pinoVersions = ['>=5.14.0 <8'];
32+
const pinoVersions = ['>=5.14.0 <9'];
3433

3534
export class PinoInstrumentation extends InstrumentationBase {
3635
constructor(config: PinoInstrumentationConfig = {}) {
@@ -39,7 +38,7 @@ export class PinoInstrumentation extends InstrumentationBase {
3938

4039
protected init() {
4140
return [
42-
new InstrumentationNodeModuleDefinition<Pino>(
41+
new InstrumentationNodeModuleDefinition<any>(
4342
'pino',
4443
pinoVersions,
4544
pinoModule => {
@@ -61,17 +60,22 @@ export class PinoInstrumentation extends InstrumentationBase {
6160
args.splice(0, 0, {
6261
mixin: instrumentation._getMixinFunction(),
6362
});
64-
return pinoModule(...(args as Parameters<Pino>));
63+
return pinoModule(...args);
6564
}
6665
}
6766

68-
args[0] = instrumentation._combineOptions(
69-
args[0] as pino.LoggerOptions
70-
);
67+
args[0] = instrumentation._combineOptions(args[0]);
7168

72-
return pinoModule(...(args as Parameters<Pino>));
69+
return pinoModule(...args);
7370
}, pinoModule);
7471

72+
if (typeof patchedPino.pino === 'function') {
73+
patchedPino.pino = patchedPino;
74+
}
75+
if (typeof patchedPino.default === 'function') {
76+
patchedPino.default = patchedPino;
77+
}
78+
7579
return patchedPino;
7680
}
7781
),
@@ -135,7 +139,7 @@ export class PinoInstrumentation extends InstrumentationBase {
135139
};
136140
}
137141

138-
private _combineOptions(options?: pino.LoggerOptions) {
142+
private _combineOptions(options?: any) {
139143
if (options === undefined) {
140144
return { mixin: this._getMixinFunction() };
141145
}

plugins/node/opentelemetry-instrumentation-pino/src/types.ts

-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import { Span } from '@opentelemetry/api';
1818
import { InstrumentationConfig } from '@opentelemetry/instrumentation';
19-
import type { pino } from 'pino';
2019

2120
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2221
export type LogHookFunction = (
@@ -28,5 +27,3 @@ export type LogHookFunction = (
2827
export interface PinoInstrumentationConfig extends InstrumentationConfig {
2928
logHook?: LogHookFunction;
3029
}
31-
32-
export type Pino = typeof pino;

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

+32-3
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,16 @@ describe('PinoInstrumentation', () => {
7575
return record;
7676
}
7777

78-
function init() {
78+
function init(importType: 'global' | 'default' | 'pino' = 'global') {
7979
stream = new Writable();
8080
stream._write = () => {};
8181
writeSpy = sinon.spy(stream, 'write');
82-
logger = pino(stream);
82+
if (importType === 'global') {
83+
logger = pino(stream);
84+
} else {
85+
// @ts-expect-error the same function reexported
86+
logger = pino[importType](stream);
87+
}
8388
}
8489

8590
before(() => {
@@ -100,6 +105,30 @@ describe('PinoInstrumentation', () => {
100105
});
101106
});
102107

108+
it('injects span context to records in default export', function () {
109+
// @ts-expect-error the same function reexported
110+
if (!pino.default) {
111+
this.skip();
112+
}
113+
init('default');
114+
const span = tracer.startSpan('abc');
115+
context.with(trace.setSpan(context.active(), span), () => {
116+
testInjection(span);
117+
});
118+
});
119+
120+
it('injects span context to records in named export', function () {
121+
// @ts-expect-error the same function reexported
122+
if (!pino.pino) {
123+
this.skip();
124+
}
125+
init('pino');
126+
const span = tracer.startSpan('abc');
127+
context.with(trace.setSpan(context.active(), span), () => {
128+
testInjection(span);
129+
});
130+
});
131+
103132
it('injects span context to child logger records', () => {
104133
const span = tracer.startSpan('abc');
105134
context.with(trace.setSpan(context.active(), span), () => {
@@ -243,7 +272,7 @@ describe('PinoInstrumentation', () => {
243272
instrumentation.enable();
244273
});
245274

246-
beforeEach(init);
275+
beforeEach(() => init());
247276

248277
it('does not inject span context', () => {
249278
const span = tracer.startSpan('abc');

0 commit comments

Comments
 (0)