Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 13328b9

Browse files
author
Mark Wolff
authored
refactor: parse node and ts-node path (#47)
1 parent 6fb7df3 commit 13328b9

File tree

1 file changed

+73
-16
lines changed
  • packages/azure-opentelemetry-exporter/src/platform/nodejs/context

1 file changed

+73
-16
lines changed

packages/azure-opentelemetry-exporter/src/platform/nodejs/context/context.ts

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,29 @@ export class Context {
2323

2424
public static nodeVersion: string = '';
2525

26+
/**
27+
* Add extra ../ to access on environments not using ts-node
28+
*/
29+
private static readonly JS_NODE_PREFIX = '../';
30+
31+
/**
32+
* Path to azure-opentelemetry-exporter
33+
*/
34+
private static readonly ROOT_PATH = '../../../../';
35+
2636
constructor(
2737
private _logger: Logger = new ConsoleLogger(LogLevel.WARN),
28-
private _exporterPrefix = '../',
29-
private _appPrefix = '../../../',
38+
/**
39+
* Path to this module's `package.json` relative to
40+
* `Context.ROOT_PATH`
41+
*/
42+
private _exporterPrefix = './',
43+
/**
44+
* Path to end user application folder which contains `package.json`
45+
* relative to `Context.ROOT_PATH`
46+
* @default `../../`
47+
*/
48+
private _appPrefix = '../../',
3049
) {
3150
this.keys = new Contracts.ContextTagKeys();
3251
this.tags = <{ [key: string]: string }>{};
@@ -39,19 +58,38 @@ export class Context {
3958
private _loadApplicationContext() {
4059
if (Object.keys(Context.appVersion).length === 0) {
4160
// note: this should return the host package.json
42-
// note: this does not require this._prefix
43-
const packageJsonPath = path.resolve(__dirname, `${this._appPrefix}../../../../package.json`);
61+
let packageJson: { version: string } | null = null;
62+
const packageJsonPath = path.resolve(
63+
__dirname,
64+
Context.JS_NODE_PREFIX,
65+
this._appPrefix,
66+
Context.ROOT_PATH,
67+
'./package.json',
68+
);
69+
const packageJsonPathTsNode = path.resolve(
70+
__dirname,
71+
this._appPrefix,
72+
Context.ROOT_PATH,
73+
'./package.json',
74+
);
75+
4476
Context.appVersion[packageJsonPath] = 'unknown';
77+
4578
try {
46-
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
47-
if (packageJson && typeof packageJson.version === 'string') {
48-
Context.appVersion[packageJsonPath] = packageJson.version;
79+
packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
80+
} catch (_) {
81+
try {
82+
packageJson = JSON.parse(fs.readFileSync(packageJsonPathTsNode, 'utf8'));
83+
} catch (exception) {
84+
this._logger.warn('Failed to load Application version', exception);
4985
}
86+
}
5087

51-
this.tags[this.keys.applicationVersion] = Context.appVersion[packageJsonPath];
52-
} catch (exception) {
53-
this._logger.warn('Failed to load Application version');
88+
if (packageJson && typeof packageJson.version === 'string') {
89+
Context.appVersion[packageJsonPath] = packageJson.version;
5490
}
91+
92+
this.tags[this.keys.applicationVersion] = Context.appVersion[packageJsonPath];
5593
}
5694
}
5795

@@ -68,20 +106,39 @@ export class Context {
68106

69107
private _loadInternalContext() {
70108
if (!Context.sdkVersion) {
109+
let packageJson: { version: string } | null = null;
71110
const { node } = process.versions;
72111
[Context.nodeVersion] = node.split('.');
73112

74113
// note: this should return the sdk package.json
75-
const packageJsonPath = path.resolve(__dirname, `${this._exporterPrefix}../../../../package.json`);
114+
const packageJsonPath = path.resolve(
115+
__dirname,
116+
Context.JS_NODE_PREFIX,
117+
this._exporterPrefix,
118+
Context.ROOT_PATH,
119+
'./package.json',
120+
);
121+
const packageJsonPathTsNode = path.resolve(
122+
__dirname,
123+
this._exporterPrefix,
124+
Context.ROOT_PATH,
125+
'./package.json',
126+
);
76127

77128
Context.sdkVersion = 'unknown';
78129
try {
79-
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
80-
if (packageJson && typeof packageJson.version === 'string') {
81-
Context.sdkVersion = packageJson.version;
130+
packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
131+
} catch (_) {
132+
try {
133+
packageJson = JSON.parse(fs.readFileSync(packageJsonPathTsNode, 'utf8'));
134+
} catch (exception) {
135+
this._logger.warn('Failed to load Exporter version', exception);
136+
throw exception;
82137
}
83-
} catch (exception) {
84-
this._logger.warn('Failed to load Exporter version');
138+
}
139+
140+
if (packageJson && typeof packageJson.version === 'string') {
141+
Context.sdkVersion = packageJson.version;
85142
}
86143
}
87144

0 commit comments

Comments
 (0)