Skip to content

Commit 055355c

Browse files
authored
Fix SDK already exists detection. (#1284)
1 parent d0330d7 commit 055355c

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

Bootstrap/Helpers.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,46 @@
11
import { DiagnosticLog, DiagnosticMessageId } from "./DataModel";
22
import { DiagnosticLogger } from "./DiagnosticLogger";
33

4+
const USER_APP_PATH = "/home/site/wwwroot";
5+
46
export function sdkAlreadyExists(_logger: DiagnosticLogger): boolean {
57
try {
68
// appInstance should either resolve to user SDK or crash. If it resolves to attach SDK, user probably modified their NODE_PATH
79
let appInstance: string;
810
try {
911
// Node 8.9+
10-
appInstance = (require.resolve as any)("applicationinsights", { paths: [process.cwd()] });
12+
// If we find the applicationinsights module under in the user application path, do not attach the SDK
13+
// In order for this to work in Windows, we need to pass the full "/home/site/wwwroot" path to require.resolve
14+
appInstance = (require.resolve as any)("applicationinsights", { paths: [USER_APP_PATH] });
15+
if (appInstance) {
16+
diagnosticLogSdkExists(_logger, appInstance);
17+
return true;
18+
}
1119
} catch (e) {
1220
// Node <8.9
1321
appInstance = require.resolve(process.cwd() + "/node_modules/applicationinsights");
14-
}
15-
// If loaded instance is in Azure machine home path do not attach the SDK, this means customer already instrumented their app
16-
if (appInstance.indexOf("home") > -1) {
17-
const diagnosticLog: DiagnosticLog = {
18-
message: "Application Insights SDK already exists. Module is already installed in this application; not re-attaching. Installed SDK location: " + appInstance,
19-
properties: {
20-
"msgId": DiagnosticMessageId.sdkExists
21-
}
22-
};
23-
_logger.logError(diagnosticLog);
24-
return true;
25-
}
26-
else {
27-
// ApplicationInsights could be loaded outside of customer application, attach in this case
28-
return false;
22+
// If loaded instance is in Azure machine home path do not attach the SDK, this means customer already instrumented their app
23+
if (appInstance.indexOf("home") > -1) {
24+
diagnosticLogSdkExists(_logger, appInstance);
25+
return true;
26+
}
27+
else {
28+
// ApplicationInsights could be loaded outside of customer application, attach in this case
29+
return false;
30+
}
2931
}
3032
} catch (e) {
3133
// crashed while trying to resolve "applicationinsights", so SDK does not exist. Attach appinsights
3234
return false;
3335
}
3436
}
37+
38+
function diagnosticLogSdkExists(logger: DiagnosticLogger, appInstance: string): void {
39+
const diagnosticLog: DiagnosticLog = {
40+
message: "Application Insights SDK already exists. Module is already installed in this application; not re-attaching. Installed SDK location: " + appInstance,
41+
properties: {
42+
"msgId": DiagnosticMessageId.sdkExists
43+
}
44+
};
45+
logger.logError(diagnosticLog);
46+
}

0 commit comments

Comments
 (0)