Skip to content

Commit

Permalink
fix(resource): use dynamic import over require to improve ESM compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiangmoe committed Jan 27, 2025
1 parent bdc0e3a commit 56da588
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se
### :bug: (Bug Fix)

* fix(sdk-metrics): do not export from `PeriodicExportingMetricReader` when there are no metrics to export. [#5288](https://github.com/open-telemetry/opentelemetry-js/pull/5288) @jacksonweber
* fix(resource): use dynamic import over require to improve ESM compliance [#5298](https://github.com/open-telemetry/opentelemetry-js/pull/5298) @xiaoxiangmoe

### :books: (Refine Doc)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,28 @@
*/
import * as process from 'process';

let getMachineId: () => Promise<string>;
let getMachineIdImpl: undefined | (() => Promise<string>);

switch (process.platform) {
case 'darwin':
({ getMachineId } = require('./getMachineId-darwin'));
break;
case 'linux':
({ getMachineId } = require('./getMachineId-linux'));
break;
case 'freebsd':
({ getMachineId } = require('./getMachineId-bsd'));
break;
case 'win32':
({ getMachineId } = require('./getMachineId-win'));
break;
default:
({ getMachineId } = require('./getMachineId-unsupported'));
}
export async function getMachineId(): Promise<string> {
if (!getMachineIdImpl) {
switch (process.platform) {
case 'darwin':
getMachineIdImpl = (await import('./getMachineId-darwin.js')).getMachineId;

Check failure on line 24 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-windows-tests

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 24 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / browser-tests

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 24 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / webworker-tests

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 24 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-tests (18)

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 24 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-tests (20)

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 24 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-tests (22)

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.
break;
case 'linux':
getMachineIdImpl = (await import('./getMachineId-linux.js')).getMachineId;

Check failure on line 27 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-windows-tests

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 27 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / browser-tests

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 27 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / webworker-tests

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 27 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-tests (18)

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 27 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-tests (20)

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 27 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-tests (22)

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.
break;
case 'freebsd':
getMachineIdImpl = (await import('./getMachineId-bsd.js')).getMachineId;

Check failure on line 30 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-windows-tests

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 30 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / browser-tests

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 30 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / webworker-tests

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 30 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-tests (18)

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 30 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-tests (20)

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 30 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-tests (22)

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.
break;
case 'win32':
getMachineIdImpl = (await import('./getMachineId-win.js')).getMachineId;

Check failure on line 33 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-windows-tests

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 33 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / browser-tests

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 33 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / webworker-tests

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 33 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-tests (18)

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 33 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-tests (20)

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 33 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-tests (22)

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.
break;
default:
getMachineIdImpl = (await import('./getMachineId-unsupported.js')).getMachineId;

Check failure on line 36 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-windows-tests

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 36 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / browser-tests

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 36 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / webworker-tests

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 36 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-tests (18)

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 36 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-tests (20)

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.

Check failure on line 36 in packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts

View workflow job for this annotation

GitHub Actions / node-tests (22)

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.
break;
}
}

export { getMachineId };
return getMachineIdImpl();
}

0 comments on commit 56da588

Please sign in to comment.