Skip to content

Commit

Permalink
chore: improved the comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aryamohanan committed Apr 4, 2024
1 parent 4b95c5c commit d583209
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 77 deletions.
13 changes: 10 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,19 @@ REGIONS=<region> SKIP_DOCKER_IMAGE=true BUILD_LAYER_WITH=local LAYER_NAME=experi
```
## ESM Support

We have added the ESM support for all Node.js versions, Since version 18.19, [ESM loaders are off-thread](https://github.com/nodejs/node/pull/44710), loaded separately, a shift from previous setups where the Instana collector was loaded within the loader, leading to a disruption in existing implementation. To resolve this, we've replaced the deprecated `--experimental-loader` with `--import`, facilitating the loading of the collector in the main thread. However, note that `--import` is only compatible with Node.js v18.19 and later, necessitating the maintenance of both styles for different Node.js versions.

Efforts are ongoing to integrate native ESM support, detailed in ref 117183.

Use the following command to enable experimental ESM support:

- For Node.js versions greater than or equal to 18.19:

node --import /path/to/instana/node_modules/@instana/collector/register.mjs entry-point

```sh
node --import /path/to/instana/node_modules/@instana/collector/register.mjs entry-point
```
- For Node.js versions less than 18.19:

node --experimental-loader /path/to/instana/node_modules/@instana/collector/esm-loader.mjs entry-point
```sh
node --experimental-loader /path/to/instana/node_modules/@instana/collector/esm-loader.mjs entry-point
```
19 changes: 6 additions & 13 deletions packages/aws-fargate/esm-loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
'use strict';

/**
* We currently only instrument CJS modules. As soon as we want
* to instrument ES modules (such as `got` v12), the requireHook will
* no longer work. Therefor we would need to wrap the target ES module
* with our instrumentations using the resolve & load hook.
* IMPORTANT NOTE: From Node.js version 18.19 and above, the ESM loaders operate off-thread.
* Consequently, ESM instrumentation using '--experimental-loader' becomes obsolete.
* Instead, we recommend using '--import' for loading instrumentation and relocating the
* Instana collector loading to './register' file.
* Please note that '--import' flag is unavailable in earlier versions, hence we maintain both setups.
* We will incorporate the native ESM support by using 'import-in-the-middle'.
*
* Usage:
* ENV NODE_OPTIONS='--experimental-loader=/instana/node_modules/
Expand All @@ -22,12 +24,3 @@
*/

import './src/index.js';
/*
export async function resolve(specifier, context, nextResolve) {
return nextResolve(specifier, context, nextResolve);
}
export async function load(url, context, nextLoad) {
return nextLoad(url, context, nextLoad);
}
*/
17 changes: 11 additions & 6 deletions packages/aws-fargate/register.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
*/

/**
* ESM hooks supplied via loaders (--experimental-loader=@instana/collector/esm-loader.mjs) run in a dedicated thread from v20 and above,
* isolated from the main thread. Reference: https://github.com/nodejs/node/pull/44710
* We loaded the tracer in the main thread with --import, we can extend the ESM hook with register method
* in the future to extend the ESM support.
* As of Node.js version 18.19 and above, ESM loaders (--experimental-loader)
* are executed in a dedicated thread, separate from the main thread.
* see https://github.com/nodejs/node/pull/44710.
* Previously, loading the Instana collector within the loader and after the update ESM support
* no longer working with v18.19 and above. To address this, we've opted to load the Instana
* collector in the main thread using --import.
* Additionally, we aim to incorporate native ESM
* support by utilizing the node register method, enabling customization of the ESM loader
* with 'import-in-the-middle'.
*
* Usage:
* ENV NODE_OPTIONS='--import /instana/node_modules/@instana/aws-fargate/register.mjs
*/

// We plan to utilize this for adding native ESM support in the near future
// import { register } from 'node:module';
// register(./loader.mjs, import.meta.url);

// Importing the Instana trace initialization module here, as this is the main thread.
// Importing the Instana trace initialization module here, as this is executed in the main thread.
import './src/index.js';
18 changes: 6 additions & 12 deletions packages/azure-container-services/esm-loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
'use strict';

/**
* We currently only instrument CJS modules. As soon as we want
* to instrument ES modules (such as `got` v12), the requireHook will
* no longer work. Therefor we would need to wrap the target ES module
* with our instrumentations using the resolve & load hook.
* IMPORTANT NOTE: From Node.js version 18.19 and above, the ESM loaders operate off-thread.
* Consequently, ESM instrumentation using '--experimental-loader' becomes obsolete.
* Instead, we recommend using '--import' for loading instrumentation and relocating the
* Instana collector loading to './register' file.
* Please note that '--import' flag is unavailable in earlier versions, hence we maintain both setups.
* We will incorporate the native ESM support by using 'import-in-the-middle'.
*
* Usage:
* ENV NODE_OPTIONS='--experimental-loader=/instana/node_modules/
Expand All @@ -22,12 +24,4 @@
*/

import './src/index.js';
/*
export async function resolve(specifier, context, nextResolve) {
return nextResolve(specifier, context, nextResolve);
}

export async function load(url, context, nextLoad) {
return nextLoad(url, context, nextLoad);
}
*/
15 changes: 10 additions & 5 deletions packages/azure-container-services/register.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
*/

/**
* ESM hooks supplied via loaders (--experimental-loader=@instana/collector/esm-loader.mjs) run in a dedicated thread from v20 and above,
* isolated from the main thread. Reference: https://github.com/nodejs/node/pull/44710
* We loaded the tracer in the main thread with --import, we can extend the ESM hook with register method
* in the future to extend the ESM support.
* As of Node.js version 18.19 and above, ESM loaders (--experimental-loader)
* are executed in a dedicated thread, separate from the main thread.
* see https://github.com/nodejs/node/pull/44710.
* Previously, loading the Instana collector within the loader and after the update ESM support
* no longer working with v18.19 and above. To address this, we've opted to load the Instana
* collector in the main thread using --import. Additionally, we aim to incorporate native ESM
* support by utilizing the node register method, enabling customization of the ESM loader
* with 'import-in-the-middle'.
*
* Usage:
* ENV NODE_OPTIONS='--import /instana/node_modules/@instana/azure-container-services/register.mjs server.js
*/

// We plan to utilize this for adding native ESM support in the near future
// import { register } from 'node:module';
// register(./loader.mjs, import.meta.url);

// Importing the Instana trace initialization module here, as this is the main thread.
// Importing the Instana trace initialization module here, as this is executed in the main thread.
import './src/index.js';
20 changes: 6 additions & 14 deletions packages/collector/esm-loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
'use strict';

/**
* We currently only instrument CJS modules. As soon as we want
* to instrument ES modules (such as `got` v12), the requireHook will
* no longer work. Therefor we would need to wrap the target ES module
* with our instrumentations using the resolve & load hook.
* IMPORTANT NOTE: From Node.js version 18.19 and above, the ESM loaders operate off-thread.
* Consequently, ESM instrumentation using '--experimental-loader' becomes obsolete.
* Instead, we recommend using '--import' for loading instrumentation and relocating the
* Instana collector loading to './register' file.
* Please note that '--import' flag is unavailable in earlier versions, hence we maintain both setups.
* We will incorporate the native ESM support by using 'import-in-the-middle'.
*
* Usage:
* node --experimental-loader=@instana/collector/esm-loader.mjs server.js
Expand All @@ -19,13 +21,3 @@

import instana from './src/index.js';
instana();

/*
export async function resolve(specifier, context, nextResolve) {
return nextResolve(specifier, context, nextResolve);
}
export async function load(url, context, nextLoad) {
return nextLoad(url, context, nextLoad);
}
*/
16 changes: 10 additions & 6 deletions packages/collector/register.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
*/

/**
* ESM hooks supplied via loaders (--experimental-loader=@instana/collector/esm-loader.mjs) run in a dedicated thread from v20 and above,
* isolated from the main thread. Reference: https://github.com/nodejs/node/pull/44710
* We loaded the tracer in the main thread with --import, we can extend the ESM hook with register method
* in the future to extend the ESM support.
* As of Node.js version 18.19 and above, ESM loaders (--experimental-loader)
* are executed in a dedicated thread, separate from the main thread.
* see https://github.com/nodejs/node/pull/44710.
* Previously, loading the Instana collector within the loader and after the update ESM support
* no longer working with v18.19 and above. To address this, we've opted to load the Instana
* collector in the main thread using --import. Additionally, we aim to incorporate native ESM
* support by utilizing the node register method, enabling customization of the ESM loader
* with 'import-in-the-middle'.
*
* Usage:
* node --import @instana/collector/register.mjs server.js
*/

// We plan to utilize this for adding native ESM support in the near future
// import { register } from 'node:module';
// register(./loader.mjs, import.meta.url);

// Importing the Instana trace initialization module here, as this is the main thread.
// Importing the Instana trace initialization module here, as this is executed in the main thread.
import instana from './src/index.js';
instana();
20 changes: 7 additions & 13 deletions packages/google-cloud-run/esm-loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
'use strict';

/**
* We currently only instrument CJS modules. As soon as we want
* to instrument ES modules (such as `got` v12), the requireHook will
* no longer work. Therefor we would need to wrap the target ES module
* with our instrumentations using the resolve & load hook.
* IMPORTANT NOTE: From Node.js version 18.19 and above, the ESM loaders operate off-thread.
* Consequently, ESM instrumentation using '--experimental-loader' becomes obsolete.
* Instead, we recommend using '--import' for loading instrumentation and relocating the
* Instana collector loading to './register' file.
* Please note that '--import' flag is unavailable in earlier versions, hence we maintain both setups.
* We will incorporate the native ESM support by using 'import-in-the-middle'.
*
* Usage:
* ENV NODE_OPTIONS='--experimental-loader=/instana/node_modules/
Expand All @@ -21,12 +23,4 @@
* can be invoked from this context.
*/

import './src/index.js';
/*
export async function resolve(specifier, context, nextResolve) {
return nextResolve(specifier, context, nextResolve);
}
export async function load(url, context, nextLoad) {
return nextLoad(url, context, nextLoad);
}
*/
import './src/index.js';
15 changes: 10 additions & 5 deletions packages/google-cloud-run/register.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
*/

/**
* ESM hooks supplied via loaders (--experimental-loader=@instana/collector/esm-loader.mjs) run in a dedicated thread from v20 and above,
* isolated from the main thread. Reference: https://github.com/nodejs/node/pull/44710
* We loaded the tracer in the main thread with --import, we can extend the ESM hook with register method
* in the future to extend the ESM support.
* As of Node.js version 18.19 and above, ESM loaders (--experimental-loader)
* are executed in a dedicated thread, separate from the main thread.
* see https://github.com/nodejs/node/pull/44710.
* Previously, loading the Instana collector within the loader and after the update ESM support
* no longer working with v18.19 and above. To address this, we've opted to load the Instana
* collector in the main thread using --import. Additionally, we aim to incorporate native ESM
* support by utilizing the node register method, enabling customization of the ESM loader
* with 'import-in-the-middle'.
*
* Usage:
* ENV NODE_OPTIONS='--import /instana/node_modules/@instana/google-cloud-run/register.mjs
*/

// We plan to utilize this for adding native ESM support in the near future
// import { register } from 'node:module';
// register(./loader.mjs, import.meta.url);

// Importing the Instana trace initialization module here, as this is the main thread.
// Importing the Instana trace initialization module here, as this is executed in the main thread.
import './src/index.js';

0 comments on commit d583209

Please sign in to comment.