|
| 1 | +import { tracingChannel } from 'node:diagnostics_channel'; |
| 2 | +import type { IntegrationFn, LogSeverityLevel } from '@sentry/core'; |
| 3 | +import { |
| 4 | + _INTERNAL_captureLog, |
| 5 | + addExceptionMechanism, |
| 6 | + captureException, |
| 7 | + captureMessage, |
| 8 | + severityLevelFromString, |
| 9 | + withScope, |
| 10 | +} from '@sentry/core'; |
| 11 | +import { addInstrumentationConfig } from '../sdk/injectLoader'; |
| 12 | + |
| 13 | +type LevelMapping = { |
| 14 | + // Fortunately pino uses the same levels as Sentry |
| 15 | + labels: { [level: number]: LogSeverityLevel }; |
| 16 | +}; |
| 17 | + |
| 18 | +type Pino = { |
| 19 | + levels: LevelMapping; |
| 20 | +}; |
| 21 | + |
| 22 | +type MergeObject = { |
| 23 | + [key: string]: unknown; |
| 24 | + err?: Error; |
| 25 | +}; |
| 26 | + |
| 27 | +type PinoHookArgs = { |
| 28 | + self: Pino; |
| 29 | + arguments: [MergeObject, string, number]; |
| 30 | +}; |
| 31 | + |
| 32 | +type Options = { |
| 33 | + /** |
| 34 | + * Levels that trigger capturing of events. |
| 35 | + * |
| 36 | + * @default ["error", "fatal"] |
| 37 | + */ |
| 38 | + eventLevels?: LogSeverityLevel[]; |
| 39 | + /** |
| 40 | + * By default, Sentry will mark captured console messages as handled. |
| 41 | + * Set this to `false` if you want to mark them as unhandled instead. |
| 42 | + * |
| 43 | + * @default true |
| 44 | + */ |
| 45 | + handled?: boolean; |
| 46 | +}; |
| 47 | + |
| 48 | +function attributesFromObject(obj: object, attr: Record<string, unknown>, key?: string): Record<string, unknown> { |
| 49 | + for (const [k, v] of Object.entries(obj)) { |
| 50 | + const newKey = key ? `${key}.${k}` : k; |
| 51 | + if (v && typeof v === 'object' && !Array.isArray(v) && !(v instanceof Error)) { |
| 52 | + attributesFromObject(v as object, attr, newKey); |
| 53 | + } else { |
| 54 | + attr[newKey] = v; |
| 55 | + } |
| 56 | + } |
| 57 | + return attr; |
| 58 | +} |
| 59 | + |
| 60 | +export const pinoIntegration = ((options: Options = { eventLevels: ['error', 'fatal'], handled: true }) => { |
| 61 | + return { |
| 62 | + name: 'Pino', |
| 63 | + setup: () => { |
| 64 | + addInstrumentationConfig({ |
| 65 | + channelName: 'pino-log', |
| 66 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 67 | + // @ts-ignore https://github.com/apm-js-collab/orchestrion-js/pull/35 |
| 68 | + module: { name: 'pino', versionRange: '>=8.0.0', filePath: 'lib/tools.js' }, |
| 69 | + functionQuery: { |
| 70 | + functionName: 'asJson', |
| 71 | + kind: 'Sync', |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + const channel = tracingChannel('orchestrion:pino:pino-log'); |
| 76 | + |
| 77 | + channel.start.subscribe(data => { |
| 78 | + const { self, arguments: args } = data as PinoHookArgs; |
| 79 | + const [obj, message, levelNumber] = args; |
| 80 | + const level = self?.levels?.labels?.[levelNumber] || 'info'; |
| 81 | + |
| 82 | + const attributes = attributesFromObject(obj, { |
| 83 | + 'sentry.origin': 'auto.logging.pino', |
| 84 | + 'sentry.pino.level': levelNumber, |
| 85 | + }); |
| 86 | + |
| 87 | + _INTERNAL_captureLog({ level, message, attributes }); |
| 88 | + |
| 89 | + if (options.eventLevels?.includes(level)) { |
| 90 | + const captureContext = { |
| 91 | + level: severityLevelFromString(level), |
| 92 | + }; |
| 93 | + |
| 94 | + withScope(scope => { |
| 95 | + scope.addEventProcessor(event => { |
| 96 | + event.logger = 'pino'; |
| 97 | + |
| 98 | + addExceptionMechanism(event, { |
| 99 | + handled: !!options.handled, |
| 100 | + type: 'pino', |
| 101 | + }); |
| 102 | + |
| 103 | + return event; |
| 104 | + }); |
| 105 | + |
| 106 | + if (obj.err) { |
| 107 | + captureException(obj.err, captureContext); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + captureMessage(message, captureContext); |
| 112 | + }); |
| 113 | + } |
| 114 | + }); |
| 115 | + }, |
| 116 | + }; |
| 117 | +}) satisfies IntegrationFn; |
0 commit comments