Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SDK Version Set in the Agent #1283

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions Bootstrap/Default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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.",
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 0 additions & 3 deletions Bootstrap/Oryx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
6 changes: 5 additions & 1 deletion Library/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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}`
}
}
}

Expand Down
41 changes: 41 additions & 0 deletions Tests/Bootstrap/Default.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
});
});
2 changes: 1 addition & 1 deletion Tests/Library/Context.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("Library/Context", () => {
}
});

it("should set internalSdkVersion to 'prefix_node:<version>'", () => {
it("should set internalSdkVersion to 'prefix_node:<version>' in manual SDK scenarios", () => {
var context = new Context();
const packageJsonPath = path.resolve(__dirname, "../../../", "./package.json");
let packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
Expand Down
Loading