|
1 | 1 | import { DiagnosticLog, DiagnosticMessageId } from "./DataModel"; |
2 | 2 | import { DiagnosticLogger } from "./DiagnosticLogger"; |
3 | 3 |
|
| 4 | +const USER_APP_PATH = "/home/site/wwwroot"; |
| 5 | + |
4 | 6 | export function sdkAlreadyExists(_logger: DiagnosticLogger): boolean { |
5 | 7 | try { |
6 | 8 | // appInstance should either resolve to user SDK or crash. If it resolves to attach SDK, user probably modified their NODE_PATH |
7 | 9 | let appInstance: string; |
8 | 10 | try { |
9 | 11 | // 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 | + } |
11 | 19 | } catch (e) { |
12 | 20 | // Node <8.9 |
13 | 21 | 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 | + } |
29 | 31 | } |
30 | 32 | } catch (e) { |
31 | 33 | // crashed while trying to resolve "applicationinsights", so SDK does not exist. Attach appinsights |
32 | 34 | return false; |
33 | 35 | } |
34 | 36 | } |
| 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