Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
10a8584
Enable customer sdk stats.
JacksonWeber Feb 13, 2026
c3b15e9
Delete customer_facing_sdk_stats_spec.md
JacksonWeber Feb 18, 2026
8489db2
Delete customer_sdk_stats_implementation.md
JacksonWeber Feb 18, 2026
7026790
Update flush of sdk stats.
JacksonWeber Feb 18, 2026
545e012
Fix possible prototype pollution bug.
JacksonWeber Feb 20, 2026
5b813a3
Update to resolve prototype pollution issue.
JacksonWeber Feb 20, 2026
7ddc24f
Add tests.
JacksonWeber Feb 24, 2026
c35f113
Resolve prototype-polluting assignment issues.
JacksonWeber Feb 24, 2026
d75c392
Address github comments.
JacksonWeber Feb 25, 2026
f397c3e
Update SdkStatsNotificationCbk.ts
JacksonWeber Feb 25, 2026
1adb17d
Update Sender.ts
JacksonWeber Feb 25, 2026
296d1fc
Add notification of retry events in post-channel.
JacksonWeber Feb 26, 2026
b783b73
Add extra length check.
JacksonWeber Feb 27, 2026
60e9a21
Move initialization inside onConfigChange.
JacksonWeber Mar 4, 2026
79e6340
Address PR comments.
JacksonWeber Mar 4, 2026
d79a094
Update SdkStatsNotificationCbk.ts
JacksonWeber Mar 5, 2026
de82fb4
Pass full core instead of using local vars, fix bracket notation.
JacksonWeber Mar 6, 2026
2e129ba
Add default values to the AppInsightsCore + AISKU.
JacksonWeber Mar 7, 2026
672e580
Add dynamic config tests + preserving defaults.
JacksonWeber Mar 7, 2026
43844de
Collapse to return number of non-minifiable "return"s.
JacksonWeber Mar 7, 2026
f9e1a51
Collapsed the props creation into createMetric.
JacksonWeber Mar 7, 2026
4f87fda
Add default value for ver and update test.
JacksonWeber Mar 7, 2026
87b829b
Update cfgsynchelper.tests.ts
JacksonWeber Mar 8, 2026
e55627c
Update SdkStatsFeature.tests.ts
JacksonWeber Mar 8, 2026
dbcc9dc
Remove ability to dynamically set version and language.
JacksonWeber Mar 12, 2026
c732efd
Update version string check.
JacksonWeber Mar 13, 2026
ba8a7f9
Remove "use strict".
JacksonWeber Mar 18, 2026
bccd721
Lock the language and version values to be internal.
JacksonWeber Mar 18, 2026
c5776d5
Update gruntfile to handle unique 1ds version.
JacksonWeber Mar 18, 2026
9246cae
Derive 1ds version from core package version instead of 1ds-core-js p…
JacksonWeber Mar 19, 2026
20d3096
Update per Nev's latest comments.
JacksonWeber Mar 19, 2026
a99ac77
Update tests.
JacksonWeber Mar 20, 2026
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
306 changes: 306 additions & 0 deletions AISKU/Tests/Unit/src/SdkStatsFeature.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
import { ApplicationInsights, IConfig, IConfiguration } from '../../../src/applicationinsights-web';
import { AITestClass, Assert } from '@microsoft/ai-test-framework';
import { FeatureOptInMode, ISdkStatsNotifCbk, onConfigChange } from '@microsoft/applicationinsights-core-js';
import { AppInsightsSku } from '../../../src/AISku';
import { ICfgSyncMode } from '@microsoft/applicationinsights-cfgsync-js';

const TestInstrumentationKey = 'b7170927-2d1c-44f1-acec-59f4e1751c11';
const TestConnectionString = "InstrumentationKey=" + TestInstrumentationKey;

