-
Notifications
You must be signed in to change notification settings - Fork 257
Enable Customer SDK Stats #2707
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
Open
JacksonWeber
wants to merge
32
commits into
microsoft:main
Choose a base branch
from
JacksonWeber:jacksonweber/enable-customer-sdk-stats
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 c3b15e9
Delete customer_facing_sdk_stats_spec.md
JacksonWeber 8489db2
Delete customer_sdk_stats_implementation.md
JacksonWeber 7026790
Update flush of sdk stats.
JacksonWeber 545e012
Fix possible prototype pollution bug.
JacksonWeber 5b813a3
Update to resolve prototype pollution issue.
JacksonWeber 7ddc24f
Add tests.
JacksonWeber c35f113
Resolve prototype-polluting assignment issues.
JacksonWeber d75c392
Address github comments.
JacksonWeber f397c3e
Update SdkStatsNotificationCbk.ts
JacksonWeber 1adb17d
Update Sender.ts
JacksonWeber 296d1fc
Add notification of retry events in post-channel.
JacksonWeber b783b73
Add extra length check.
JacksonWeber 60e9a21
Move initialization inside onConfigChange.
JacksonWeber 79e6340
Address PR comments.
JacksonWeber d79a094
Update SdkStatsNotificationCbk.ts
JacksonWeber de82fb4
Pass full core instead of using local vars, fix bracket notation.
JacksonWeber 2e129ba
Add default values to the AppInsightsCore + AISKU.
JacksonWeber 672e580
Add dynamic config tests + preserving defaults.
JacksonWeber 43844de
Collapse to return number of non-minifiable "return"s.
JacksonWeber f9e1a51
Collapsed the props creation into createMetric.
JacksonWeber 4f87fda
Add default value for ver and update test.
JacksonWeber 87b829b
Update cfgsynchelper.tests.ts
JacksonWeber e55627c
Update SdkStatsFeature.tests.ts
JacksonWeber dbcc9dc
Remove ability to dynamically set version and language.
JacksonWeber c732efd
Update version string check.
JacksonWeber ba8a7f9
Remove "use strict".
JacksonWeber bccd721
Lock the language and version values to be internal.
JacksonWeber c5776d5
Update gruntfile to handle unique 1ds version.
JacksonWeber 9246cae
Derive 1ds version from core package version instead of 1ds-core-js p…
JacksonWeber 20d3096
Update per Nev's latest comments.
JacksonWeber a99ac77
Update tests.
JacksonWeber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" && | ||
| 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(); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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,