Skip to content
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

Reconfiguration of JS snippet via module import ignores settings #1370

Closed
coonmoo opened this issue Sep 15, 2020 · 5 comments
Closed

Reconfiguration of JS snippet via module import ignores settings #1370

coonmoo opened this issue Sep 15, 2020 · 5 comments

Comments

@coonmoo
Copy link

coonmoo commented Sep 15, 2020

Previously we just used the applicationinsights-web module in our Angular application, but that didn't track any exceptions occurring before loading the Angular ApplicationInsights service.

So now we load the ApplicationInsights CDN JS snippet in the head part of our page and after bootstrapping the Angular application we re-configure ApplicationInsights with values provided from our API.

    const instance = (window as any).appInsights;
    instance.config.correlationHeaderDomains = configuration.correlationHeaderDomains;
    instance.config.enableCorsCorrelation = configuration.enableCorrelation;
    instance.addTelemetryInitializer(configuration.initializers);

In the head script we have enableCorsCorrelation:false it seems having the Angular service setting enableCorsCorrelation=true on the global appInsights instance seems to have no effect.
Correlation headers are missing.

Is there a proper way to provide new configuration values?

@MSNev
Copy link
Collaborator

MSNev commented Sep 15, 2020

Changing the configuration after initialization is not a (generally) supported scenario --

While it may (currently) work as a side effect of some plugin implementations, unless it's specifically documentation as "supported" ALL configuration should be set prior to calling initialize() on the appInsights instance.

@coonmoo
Copy link
Author

coonmoo commented Sep 16, 2020

Thanks for the fast feedback.

Is there any way to support the scenario I'm describing? We want to be able to catch client exceptions occurring prior to having application insights configuration settings available (these are tenant specific and we first need to fetch them from the API).

I also tried replacing the ApplicationInsights instance created in the head section with a new one:
(window as any).appInsights = new ApplicationInsights({....})

but that resulted in having e.g. ajax dependencies being tracked twice.

Can we somehow flush and dispose / unload the first instance before creating a new one?

@MSNev
Copy link
Collaborator

MSNev commented Sep 16, 2020

The Snippet should be handling catching all errors that occur from the point of running snippet script (i.e. any exceptions prior to snippet script won't be caught).

You should be able to set you configuration on the snippet chunk (in the cfg section) and you can add the telemetry initializer to the queue mechanism that is available by (currently) adding some code to the end of the snippet script (in the same script tag)

I have some ideas on making this a bit cleaner, but for now it's something like
<script... the snippet block
...
cfg: { // Application Insights Configuration
instrumentationKey: "INSTRUMENTATION_KEY"
}});
if (appInsights && appInsights.queue) {
// Add telemetry initializer
appInsights.queue.push(function () {
appInsights.addTelemetryInitializer(function (envelope) {
//Add a custom property
envelope.data.name = 'This item passed through my telemetry initializer';
});
});
}
</script>

If you put the addTelemetryInitializer in a trailing script block, it very likely that the queue has already been processed and therefore the above code (the added queue function) will actually never get called.

@MSNev
Copy link
Collaborator

MSNev commented Mar 22, 2021

Linked to future enhancement #1427 and closing this issue

@MSNev MSNev closed this as completed Mar 22, 2021
@github-actions
Copy link

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 github-actions bot locked as resolved and limited conversation to collaborators Mar 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants