Skip to content

Commit

Permalink
fix: Don't use require to load package.json files (open-telemetry…
Browse files Browse the repository at this point in the history
…#4593)

* fix: Don't use require to load package.json files

* update changelog

* Move changelog entry

---------

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
2 people authored and Zirak committed Sep 14, 2024
1 parent 5466b85 commit 4506e8f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ All notable changes to experimental packages in this project will be documented
### :bug: (Bug Fix)

* fix(otlp-grpc-exporter-base): avoid TypeError on exporter shutdown [#4612](https://github.com/open-telemetry/opentelemetry-js/pull/4612)
* fix(instrumentation): Don't use `require` to load `package.json` files

### :books: (Refine Doc)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { InstrumentationModuleDefinition } from '../../types';
import { diag } from '@opentelemetry/api';
import type { OnRequireFn } from 'require-in-the-middle';
import { Hook } from 'require-in-the-middle';
import { readFileSync } from 'fs';

/**
* Base abstract class for instrumenting node plugins
Expand Down Expand Up @@ -160,8 +161,10 @@ export abstract class InstrumentationBase<T = any>

private _extractPackageVersion(baseDir: string): string | undefined {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const version = require(path.join(baseDir, 'package.json')).version;
const json = readFileSync(path.join(baseDir, 'package.json'), {
encoding: 'utf8',
});
const version = JSON.parse(json).version;
return typeof version === 'string' ? version : undefined;
} catch (error) {
diag.warn('Failed extracting version', baseDir);
Expand Down

0 comments on commit 4506e8f

Please sign in to comment.