forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(instrumentation-pino): instrument pino used in ESM (open-telemetr…
…y#1831) This also reduces the number of pino versions tested with test-all-versions test (from 42 to 13, currently).
- Loading branch information
Showing
7 changed files
with
171 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(), | ||
}); | ||
} | ||
|
@@ -61,21 +63,28 @@ 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) { | ||
if (module.pino) { | ||
// This was added in [email protected] (https://github.com/pinojs/pino/pull/936). | ||
module.pino = patchedPino; | ||
} | ||
module.default = patchedPino; | ||
} | ||
|
||
return patchedPino; | ||
} | ||
|
42 changes: 42 additions & 0 deletions
42
plugins/node/opentelemetry-instrumentation-pino/test/fixtures/use-pino-default-import.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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-default-import.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(); | ||
|
||
import pino from 'pino'; | ||
const logger = pino(); | ||
|
||
const tracer = trace.getTracer(); | ||
await tracer.startActiveSpan('manual', async (span) => { | ||
logger.info('hi from logger') | ||
span.end(); | ||
}); | ||
|
||
await sdk.shutdown(); |
42 changes: 42 additions & 0 deletions
42
plugins/node/opentelemetry-instrumentation-pino/test/fixtures/use-pino-named-import.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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-named-import.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(); | ||
|
||
import { pino } from 'pino'; // named import, supported with pino >=6.8.0 | ||
const logger = pino(); | ||
|
||
const tracer = trace.getTracer(); | ||
await tracer.startActiveSpan('manual', async (span) => { | ||
logger.info('hi from logger') | ||
span.end(); | ||
}); | ||
|
||
await sdk.shutdown(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters