-
Notifications
You must be signed in to change notification settings - Fork 544
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
Changes from 3 commits
626c3b7
e768ee3
212055f
0bde3ad
b10ec11
13200b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,12 +41,14 @@ | |
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(), | ||
}); | ||
} | ||
|
@@ -61,21 +63,25 @@ | |
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
These lines actually are covered by the test, but I guess the out of process |
||
|
||
return patchedPino; | ||
} | ||
|
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(); |
There was a problem hiding this comment.
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:
after: