-
Notifications
You must be signed in to change notification settings - Fork 825
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
ESM --experimental-loader=@opentelemetry/instrumentation/hook.mjs
breaks typescript path aliases
#5097
Comments
hey @douglasg14b would you be able to provide the tsconfig.json you're using as well? Thanks |
@jpmunz tsconfig added to OP |
@douglasg14b I believe this behaviour is coming from the I'm not actually sure if this package is intended to understand those path aliases, I'm asking about that here nodejs/import-in-the-middle#162, if not there would need to be some additional step in your build process to resolve those paths prior to running the loader |
Hm, I'm not entirely sure how paths would be resolved before using Precompiling would defeat the purpose of using |
so to get that working with tsx I think the hook would have to be wrapped with something that could resolve the path aliases at run time. You might be able to give this workaround (based on https://stackoverflow.com/a/75318315) a try, it uses // wrapper_hook.mjs
import {
load,
resolve as origResolve,
getFormat,
getSource,
} from '@opentelemetry/instrumentation/hook.mjs';
// See https://stackoverflow.com/a/75318315
import {loadConfig, createMatchPath} from 'tsconfig-paths';
import {pathToFileURL} from 'url'
const {absoluteBaseUrl, paths} = loadConfig()
const matchPath = createMatchPath(absoluteBaseUrl, paths)
const wrappedResolve = (specifier, ctx, defaultResolve) => {
const match = matchPath(specifier, undefined, undefined, [".ts", ".tsx"]);
const updatedSpecifier = match
? pathToFileURL(match).href : specifier;
return origResolve(updatedSpecifier, ctx, defaultResolve);
}
export { load, wrappedResolve as resolve, getFormat, getSource }; and then use that wrapper as the loader instead of '@opentelemetry/instrumentation/hook.mjs': npx tsx --experimental-loader=./wrapper_hook.mjs --import ./telemetry.mjs <your-script>.ts I put that together in an example here where I use path aliases and was able to see spans for http requests from the auto instrumentation working: https://github.com/jpmunz/simple-node-app/tree/otel-loading-example |
Thank you for this! I do want to note that I get This appears to avoid a crashing error, however, I don't get anything more than |
Is this expected behavior? That |
@jpmunz Using the method described in the linked issue resolve the problem for me.
/*instrumentation.ts*/
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus';
import { Resource } from '@opentelemetry/resources';
import { NodeSDK } from '@opentelemetry/sdk-node';
import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';
import { createAddHookMessageChannel, Hook } from 'import-in-the-middle';
import { register } from 'module';
// Yes, createAddHookMessageChannel is new. See below.
const { registerOptions, waitForAllMessagesAcknowledged } = createAddHookMessageChannel();
register('import-in-the-middle/hook.mjs', import.meta.url, registerOptions);
const sdk = new NodeSDK({
resource: new Resource({
[ATTR_SERVICE_NAME]: 'ivr3-api',
[ATTR_SERVICE_VERSION]: '1.0',
}),
metricReader: new PrometheusExporter({
host: '0.0.0.0',
port: 9464,
}),
instrumentations: [
getNodeAutoInstrumentations({
'@opentelemetry/instrumentation-http': {},
'@opentelemetry/instrumentation-express': {},
}),
],
});
sdk.start();
await waitForAllMessagesAcknowledged(); |
nice! If that worked it seems like the way to make this easier going forward, as that other issues suggests, is with |
@douglasg14b for this I'd recommended filing another issue, if you aren't getting telemetry from express either way that seems separate from the original problem outlined here around path aliases |
This seems to be resolved. I'm closing this, but anyone who finds this should refer to #4933. Please create a new issue for the http/express/config issue |
What happened?
Using the recommended way of instrumenting an ESM application causes path aliases to break, rendering it unusable.
Steps to Reproduce
tsx
--experimental-loader=@opentelemetry/instrumentation/hook.mjs
to your startup commandExpected Result
It starts
Actual Result
It fails to start, erroring out on any imported files that use path aliases
Additional Details
OpenTelemetry Setup Code
tsconfig.json
package.json
Relevant log output
No response
The text was updated successfully, but these errors were encountered: