-
Notifications
You must be signed in to change notification settings - Fork 992
feat: ESM support for instrumentation #3698
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
43fd59e
docs: add example for esm-http-ts
JamieDanielson e5215fe
fix: use instr version not node version
JamieDanielson 3c51c6d
deps: add import-in-the-middle
JamieDanielson 8444af3
feat: add iitm for esm instr
JamieDanielson e4478d9
docs: add readme notes from valentin
JamieDanielson 1434b27
wip: add test based on valentin's test
JamieDanielson a533f9f
remove changes to ritm init
JamieDanielson a43089b
update example - use build dir, add readme
JamieDanielson 85a2e8f
add changelog entry
JamieDanielson e0b5c44
dont use ritm singleton for iitm
JamieDanielson 63477c5
fix up call for new iitm
JamieDanielson 1188c02
Merge branch 'main' into esm-support
JamieDanielson 33e544c
Merge branch 'main' into esm-support
lizthegrey bfe51f5
wrap function refactor
pkanal 7110c37
clean up
pkanal 3ddb3a9
Merge pull request #1 from honeycombio/purvi/esm-tests
JamieDanielson 3f3d9a2
Merge branch 'main' into esm-support
JamieDanielson 2936a16
update changelog: it takes a village
JamieDanielson 4a10129
small cleanup on test
JamieDanielson 50363c5
version should be node version, not instr version
JamieDanielson c9922c3
run lint:fix for prettier error
JamieDanielson b722486
Merge pull request #2 from honeycombio/jamie.http-node-version
pkanal 4ac1ee1
base unwrap functionality
pkanal a346531
move esm specific wrap logic to node class
pkanal 1ca6b55
fix types
pkanal 4401576
add masswrap & massunwrap
pkanal 86b6837
handle edge cases for masswrap and massunwrap
pkanal da694c7
Merge pull request #3 from honeycombio/purvi.unwrap
JamieDanielson 1612855
Merge branch 'main' into esm-support
JamieDanielson e924c99
Ship shimmer types (#5)
pkanal b894bcd
Merge branch 'main' into esm-support
pkanal 68c43c9
use --experimental-meta-resolve option with hook file (#8)
pkanal a2a74f8
Merge branch 'main' into esm-support
pkanal a529c8c
fix markdown
pkanal ddc8dc5
Merge branch 'main' into esm-support
pkanal 42eb5ba
address PR feedback (#9)
pkanal 4f71df8
Update experimental/packages/opentelemetry-instrumentation/hook.js
pkanal 6a052b1
Update experimental/packages/opentelemetry-instrumentation/hook.mjs
pkanal 58d34c0
change back to process.versions.node (#10)
pkanal 33409da
address pr feedback (#11)
pkanal 1f017b0
remove `--experimental-import-meta-resolve` flag (#12)
pkanal 16109d3
Merge branch 'main' into esm-support
JamieDanielson bcaef1c
Merge branch 'main' into esm-support
JamieDanielson c590efc
Merge branch 'main' into esm-support
JamieDanielson 59047bd
remove import limitation from README
pkanal 0167a10
add esm example to examples readme
pkanal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
use --experimental-meta-resolve option with hook file (#8)
Allows `hook.mjs` file to exported from the instrumentation package if the user also uses the `--experimental-meta-resolve` flag. If a user doesn't want to use this flag, they can still reference `import-in-the-middle/hook.mjs`. I figure this option is safe because this is such a Node targeted feature that I'm not worried about someone trying to run this in the browser. The code path to the ESM hook is entirely contained in the `node` folder which wouldn't be available if someone were to run this package in the browser and they wouldn't be using the hook anyway. --------- Co-authored-by: Jamie Danielson <[email protected]>
- Loading branch information
commit 68c43c91b69ec577c0506215df60c6b759c1f177
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
198 changes: 198 additions & 0 deletions
198
experimental/packages/opentelemetry-instrumentation/hook.js
This file contains hidden or 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,198 @@ | ||
| /*! | ||
| * Copyright The OpenTelemetry Authors | ||
pkanal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License. | ||
| // | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc. | ||
|
|
||
| const specifiers = new Map(); | ||
| const isWin = process.platform === 'win32'; | ||
|
|
||
| // FIXME: Typescript extensions are added temporarily until we find a better | ||
| // way of supporting arbitrary extensions | ||
| const EXTENSION_RE = /\.(js|mjs|cjs|ts|mts|cts)$/; | ||
Flarna marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const NODE_VERSION = process.versions.node.split('.'); | ||
| const NODE_MAJOR = Number(NODE_VERSION[0]); | ||
| const NODE_MINOR = Number(NODE_VERSION[1]); | ||
|
|
||
| let entrypoint; | ||
|
|
||
| function hasIitm(url) { | ||
| try { | ||
| return new URL(url).searchParams.has('iitm'); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| function isIitm(url, meta) { | ||
| return url === meta.url || url === meta.url.replace('hook.mjs', 'hook.js'); | ||
| } | ||
|
|
||
| function deleteIitm(url) { | ||
| let resultUrl; | ||
| try { | ||
| const urlObj = new URL(url); | ||
| if (urlObj.searchParams.has('iitm')) { | ||
| urlObj.searchParams.delete('iitm'); | ||
| resultUrl = urlObj.href; | ||
| if (resultUrl.startsWith('file:node:')) { | ||
| resultUrl = resultUrl.replace('file:', ''); | ||
| } | ||
| if (resultUrl.startsWith('file:///node:')) { | ||
| resultUrl = resultUrl.replace('file:///', ''); | ||
| } | ||
| } else { | ||
| resultUrl = urlObj.href; | ||
| } | ||
| } catch { | ||
| resultUrl = url; | ||
| } | ||
| return resultUrl; | ||
| } | ||
|
|
||
| function isNode16AndBiggerOrEqualsThan16_17_0() { | ||
| return NODE_MAJOR === 16 && NODE_MINOR >= 17; | ||
| } | ||
|
|
||
| function isFileProtocol(urlObj) { | ||
| return urlObj.protocol === 'file:'; | ||
| } | ||
|
|
||
| function isNodeProtocol(urlObj) { | ||
| return urlObj.protocol === 'node:'; | ||
| } | ||
|
|
||
| function needsToAddFileProtocol(urlObj) { | ||
| if (NODE_MAJOR === 17) { | ||
| return !isFileProtocol(urlObj); | ||
| } | ||
| if (isNode16AndBiggerOrEqualsThan16_17_0()) { | ||
| return !isFileProtocol(urlObj) && !isNodeProtocol(urlObj); | ||
| } | ||
| return !isFileProtocol(urlObj) && NODE_MAJOR < 18; | ||
| } | ||
|
|
||
| function addIitm(url) { | ||
| const urlObj = new URL(url); | ||
| urlObj.searchParams.set('iitm', 'true'); | ||
| return needsToAddFileProtocol(urlObj) ? 'file:' + urlObj.href : urlObj.href; | ||
| } | ||
|
|
||
| async function createHook(meta) { | ||
| async function resolve(specifier, context, parentResolve) { | ||
| const { parentURL = '' } = context; | ||
| const newSpecifier = deleteIitm(specifier); | ||
| if (isWin && parentURL.indexOf('file:node') === 0) { | ||
| context.parentURL = ''; | ||
| } | ||
| const url = await parentResolve(newSpecifier, context, parentResolve); | ||
| if (parentURL === '' && !EXTENSION_RE.test(url.url)) { | ||
| entrypoint = url.url; | ||
| return { url: url.url, format: 'commonjs' }; | ||
| } | ||
|
|
||
| if (isIitm(parentURL, meta) || hasIitm(parentURL)) { | ||
| return url; | ||
| } | ||
|
|
||
| if (context.importAssertions && context.importAssertions.type === 'json') { | ||
| return url; | ||
| } | ||
|
|
||
| specifiers.set(url.url, specifier); | ||
|
|
||
| return { | ||
| url: addIitm(url.url), | ||
| shortCircuit: true, | ||
| }; | ||
| } | ||
|
|
||
| const iitmURL = await meta.resolve('import-in-the-middle/lib/register.js'); | ||
| async function getSource(url, context, parentGetSource) { | ||
| if (hasIitm(url)) { | ||
| const realUrl = deleteIitm(url); | ||
| const realModule = await import(realUrl); | ||
| const exportNames = Object.keys(realModule); | ||
| return { | ||
| source: ` | ||
| import { register } from '${iitmURL}' | ||
| import * as namespace from '${url}' | ||
| const set = {} | ||
| ${exportNames | ||
| .map( | ||
| n => ` | ||
| let $${n} = namespace.${n} | ||
| export { $${n} as ${n} } | ||
| set.${n} = (v) => { | ||
| $${n} = v | ||
| return true | ||
| } | ||
| ` | ||
| ) | ||
| .join('\n')} | ||
| register('${realUrl}', namespace, set, '${specifiers.get(realUrl)}') | ||
| `, | ||
| }; | ||
| } | ||
|
|
||
| return parentGetSource(url, context, parentGetSource); | ||
| } | ||
|
|
||
| // For Node.js 16.12.0 and higher. | ||
| async function load(url, context, parentLoad) { | ||
| if (hasIitm(url)) { | ||
| const { source } = await getSource(url, context); | ||
| return { | ||
| source, | ||
| shortCircuit: true, | ||
| format: 'module', | ||
| }; | ||
| } | ||
|
|
||
| return parentLoad(url, context, parentLoad); | ||
| } | ||
|
|
||
| if (NODE_MAJOR >= 20) { | ||
| process.emitWarning( | ||
| 'import-in-the-middle is currently unsupported on Node.js v20 and has been disabled.' | ||
| ); | ||
| return {}; // TODO: Add support for Node >=20 | ||
| } else if (NODE_MAJOR >= 17 || (NODE_MAJOR === 16 && NODE_MINOR >= 12)) { | ||
| return { load, resolve }; | ||
| } else { | ||
| return { | ||
| load, | ||
| resolve, | ||
| getSource, | ||
| getFormat(url, context, parentGetFormat) { | ||
| if (hasIitm(url)) { | ||
| return { | ||
| format: 'module', | ||
| }; | ||
| } | ||
| if (url === entrypoint) { | ||
| return { | ||
| format: 'commonjs', | ||
| }; | ||
| } | ||
|
|
||
| return parentGetFormat(url, context, parentGetFormat); | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| module.exports = { createHook }; | ||
25 changes: 25 additions & 0 deletions
25
experimental/packages/opentelemetry-instrumentation/hook.mjs
This file contains hidden or 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,25 @@ | ||
| /*! | ||
| * Copyright The OpenTelemetry Authors | ||
pkanal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
|
|
||
| // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License. | ||
| // | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc. | ||
|
|
||
| import { createHook } from './hook.js'; | ||
|
|
||
| const { load, resolve, getFormat, getSource } = await createHook(import.meta); | ||
|
|
||
| export { load, resolve, getFormat, getSource }; | ||
pichlermarc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.