ApplicationInsights-JS icon indicating copy to clipboard operation
ApplicationInsights-JS copied to clipboard

Dynamically updating config (for extensions in my case)

Open mikecabana opened this issue 5 years ago • 8 comments

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

mikecabana avatar Nov 16 '20 21:11 mikecabana

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

  • Enable updating / reconfiguring of plugins (and core) after initialization
  • As part of this Investigate potential for dynamically (late loading) of additional plugins after initialization (ideally, this will get included, but needs to be investigated first)

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)

MSNev avatar Nov 16 '20 21:11 MSNev

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?

mikecabana avatar Nov 16 '20 21:11 mikecabana

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.

MSNev avatar Nov 16 '20 21:11 MSNev

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

mikecabana avatar Nov 16 '20 21:11 mikecabana

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

mikecabana avatar Nov 18 '20 16:11 mikecabana

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 avatar Apr 15 '22 00:04 MSNev

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 avatar Apr 18 '22 23:04 MSNev

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

MSNev avatar Apr 22 '22 18:04 MSNev

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.

github-actions[bot] avatar Apr 12 '24 00:04 github-actions[bot]