export class SdkStatsFeatureTests extends AITestClass {
private _ai: AppInsightsSku | null = null;

constructor() {
super("SdkStatsFeatureTests");
}

public testInitialize() {
try {
if (window.localStorage) {
window.localStorage.clear();
}
} catch (e) {
// ignore
}
}

public testFinishedCleanup(): void {
if (this._ai) {
this._ai.unload(false);
this._ai = null;
}
if (window.localStorage) {
window.localStorage.clear();
}
}

public registerTests() {
this._testSdkStatsEnabledByDefault();
this._testSdkStatsDisabledViaFeatureOptIn();
this._testSdkStatsDynamicEnableDisable();
this._testSdkStatsConfigDefaults();
this._testSdkStatsDynamicConfigChanges();
}

private _createAi(configOverrides?: Partial<IConfiguration & IConfig>): AppInsightsSku {
let config: IConfiguration & IConfig = {
connectionString: TestConnectionString,
extensionConfig: {
["AppInsightsCfgSyncPlugin"]: {
syncMode: ICfgSyncMode.Receive,
cfgUrl: ""
}
}
} as IConfiguration & IConfig;

if (configOverrides) {
for (let key in configOverrides) {
if (configOverrides.hasOwnProperty(key)) {
(config as any)[key] = (configOverrides as any)[key];
}
}
}

let ai = new ApplicationInsights({ config: config });
ai.loadAppInsights();
this._ai = ai;
return ai;
}

private _findSdkStatsListener(ai: AppInsightsSku): ISdkStatsNotifCbk | null {
let core = ai["core"];
let notifyMgr = core.getNotifyMgr();
let listeners = (notifyMgr as any).listeners;
if (listeners) {
for (let i = 0; i < listeners.length; i++) {
let listener = listeners[i];
// The SDK stats listener has a flush and unload method
if (listener && typeof listener.flush === "function" && typeof listener.unload === "function" &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

general comment (doesn't need to be changed)

This is potentially fragile and might break at some point in the future, as this would find the "first" listener that has all 4 functions and may not actually be the sdkStats listener -- maybe use a "known" symbol to tag the returned instance (object returned by the createSdkStats...) using effectively the Symbol.for (via symbolFor helper) as this returns the same global symbol for the provided string -- so symbolFor("sdkStats") always creates and returns the same symbol regardless of who creates it the first time,

typeof listener.eventsSent === "function" && typeof listener.eventsRetry === "function") {
return listener as ISdkStatsNotifCbk;
}
}
}
return null;
}

private _testSdkStatsEnabledByDefault() {
this.testCase({
name: "SdkStatsFeature: SDK stats listener is added by default when featureOptIn is not specified",
useFakeTimers: true,
test: () => {
let ai = this._createAi();
this.clock.tick(1);

let listener = this._findSdkStatsListener(ai);
Assert.ok(listener, "SDK Stats listener should be added by default");
}
});

this.testCase({
name: "SdkStatsFeature: SDK stats listener is added when SdkStats feature is explicitly enabled",
useFakeTimers: true,
test: () => {
let ai = this._createAi({
featureOptIn: {
["SdkStats"]: { mode: FeatureOptInMode.enable }
}
});
this.clock.tick(1);

let listener = this._findSdkStatsListener(ai);
Assert.ok(listener, "SDK Stats listener should be present when explicitly enabled");
}
});
}

private _testSdkStatsDisabledViaFeatureOptIn() {
this.testCase({
name: "SdkStatsFeature: SDK stats listener is NOT added when SdkStats feature is disabled",
useFakeTimers: true,
test: () => {
let ai = this._createAi({
featureOptIn: {
["SdkStats"]: { mode: FeatureOptInMode.disable }
}
});
this.clock.tick(1);

let listener = this._findSdkStatsListener(ai);
Assert.ok(!listener, "SDK Stats listener should NOT be present when disabled");
}
});
}

private _testSdkStatsDynamicEnableDisable() {
this.testCase({
name: "SdkStatsFeature: disabling SdkStats feature dynamically removes the listener",
useFakeTimers: true,
test: () => {
let ai = this._createAi();
this.clock.tick(1);

// Should be enabled by default
let listener = this._findSdkStatsListener(ai);
Assert.ok(listener, "SDK Stats listener should be present initially");

// Disable the feature
ai.config.featureOptIn = {
["SdkStats"]: { mode: FeatureOptInMode.disable }
};
this.clock.tick(1);

listener = this._findSdkStatsListener(ai);
Assert.ok(!listener, "SDK Stats listener should be removed after disabling feature");
}
});

this.testCase({
name: "SdkStatsFeature: re-enabling SdkStats feature dynamically adds the listener back",
useFakeTimers: true,
test: () => {
// Start with disabled
let ai = this._createAi({
featureOptIn: {
["SdkStats"]: { mode: FeatureOptInMode.disable }
}
});
this.clock.tick(1);

let listener = this._findSdkStatsListener(ai);
Assert.ok(!listener, "SDK Stats listener should NOT be present initially when disabled");

// Re-enable the feature
ai.config.featureOptIn = {
["SdkStats"]: { mode: FeatureOptInMode.enable }
};
this.clock.tick(1);

listener = this._findSdkStatsListener(ai);
Assert.ok(listener, "SDK Stats listener should be added after re-enabling feature");
}
});

this.testCase({
name: "SdkStatsFeature: toggling SdkStats feature multiple times works correctly",
useFakeTimers: true,
test: () => {
let ai = this._createAi();
this.clock.tick(1);

// Initially enabled
Assert.ok(this._findSdkStatsListener(ai), "Listener should be present (initial)");

// Disable
ai.config.featureOptIn = { ["SdkStats"]: { mode: FeatureOptInMode.disable } };
this.clock.tick(1);
Assert.ok(!this._findSdkStatsListener(ai), "Listener should be removed after first disable");

// Re-enable
ai.config.featureOptIn = { ["SdkStats"]: { mode: FeatureOptInMode.enable } };
this.clock.tick(1);
Assert.ok(this._findSdkStatsListener(ai), "Listener should be present after re-enable");

// Disable again
ai.config.featureOptIn = { ["SdkStats"]: { mode: FeatureOptInMode.disable } };
this.clock.tick(1);
Assert.ok(!this._findSdkStatsListener(ai), "Listener should be removed after second disable");
}
});
}

private _testSdkStatsConfigDefaults() {
this.testCase({
name: "SdkStatsFeature: sdkStats config defaults are applied (lang and int)",
useFakeTimers: true,
test: () => {
let ai = this._createAi();
this.clock.tick(1);

let config = ai.config;
Assert.ok(config.sdkStats, "sdkStats config should exist after initialization");
Assert.equal(900000, config.sdkStats!.int, "int should default to 900000 (15 minutes)");
}
});

this.testCase({
name: "SdkStatsFeature: user-provided sdkStats config is preserved",
useFakeTimers: true,
test: () => {
let ai = this._createAi({
sdkStats: {
int: 60000
}
});
this.clock.tick(1);

let config = ai.config;
Assert.ok(config.sdkStats, "sdkStats config should exist");
Assert.equal(60000, config.sdkStats!.int, "User-provided int should be preserved");
}
});
}

private _testSdkStatsDynamicConfigChanges() {
this.testCase({
name: "SdkStatsFeature: changing sdkStats.int dynamically triggers config change",
useFakeTimers: true,
test: () => {
let ai = this._createAi();
this.clock.tick(1);

let onChangeCalled = 0;
let expectedInt = 900000;

let handler = onConfigChange(ai.config as any, (details: any) => {
onChangeCalled++;
if (details.cfg.sdkStats) {
Assert.equal(expectedInt, details.cfg.sdkStats.int,
"sdkStats.int should be " + expectedInt + " in onChange callback");
}
});

Assert.equal(1, onChangeCalled, "onConfigChange should fire once initially");

// Change interval
expectedInt = 60000;
ai.config.sdkStats!.int = 60000;
this.clock.tick(1);
Assert.equal(2, onChangeCalled, "onConfigChange should fire again after changing int");

handler.rm();
}
});

this.testCase({
name: "SdkStatsFeature: replacing entire sdkStats object dynamically triggers config change",
useFakeTimers: true,
test: () => {
let ai = this._createAi();
this.clock.tick(1);

let onChangeCalled = 0;
let observedInt: number | undefined;

let handler = onConfigChange(ai.config as any, (details: any) => {
onChangeCalled++;
if (details.cfg.sdkStats) {
observedInt = details.cfg.sdkStats.int;
}
});

Assert.equal(1, onChangeCalled, "onConfigChange should fire once initially");

ai.config.sdkStats = {
int: 30000
};
this.clock.tick(1);
Assert.equal(2, onChangeCalled, "onConfigChange should fire after replacing sdkStats block");

Assert.equal(30000, observedInt, "int should be 30000 in callback");

handler.rm();
}
});
}
}
2 changes: 2 additions & 0 deletions AISKU/Tests/Unit/src/aiskuunittests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { TraceSuppressionTests } from "./TraceSuppression.Tests";
import { TraceProviderTests } from "./TraceProvider.Tests";
import { TraceContextTests } from "./TraceContext.Tests";
import { OTelInitTests } from "./OTelInit.Tests";
import { SdkStatsFeatureTests } from "./SdkStatsFeature.tests";

export function runTests() {
new OTelInitTests().registerTests();
Expand Down Expand Up @@ -58,4 +59,5 @@ export function runTests() {
new SpanContextPropagationTests().registerTests();
new SpanLifeCycleTests().registerTests();
new TelemetryItemGenerationTests().registerTests();
new SdkStatsFeatureTests().registerTests();
}
Loading
Loading