Skip to content

Commit d0330d7

Browse files
authored
Fix SDK Version Set in the Agent (#1283)
* Update how we set sdk version in the agent and update tests. * Update Context.tests.ts
1 parent 2284859 commit d0330d7

File tree

5 files changed

+55
-20
lines changed

5 files changed

+55
-20
lines changed

Bootstrap/Default.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import { DiagnosticLogger } from "./DiagnosticLogger";
88
import Config = require("../Library/Config");
99
import { DiagnosticLog, DiagnosticMessageId } from "./DataModel";
1010
import * as PrefixHelpers from "../Library/PrefixHelper";
11+
import Context = require("../Library/Context");
1112

1213
// Private configuration vars
1314
let _appInsights: typeof types | null;
14-
let _prefix = "ud_"; // Unknown, Default
15+
let _prefix = `${PrefixHelpers.getResourceProvider()}${PrefixHelpers.getOsPrefix()}${Constants.AttachTypePrefix.INTEGRATED_AUTO}_`;
16+
let _fullSdkVersion = `${_prefix}node:${Context.sdkVersion}`;
1517

1618
export const defaultConfig = new Config(); // Will read env variables, expose for Agent initialization
1719
const _instrumentationKey = defaultConfig.instrumentationKey;
@@ -30,14 +32,6 @@ export function setLogger(logger: DiagnosticLogger) {
3032
return _logger = logger;
3133
}
3234

33-
/**
34-
* Sets the string which is prefixed to the existing sdkVersion, e.g. `ad_`, `alr_`
35-
* @param prefix string prefix, including underscore. Defaults to `ud_`
36-
*/
37-
export function setUsagePrefix(prefix: string) {
38-
_prefix = prefix;
39-
}
40-
4135
export function setStatusLogger(statusLogger: StatusLogger) {
4236
_statusLogger = statusLogger;
4337
}
@@ -89,13 +83,9 @@ export function setupAndStart(aadTokenCredential?: azureCoreAuth.TokenCredential
8983

9084
/** Sets the SDK version prefix in auto-attach scenarios */
9185
const prefixInternalSdkVersion = function (envelope: types.Contracts.Envelope, _contextObjects: Object) {
92-
if (_prefix === "ud_") {
93-
// If SDK version prefix is not set - set it using {RP}{OS}{Attach Type}_ pattern
94-
_prefix = `${PrefixHelpers.getResourceProvider()}${PrefixHelpers.getOsPrefix()}${Constants.AttachTypePrefix.INTEGRATED_AUTO}_`
95-
}
86+
// If SDK version prefix is not set - set it using {RP}{OS}{Attach Type}_ pattern
9687
try {
97-
var appInsightsSDKVersion = _appInsights.defaultClient.context.keys.internalSdkVersion;
98-
envelope.tags[appInsightsSDKVersion] = _prefix + envelope.tags[appInsightsSDKVersion];
88+
envelope.tags[appInsightsSDKVersion] = _fullSdkVersion;
9989
} catch (e) {
10090
const diagnosticLog: DiagnosticLog = {
10191
message: "Error prefixing SDK version.",
@@ -122,6 +112,7 @@ export function setupAndStart(aadTokenCredential?: azureCoreAuth.TokenCredential
122112

123113
// Instrument the SDK
124114
_appInsights.setup();
115+
const appInsightsSDKVersion = _appInsights.defaultClient.context.keys.internalSdkVersion;
125116

126117
// Azure Functions
127118
if (isAzureFunction) {
@@ -195,6 +186,8 @@ export function setupAndStart(aadTokenCredential?: azureCoreAuth.TokenCredential
195186
}
196187

197188
_appInsights.start();
189+
// Set the SDK verison in the context
190+
_appInsights.defaultClient.context.tags[appInsightsSDKVersion] = _fullSdkVersion;
198191
// Add attach flag in Statsbeat
199192
let statsbeat = _appInsights.defaultClient.getStatsbeat();
200193
if (statsbeat) {

Bootstrap/Oryx.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import { StatusLogger } from "./StatusLogger";
33
import { DiagnosticLogger } from "./DiagnosticLogger";
44
import { NoopLogger } from "./NoopLogger";
55
import appInsightsLoader = require("./Default");
6-
import { AttachTypePrefix } from "../Declarations/Constants";
7-
8-
appInsightsLoader.setUsagePrefix(`al${AttachTypePrefix.INTEGRATED_AUTO}_`); // App Services Linux Auto Attach
96

107
// Set Status.json logger
118
appInsightsLoader.setStatusLogger(new StatusLogger(new NoopLogger()));

Library/Context.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { APPLICATION_INSIGHTS_SDK_VERSION } from "../Declarations/Constants";
77
import Logging = require("./Logging");
88
import * as PrefixHelpers from "./PrefixHelper";
99
import * as Constants from "../Declarations/Constants";
10+
import appInsights = require("../Bootstrap/Oryx");
1011

1112
class Context {
1213

@@ -67,7 +68,10 @@ class Context {
6768

6869
private _loadInternalContext() {
6970
Context.sdkVersion = APPLICATION_INSIGHTS_SDK_VERSION;
70-
this.tags[this.keys.internalSdkVersion] = `${PrefixHelpers.getResourceProvider()}${PrefixHelpers.getOsPrefix()}${Constants.AttachTypePrefix.MANUAL}_node:${Context.sdkVersion}`
71+
// If Context is already set in the bootstrap, don't set it here
72+
if (PrefixHelpers.getResourceProvider() === "u") {
73+
this.tags[this.keys.internalSdkVersion] = `${PrefixHelpers.getResourceProvider()}${PrefixHelpers.getOsPrefix()}${Constants.AttachTypePrefix.MANUAL}_node:${Context.sdkVersion}`
74+
}
7175
}
7276
}
7377

Tests/Bootstrap/Default.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as Helpers from "../../Bootstrap/Helpers";
66
import * as DefaultTypes from "../../Bootstrap/Default";
77
import { JsonConfig } from "../../Library/JsonConfig";
88
import * as applicationinsights from "../../applicationinsights";
9+
import Context = require("../../Library/Context");
910

1011

1112
const appInsights = require("../../applicationinsights");
@@ -203,4 +204,44 @@ describe("#setupAndStart()", () => {
203204
assert.equal(result.defaultClient.config.enableAutoCollectHeartbeat, false, "wrong enableAutoCollectHeartbeat");
204205
assert.equal(result.defaultClient.config.enableUseDiskRetryCaching, false, "wrong enableUseDiskRetryCaching");
205206
});
207+
208+
it("should get App Services prefix correctly", () => {
209+
const env = <{ [id: string]: string }>{};
210+
env["APPLICATIONINSIGHTS_CONNECTION_STRING"] = "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333;IngestionEndpoint=https://centralus-0.in.applicationinsights.azure.com/";
211+
env["WEBSITE_SITE_NAME"] = "test-site";
212+
process.env = env;
213+
const Default = require("../../Bootstrap/Default") as typeof DefaultTypes;
214+
Default.setupAndStart(null, false);
215+
const appInsightsSDKVersion = appInsights.defaultClient.context.keys.internalSdkVersion;
216+
217+
// Test
218+
if (process.platform === "win32") {
219+
assert.equal(appInsights.defaultClient.context.tags[appInsightsSDKVersion], `awi_node:${Context.sdkVersion}`, "SDK version tag is incorrect");
220+
} else if (process.platform === "linux") {
221+
assert.equal(appInsights.defaultClient.context.tags[appInsightsSDKVersion], `ali_node:${Context.sdkVersion}`, "SDK version tag is incorrect");
222+
} else {
223+
// Case that the test is being run on an OS not supported by App Service
224+
assert.ok(true);
225+
}
226+
});
227+
228+
it("should get Azure Functions prefix correctly", () => {
229+
const env = <{ [id: string]: string }>{};
230+
env["APPLICATIONINSIGHTS_CONNECTION_STRING"] = "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333;IngestionEndpoint=https://centralus-0.in.applicationinsights.azure.com/";
231+
env["FUNCTIONS_WORKER_RUNTIME"] = "test-function";
232+
process.env = env;
233+
const Default = require("../../Bootstrap/Default") as typeof DefaultTypes;
234+
Default.setupAndStart(null, false);
235+
const appInsightsSDKVersion = appInsights.defaultClient.context.keys.internalSdkVersion;
236+
237+
// Test
238+
if (process.platform === "win32") {
239+
assert.equal(appInsights.defaultClient.context.tags[appInsightsSDKVersion], `fwi_node:${Context.sdkVersion}`, "SDK version tag is incorrect");
240+
} else if (process.platform === "linux") {
241+
assert.equal(appInsights.defaultClient.context.tags[appInsightsSDKVersion], `fli_node:${Context.sdkVersion}`, "SDK version tag is incorrect");
242+
} else {
243+
// Case that the test is being run on an OS not supported by App Service
244+
assert.ok(true);
245+
}
246+
});
206247
});

Tests/Library/Context.tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe("Library/Context", () => {
5757
}
5858
});
5959

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

0 commit comments

Comments
 (0)