microsoft / ApplicationInsights-JS

Microsoft Application Insights SDK for JavaScript
MIT License
650 stars 241 forks source link

Dynamically updating config (for extensions in my case) #1427

Closed mikecabana closed 1 year ago

mikecabana commented 4 years ago

Is your feature request related to a problem? Please describe. I'd like to be able to update the configuration of application insights after it has already been initialized. I've wrapped the package in an Angular Library using APP_INITIALIZER so using ai is much more straight forward for my team. The only issue is that something like the Angular plugin requires to run an init step. Not really compatible with passing config using forRoot({...})

Describe the solution you'd like I'd like to be able to do something like

addExtension(extension: ITelemetryPlugin, config?: { [extKey: string]: { [configKey: string]: any } }): void {
        const extensions = [...this.config.extensions, extension];
        this.config = { ...this.config, extensions };

        let extensionConfig = this.config.extensionConfig;
        if (config) {
            extensionConfig = { ...extensionConfig, config };
        }

        this.appInsights = new ApplicationInsights({
            config: this.config
        });
        this.appInsights.reLoadAppInsights();  // <--- new function to update config
    }

Running the above code of course errors out with

ERROR Error: Core should not be initialized more than once
MSNev commented 4 years ago

Hi @mikecabana, yes 100% agree, this is one of the enhancements that we will be looking at creating next year (based on current planning). It may not be exactly as you have described, but the high level design is

We can't commit to a timeframe just yet as there are some other largish features ahead of this work and this has implications on minification / tree shaking efforts (i.e. it's going to add more code)

mikecabana commented 4 years ago

Hey @MSNev that's awesome to hear. Looking forward to using this enhancement when it's released!!

It's not really a show stopper for now on my end but would you have any suggestions or workarounds that can get me going in the meantime?

MSNev commented 4 years ago

At this point no, the only suggestion I can immediately think of would be to create a lazy initialization layer. I assume your not using the snippet, in which case as part of that layer you would have to cache (save into an array -- the same way as the ai queue ) any calls you have and replay them one you initialize.

mikecabana commented 4 years ago

@MSNev Gotcha - I'll give it a shot thanks!

mikecabana commented 4 years ago

In case anyone comes across this thread, I think I came up with a decent workaround that's not to hard on the eyes ;). Improvements are welcome.

https://stackblitz.com/edit/angular-ivy-iujyla?devtoolsheight=33&file=src/app/app.module.ts

MSNev commented 2 years ago

An update on progress, we originally intended to deliver this as part of the next release v2.8.0 but because of the size of the changes required the next release will only partial support this.

Simplistically, v2.8.0 is going to support completely unloading and re-initializing the SDK (not ideal but a start) and we are planning that the next major release (v3.0.0) will include full support to dynamically update the config on the fly.

Timeline wise apart from bug fixes to the 2.8.0 lineage 3.x will be the next release that we will start working on (initially in the beta branch).

One other planned change to 3.x is that we are currently intending to drop ES3 (IE8) support.

MSNev commented 2 years ago

v2.8.0 is now released and published to NPM, because of the size of the change it isn't planned to be fully available on the CDN until April 25th, 2022

MSNev commented 2 years ago

Please use v2.8.1 as v2.8.0 had a couple of issue, specifically it disabled automatic exception tracking.

github-actions[bot] commented 7 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.