diff --git a/Bootstrap/Helpers.ts b/Bootstrap/Helpers.ts index 7db10093..b7ba8b8f 100644 --- a/Bootstrap/Helpers.ts +++ b/Bootstrap/Helpers.ts @@ -1,7 +1,8 @@ +import { isLinux } from "../Library/PrefixHelper"; import { DiagnosticLog, DiagnosticMessageId } from "./DataModel"; import { DiagnosticLogger } from "./DiagnosticLogger"; -const USER_APP_PATH = "/home/site/wwwroot"; +const LINUX_USER_APPLICATION_INSIGHTS_PATH = "/node_modules/applicationinsights/out/applicationinsights.js"; export function sdkAlreadyExists(_logger: DiagnosticLogger): boolean { try { @@ -9,38 +10,35 @@ export function sdkAlreadyExists(_logger: DiagnosticLogger): boolean { let appInstance: string; try { // Node 8.9+ - // 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; - } + appInstance = (require.resolve as any)("applicationinsights", { paths: [process.cwd()] }); } 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) { - diagnosticLogSdkExists(_logger, appInstance); - 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. + * Linux App Service doesn't append the full cwd to the require.resolve, so we need to check for the relative path we expect + * if application insights is being imported in the user app code. + */ + if ( + appInstance.indexOf("home") > -1 || + (appInstance === LINUX_USER_APPLICATION_INSIGHTS_PATH && isLinux()) + ) { + 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; } } 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); -} +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index fafe6ac8..4e91c0e4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -337,25 +337,25 @@ } }, "node_modules/@opentelemetry/api": { - "version": "1.7.0", - "resolved": "https://registry.npmjs.org/@opentelemetry/api/-/api-1.7.0.tgz", - "integrity": "sha512-AdY5wvN0P2vXBi3b29hxZgSFvdhdxPB9+f0B6s//P9Q8nibRWeA3cHm8UmLpio9ABigkVHJ5NMPk+Mz8VCCyrw==", + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/@opentelemetry/api/-/api-1.8.0.tgz", + "integrity": "sha512-I/s6F7yKUDdtMsoBWXJe8Qz40Tui5vsuKCWJEWVL+5q9sSWRzzx6v2KeNsOBEwd94j0eWkpWCH4yB6rZg9Mf0w==", "engines": { "node": ">=8.0.0" } }, "node_modules/@opentelemetry/core": { - "version": "1.21.0", - "resolved": "https://registry.npmjs.org/@opentelemetry/core/-/core-1.21.0.tgz", - "integrity": "sha512-KP+OIweb3wYoP7qTYL/j5IpOlu52uxBv5M4+QhSmmUfLyTgu1OIS71msK3chFo1D6Y61BIH3wMiMYRCxJCQctA==", + "version": "1.22.0", + "resolved": "https://registry.npmjs.org/@opentelemetry/core/-/core-1.22.0.tgz", + "integrity": "sha512-0VoAlT6x+Xzik1v9goJ3pZ2ppi6+xd3aUfg4brfrLkDBHRIVjMP0eBHrKrhB+NKcDyMAg8fAbGL3Npg/F6AwWA==", "dependencies": { - "@opentelemetry/semantic-conventions": "1.21.0" + "@opentelemetry/semantic-conventions": "1.22.0" }, "engines": { "node": ">=14" }, "peerDependencies": { - "@opentelemetry/api": ">=1.0.0 <1.8.0" + "@opentelemetry/api": ">=1.0.0 <1.9.0" } }, "node_modules/@opentelemetry/instrumentation": { @@ -377,40 +377,40 @@ } }, "node_modules/@opentelemetry/resources": { - "version": "1.21.0", - "resolved": "https://registry.npmjs.org/@opentelemetry/resources/-/resources-1.21.0.tgz", - "integrity": "sha512-1Z86FUxPKL6zWVy2LdhueEGl9AHDJcx+bvHStxomruz6Whd02mE3lNUMjVJ+FGRoktx/xYQcxccYb03DiUP6Yw==", + "version": "1.22.0", + "resolved": "https://registry.npmjs.org/@opentelemetry/resources/-/resources-1.22.0.tgz", + "integrity": "sha512-+vNeIFPH2hfcNL0AJk/ykJXoUCtR1YaDUZM+p3wZNU4Hq98gzq+7b43xbkXjadD9VhWIUQqEwXyY64q6msPj6A==", "dependencies": { - "@opentelemetry/core": "1.21.0", - "@opentelemetry/semantic-conventions": "1.21.0" + "@opentelemetry/core": "1.22.0", + "@opentelemetry/semantic-conventions": "1.22.0" }, "engines": { "node": ">=14" }, "peerDependencies": { - "@opentelemetry/api": ">=1.0.0 <1.8.0" + "@opentelemetry/api": ">=1.0.0 <1.9.0" } }, "node_modules/@opentelemetry/sdk-trace-base": { - "version": "1.21.0", - "resolved": "https://registry.npmjs.org/@opentelemetry/sdk-trace-base/-/sdk-trace-base-1.21.0.tgz", - "integrity": "sha512-yrElGX5Fv0umzp8Nxpta/XqU71+jCAyaLk34GmBzNcrW43nqbrqvdPs4gj4MVy/HcTjr6hifCDCYA3rMkajxxA==", + "version": "1.22.0", + "resolved": "https://registry.npmjs.org/@opentelemetry/sdk-trace-base/-/sdk-trace-base-1.22.0.tgz", + "integrity": "sha512-pfTuSIpCKONC6vkTpv6VmACxD+P1woZf4q0K46nSUvXFvOFqjBYKFaAMkKD3M1mlKUUh0Oajwj35qNjMl80m1Q==", "dependencies": { - "@opentelemetry/core": "1.21.0", - "@opentelemetry/resources": "1.21.0", - "@opentelemetry/semantic-conventions": "1.21.0" + "@opentelemetry/core": "1.22.0", + "@opentelemetry/resources": "1.22.0", + "@opentelemetry/semantic-conventions": "1.22.0" }, "engines": { "node": ">=14" }, "peerDependencies": { - "@opentelemetry/api": ">=1.0.0 <1.8.0" + "@opentelemetry/api": ">=1.0.0 <1.9.0" } }, "node_modules/@opentelemetry/semantic-conventions": { - "version": "1.21.0", - "resolved": "https://registry.npmjs.org/@opentelemetry/semantic-conventions/-/semantic-conventions-1.21.0.tgz", - "integrity": "sha512-lkC8kZYntxVKr7b8xmjCVUgE0a8xgDakPyDo9uSWavXPyYqLgYYGdEd2j8NxihRyb6UwpX3G/hFUF4/9q2V+/g==", + "version": "1.22.0", + "resolved": "https://registry.npmjs.org/@opentelemetry/semantic-conventions/-/semantic-conventions-1.22.0.tgz", + "integrity": "sha512-CAOgFOKLybd02uj/GhCdEeeBjOS0yeoDeo/CA7ASBSmenpZHAKGB3iDm/rv3BQLcabb/OprDEsSQ1y0P8A7Siw==", "engines": { "node": ">=14" }