Skip to content
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

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

Merged
merged 6 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ All notable changes to experimental packages in this project will be documented
* fix(exporter-*-otlp-*): use parseHeaders() to ensure header-values are not 'undefined' #4540
* Fixes a bug where passing `undefined` as a header value would crash the end-user app after the export timeout elapsed.
* fix(sdk-logs): ensure default resource attributes are used as fallbacks when a resource is passed to LoggerProvider.
* fix(instrumentation): Don't use `require` to load `package.json` files
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved

### :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