diff --git a/Bootstrap/Default.ts b/Bootstrap/Default.ts index 0dbf0a512..ee66f5bc8 100644 --- a/Bootstrap/Default.ts +++ b/Bootstrap/Default.ts @@ -8,10 +8,12 @@ import { DiagnosticLogger } from "./DiagnosticLogger"; import Config = require("../Library/Config"); import { DiagnosticLog, DiagnosticMessageId } from "./DataModel"; import * as PrefixHelpers from "../Library/PrefixHelper"; +import Context = require("../Library/Context"); // Private configuration vars let _appInsights: typeof types | null; -let _prefix = "ud_"; // Unknown, Default +let _prefix = `${PrefixHelpers.getResourceProvider()}${PrefixHelpers.getOsPrefix()}${Constants.AttachTypePrefix.INTEGRATED_AUTO}_`; +let _fullSdkVersion = `${_prefix}node:${Context.sdkVersion}`; export const defaultConfig = new Config(); // Will read env variables, expose for Agent initialization const _instrumentationKey = defaultConfig.instrumentationKey; @@ -30,14 +32,6 @@ export function setLogger(logger: DiagnosticLogger) { return _logger = logger; } -/** - * Sets the string which is prefixed to the existing sdkVersion, e.g. `ad_`, `alr_` - * @param prefix string prefix, including underscore. Defaults to `ud_` - */ -export function setUsagePrefix(prefix: string) { - _prefix = prefix; -} - export function setStatusLogger(statusLogger: StatusLogger) { _statusLogger = statusLogger; } @@ -89,13 +83,9 @@ export function setupAndStart(aadTokenCredential?: azureCoreAuth.TokenCredential /** Sets the SDK version prefix in auto-attach scenarios */ const prefixInternalSdkVersion = function (envelope: types.Contracts.Envelope, _contextObjects: Object) { - if (_prefix === "ud_") { - // If SDK version prefix is not set - set it using {RP}{OS}{Attach Type}_ pattern - _prefix = `${PrefixHelpers.getResourceProvider()}${PrefixHelpers.getOsPrefix()}${Constants.AttachTypePrefix.INTEGRATED_AUTO}_` - } + // If SDK version prefix is not set - set it using {RP}{OS}{Attach Type}_ pattern try { - var appInsightsSDKVersion = _appInsights.defaultClient.context.keys.internalSdkVersion; - envelope.tags[appInsightsSDKVersion] = _prefix + envelope.tags[appInsightsSDKVersion]; + envelope.tags[appInsightsSDKVersion] = _fullSdkVersion; } catch (e) { const diagnosticLog: DiagnosticLog = { message: "Error prefixing SDK version.", @@ -122,6 +112,7 @@ export function setupAndStart(aadTokenCredential?: azureCoreAuth.TokenCredential // Instrument the SDK _appInsights.setup(); + const appInsightsSDKVersion = _appInsights.defaultClient.context.keys.internalSdkVersion; // Azure Functions if (isAzureFunction) { @@ -195,6 +186,8 @@ export function setupAndStart(aadTokenCredential?: azureCoreAuth.TokenCredential } _appInsights.start(); + // Set the SDK verison in the context + _appInsights.defaultClient.context.tags[appInsightsSDKVersion] = _fullSdkVersion; // Add attach flag in Statsbeat let statsbeat = _appInsights.defaultClient.getStatsbeat(); if (statsbeat) { diff --git a/Bootstrap/Oryx.ts b/Bootstrap/Oryx.ts index 5bb0ebb50..656dd09ec 100644 --- a/Bootstrap/Oryx.ts +++ b/Bootstrap/Oryx.ts @@ -3,9 +3,6 @@ import { StatusLogger } from "./StatusLogger"; import { DiagnosticLogger } from "./DiagnosticLogger"; import { NoopLogger } from "./NoopLogger"; import appInsightsLoader = require("./Default"); -import { AttachTypePrefix } from "../Declarations/Constants"; - -appInsightsLoader.setUsagePrefix(`al${AttachTypePrefix.INTEGRATED_AUTO}_`); // App Services Linux Auto Attach // Set Status.json logger appInsightsLoader.setStatusLogger(new StatusLogger(new NoopLogger())); diff --git a/Library/Context.ts b/Library/Context.ts index dc94cce2f..8bca97ff8 100644 --- a/Library/Context.ts +++ b/Library/Context.ts @@ -7,6 +7,7 @@ import { APPLICATION_INSIGHTS_SDK_VERSION } from "../Declarations/Constants"; import Logging = require("./Logging"); import * as PrefixHelpers from "./PrefixHelper"; import * as Constants from "../Declarations/Constants"; +import appInsights = require("../Bootstrap/Oryx"); class Context { @@ -67,7 +68,10 @@ class Context { private _loadInternalContext() { Context.sdkVersion = APPLICATION_INSIGHTS_SDK_VERSION; - this.tags[this.keys.internalSdkVersion] = `${PrefixHelpers.getResourceProvider()}${PrefixHelpers.getOsPrefix()}${Constants.AttachTypePrefix.MANUAL}_node:${Context.sdkVersion}` + // If Context is already set in the bootstrap, don't set it here + if (PrefixHelpers.getResourceProvider() === "u") { + this.tags[this.keys.internalSdkVersion] = `${PrefixHelpers.getResourceProvider()}${PrefixHelpers.getOsPrefix()}${Constants.AttachTypePrefix.MANUAL}_node:${Context.sdkVersion}` + } } } diff --git a/Tests/Bootstrap/Default.spec.ts b/Tests/Bootstrap/Default.spec.ts index 95bbb0dca..1e2f0ae4b 100644 --- a/Tests/Bootstrap/Default.spec.ts +++ b/Tests/Bootstrap/Default.spec.ts @@ -6,6 +6,7 @@ import * as Helpers from "../../Bootstrap/Helpers"; import * as DefaultTypes from "../../Bootstrap/Default"; import { JsonConfig } from "../../Library/JsonConfig"; import * as applicationinsights from "../../applicationinsights"; +import Context = require("../../Library/Context"); const appInsights = require("../../applicationinsights"); @@ -203,4 +204,44 @@ describe("#setupAndStart()", () => { assert.equal(result.defaultClient.config.enableAutoCollectHeartbeat, false, "wrong enableAutoCollectHeartbeat"); assert.equal(result.defaultClient.config.enableUseDiskRetryCaching, false, "wrong enableUseDiskRetryCaching"); }); + + it("should get App Services prefix correctly", () => { + const env = <{ [id: string]: string }>{}; + env["APPLICATIONINSIGHTS_CONNECTION_STRING"] = "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333;IngestionEndpoint=https://centralus-0.in.applicationinsights.azure.com/"; + env["WEBSITE_SITE_NAME"] = "test-site"; + process.env = env; + const Default = require("../../Bootstrap/Default") as typeof DefaultTypes; + Default.setupAndStart(null, false); + const appInsightsSDKVersion = appInsights.defaultClient.context.keys.internalSdkVersion; + + // Test + if (process.platform === "win32") { + assert.equal(appInsights.defaultClient.context.tags[appInsightsSDKVersion], `awi_node:${Context.sdkVersion}`, "SDK version tag is incorrect"); + } else if (process.platform === "linux") { + assert.equal(appInsights.defaultClient.context.tags[appInsightsSDKVersion], `ali_node:${Context.sdkVersion}`, "SDK version tag is incorrect"); + } else { + // Case that the test is being run on an OS not supported by App Service + assert.ok(true); + } + }); + + it("should get Azure Functions prefix correctly", () => { + const env = <{ [id: string]: string }>{}; + env["APPLICATIONINSIGHTS_CONNECTION_STRING"] = "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333;IngestionEndpoint=https://centralus-0.in.applicationinsights.azure.com/"; + env["FUNCTIONS_WORKER_RUNTIME"] = "test-function"; + process.env = env; + const Default = require("../../Bootstrap/Default") as typeof DefaultTypes; + Default.setupAndStart(null, false); + const appInsightsSDKVersion = appInsights.defaultClient.context.keys.internalSdkVersion; + + // Test + if (process.platform === "win32") { + assert.equal(appInsights.defaultClient.context.tags[appInsightsSDKVersion], `fwi_node:${Context.sdkVersion}`, "SDK version tag is incorrect"); + } else if (process.platform === "linux") { + assert.equal(appInsights.defaultClient.context.tags[appInsightsSDKVersion], `fli_node:${Context.sdkVersion}`, "SDK version tag is incorrect"); + } else { + // Case that the test is being run on an OS not supported by App Service + assert.ok(true); + } + }); }); diff --git a/Tests/Library/Context.tests.ts b/Tests/Library/Context.tests.ts index 0b68b9e1f..0e2c55e0e 100644 --- a/Tests/Library/Context.tests.ts +++ b/Tests/Library/Context.tests.ts @@ -57,7 +57,7 @@ describe("Library/Context", () => { } }); - it("should set internalSdkVersion to 'prefix_node:'", () => { + it("should set internalSdkVersion to 'prefix_node:' in manual SDK scenarios", () => { var context = new Context(); const packageJsonPath = path.resolve(__dirname, "../../../", "./package.json"); let packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));