Conversation
There was a problem hiding this comment.
Pull request overview
This pull request introduces comprehensive SDK statistics tracking and enhances the notification system to support telemetry retry events. The main objective is to enable periodic reporting of SDK usage metrics (success, dropped, and retry counts) and provide visibility into the complete telemetry lifecycle including retry scenarios.
Changes:
- Adds
eventsRetrynotification callback to INotificationManager and INotificationListener interfaces for tracking telemetry retry events with HTTP status codes - Implements
SdkStatsNotificationCbk- a notification listener that accumulates telemetry counts by type and periodically reports them as metrics - Integrates SDK stats listener into AISku with automatic initialization (enabled by default) and proper cleanup during unload
- Updates Sender to trigger notifications for sent, discarded, and retried events, storing baseType in IInternalStorageItem for telemetry type mapping
- Adds comprehensive unit tests for the SDK stats notification callback covering all major scenarios
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| shared/AppInsightsCore/src/interfaces/ai/INotificationManager.ts | Adds eventsRetry method signature to notification manager interface |
| shared/AppInsightsCore/src/interfaces/ai/INotificationListener.ts | Adds optional eventsRetry callback to listener interface |
| shared/AppInsightsCore/src/index.ts | Exports new SDK stats notification callback factory and interfaces |
| shared/AppInsightsCore/src/core/SdkStatsNotificationCbk.ts | New file implementing the SDK stats accumulator and periodic metric flushing logic |
| shared/AppInsightsCore/src/core/NotificationManager.ts | Implements eventsRetry notification dispatch to all registered listeners |
| shared/AppInsightsCore/src/constants/InternalConstants.ts | Adds STR_EVENTS_RETRY constant for the new notification type |
| shared/AppInsightsCore/Tests/Unit/src/aiunittests.ts | Registers new SDK stats notification callback test suite |
| shared/AppInsightsCore/Tests/Unit/src/ai/SdkStatsNotificationCbk.Tests.ts | New comprehensive test suite covering all SDK stats functionality |
| channels/applicationinsights-channel-js/src/Sender.ts | Adds notification triggers for sent/discarded/retry events and extracts telemetry items for notifications |
| channels/applicationinsights-channel-js/src/Interfaces.ts | Adds bT (baseType) property to IInternalStorageItem for telemetry type mapping |
| AISKU/src/AISku.ts | Integrates SDK stats listener with initialization, configuration, and unload handling |
shared/AppInsightsCore/Tests/Unit/src/ai/SdkStatsNotificationCbk.Tests.ts
Show resolved
Hide resolved
9ed6655 to
7eaafe0
Compare
| // Notify listeners of successful send | ||
| let mgr = _getNotifyMgr(); | ||
| if (mgr) { | ||
| let items = _extractTelemetryItems(payload); |
There was a problem hiding this comment.
Follow up PR -- lets try and minimize the need to call this, it might be a large PR as it will most likely require changing the whole serialization process (like we do in 1ds-post-js) where we convert to a string as late as possible so at this level we are still playing with the objects -- there is a gotya around the persistence to the session storage / array storage today for this though.
AISKU/src/AISku.ts
Outdated
| [SDK_STATS]: {mode: FeatureOptInMode.enable} | ||
| }, | ||
| sdkStats: cfgDfMerge({ | ||
| lang: "JavaScript", |
There was a problem hiding this comment.
Do we really want to make this a config? As this would allow end-users to change this to anything they like
There was a problem hiding this comment.
Removed lang from being dynamically configurable.
AISKU/src/AISku.ts
Outdated
| }, | ||
| sdkStats: cfgDfMerge({ | ||
| lang: "JavaScript", | ||
| ver: UNDEFINED_VALUE, |
There was a problem hiding this comment.
I guess likewise with this one.
| var props: { [key: string]: any } = { | ||
| telemetry_type: telType, | ||
| language: statsCfg.lang || "JavaScript", | ||
| version: statsCfg.ver || "unknown", |
There was a problem hiding this comment.
Rather than defaulting to "Unknown" we could put a const version at the top of the file, which would be set by the build -- downside, for 1DS this would report the ApplicationInsights version and not the 1DS version.
And I guess these 2 setting are also part of the eariler comment about "letting" the end-user change / set the config -- should both of these be some sort of "internal" config (which we don't currently have). But there is a pluginString that we could use something like this that could expose the SDK version etc
There was a problem hiding this comment.
Added this constant to pull version from the build.
| 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" && |
There was a problem hiding this comment.
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,
This pull request introduces SDK statistics telemetry collection and notification improvements, as well as enhanced event retry notification and error reporting in the Application Insights JavaScript SDK. The main changes add support for dynamically enabling/disabling SDK stats telemetry, improve notification to listeners about retried and discarded events (with status codes), and enhance test coverage for these new behaviors.
SDK Statistics Telemetry and Dynamic Listener Management
SdkStats) in the main configuration and enabled dynamic creation and cleanup of the SDK stats notification listener (_sdkStatsListener) based on feature flags. The listener is properly unloaded and flushed with other telemetry during shutdown. (AISKU/src/AISku.ts) [1] [2] [3] [4] [5] [6] [7]Event Retry and Notification Enhancements
channels/1ds-post-js/src/PostChannel.ts) [1] [2] [3]Testing Improvements
eventsRetrynotification is fired when events are requeued after a retriable failure, ensuring the feature works as intended. (channels/1ds-post-js/test/Unit/src/PostChannelTest.ts) [1] [2] [3]Channel Plugin Notification Improvements
baseTypefor SDK stats mapping.channels/applicationinsights-channel-js/src/Interfaces.ts,channels/applicationinsights-channel-js/src/Sender.ts) [1] [2] [3] [4] [5] [6] [7] [8]