-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix SDK already exists detection. (#1284)
- Loading branch information
1 parent
d0330d7
commit 055355c
Showing
1 changed file
with
28 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,46 @@ | ||
import { DiagnosticLog, DiagnosticMessageId } from "./DataModel"; | ||
import { DiagnosticLogger } from "./DiagnosticLogger"; | ||
|
||
const USER_APP_PATH = "/home/site/wwwroot"; | ||
|
||
export function sdkAlreadyExists(_logger: DiagnosticLogger): boolean { | ||
try { | ||
// appInstance should either resolve to user SDK or crash. If it resolves to attach SDK, user probably modified their NODE_PATH | ||
let appInstance: string; | ||
try { | ||
// Node 8.9+ | ||
appInstance = (require.resolve as any)("applicationinsights", { paths: [process.cwd()] }); | ||
// If we find the applicationinsights module under in the user application path, do not attach the SDK | ||
// In order for this to work in Windows, we need to pass the full "/home/site/wwwroot" path to require.resolve | ||
appInstance = (require.resolve as any)("applicationinsights", { paths: [USER_APP_PATH] }); | ||
if (appInstance) { | ||
diagnosticLogSdkExists(_logger, appInstance); | ||
return true; | ||
} | ||
} catch (e) { | ||
// Node <8.9 | ||
appInstance = require.resolve(process.cwd() + "/node_modules/applicationinsights"); | ||
} | ||
// If loaded instance is in Azure machine home path do not attach the SDK, this means customer already instrumented their app | ||
if (appInstance.indexOf("home") > -1) { | ||
const diagnosticLog: DiagnosticLog = { | ||
message: "Application Insights SDK already exists. Module is already installed in this application; not re-attaching. Installed SDK location: " + appInstance, | ||
properties: { | ||
"msgId": DiagnosticMessageId.sdkExists | ||
} | ||
}; | ||
_logger.logError(diagnosticLog); | ||
return true; | ||
} | ||
else { | ||
// ApplicationInsights could be loaded outside of customer application, attach in this case | ||
return false; | ||
// If loaded instance is in Azure machine home path do not attach the SDK, this means customer already instrumented their app | ||
if (appInstance.indexOf("home") > -1) { | ||
diagnosticLogSdkExists(_logger, appInstance); | ||
return true; | ||
} | ||
else { | ||
// ApplicationInsights could be loaded outside of customer application, attach in this case | ||
return false; | ||
} | ||
} | ||
} catch (e) { | ||
// crashed while trying to resolve "applicationinsights", so SDK does not exist. Attach appinsights | ||
return false; | ||
} | ||
} | ||
|
||
function diagnosticLogSdkExists(logger: DiagnosticLogger, appInstance: string): void { | ||
const diagnosticLog: DiagnosticLog = { | ||
message: "Application Insights SDK already exists. Module is already installed in this application; not re-attaching. Installed SDK location: " + appInstance, | ||
properties: { | ||
"msgId": DiagnosticMessageId.sdkExists | ||
} | ||
}; | ||
logger.logError(diagnosticLog); | ||
} |