Skip to content

Commit

Permalink
feat: send log level to pino hook (open-telemetry#967)
Browse files Browse the repository at this point in the history
* send log level to pino hook
* add comment to hook log, change to semver.satisfies

Co-authored-by: Rauno Viskus <[email protected]>
  • Loading branch information
nozik and rauno56 authored Apr 11, 2022
1 parent bcbdeb7 commit cfb0b7a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion plugins/node/opentelemetry-instrumentation-pino/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ registerInstrumentations({
instrumentations: [
new PinoInstrumentation({
// Optional hook to insert additional context to log object.
logHook: (span, record) => {
logHook: (span, record, level) => {
record['resource.service.name'] = provider.resource.attributes['service.name'];
},
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.27.0",
"pino": "7.2.0",
"pino": "7.10.0",
"semver": "^7.3.5"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export class PinoInstrumentation extends InstrumentationBase {
const instrumentation = this;
const patchedPino = Object.assign((...args: unknown[]) => {
if (args.length == 0) {
return pinoModule({ mixin: instrumentation._getMixinFunction() });
return pinoModule({
mixin: instrumentation._getMixinFunction(),
});
}

if (args.length == 1) {
Expand Down Expand Up @@ -84,15 +86,15 @@ export class PinoInstrumentation extends InstrumentationBase {
this._config = config;
}

private _callHook(span: Span, record: Record<string, string>) {
private _callHook(span: Span, record: Record<string, string>, level: number) {
const hook = this.getConfig().logHook;

if (!hook) {
return;
}

safeExecuteInTheMiddle(
() => hook(span, record),
() => hook(span, record, level),
err => {
if (err) {
diag.error('pino instrumentation: error calling logHook', err);
Expand All @@ -104,7 +106,7 @@ export class PinoInstrumentation extends InstrumentationBase {

private _getMixinFunction() {
const instrumentation = this;
return function otelMixin() {
return function otelMixin(_context: object, level: number) {
if (!instrumentation.isEnabled()) {
return {};
}
Expand All @@ -127,7 +129,7 @@ export class PinoInstrumentation extends InstrumentationBase {
trace_flags: `0${spanContext.traceFlags.toString(16)}`,
};

instrumentation._callHook(span, record);
instrumentation._callHook(span, record, level);

return record;
};
Expand All @@ -146,8 +148,11 @@ export class PinoInstrumentation extends InstrumentationBase {
const originalMixin = options.mixin;
const otelMixin = this._getMixinFunction();

options.mixin = () => {
return Object.assign(otelMixin(), originalMixin());
options.mixin = (context: object, level: number) => {
return Object.assign(
otelMixin(context, level),
originalMixin(context, level)
);
};

return options;
Expand Down
6 changes: 5 additions & 1 deletion plugins/node/opentelemetry-instrumentation-pino/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import { InstrumentationConfig } from '@opentelemetry/instrumentation';
import type { pino } from 'pino';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type LogHookFunction = (span: Span, record: Record<string, any>) => void;
export type LogHookFunction = (
span: Span,
record: Record<string, any>,
level?: number // Available in pino >= 7.9.0
) => void;

export interface PinoInstrumentationConfig extends InstrumentationConfig {
logHook?: LogHookFunction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { Writable } from 'stream';
import * as assert from 'assert';
import * as sinon from 'sinon';
import * as semver from 'semver';
import type { pino as Pino } from 'pino';

import { PinoInstrumentation } from '../src';
Expand Down Expand Up @@ -112,8 +113,11 @@ describe('PinoInstrumentation', () => {
const span = tracer.startSpan('abc');
instrumentation.setConfig({
enabled: true,
logHook: (_span, record) => {
logHook: (_span, record, level) => {
record['resource.service.name'] = 'test-service';
if (semver.satisfies(pino.version, '>= 7.9.0')) {
assert.strictEqual(level, 30);
}
},
});
context.with(trace.setSpan(context.active(), span), () => {
Expand Down

0 comments on commit cfb0b7a

Please sign in to comment.