[BUG] Documentation and functionality does not match with what is on Microsoft's website
Description/Screenshot I am trying to implement the change that Microsoft brought out of the discontinuation of Instrumentation Key and use Connection String instead. On https://docs.microsoft.com/en-us/azure/azure-monitor/app/javascript it shows the Connection String field is required. But on your documentation it shows Instrumentation Key is still required and when I downloaded and tried
Steps to Reproduce Download package Set the config with just connectionstring: 'you connection string' on applicationInsight.loadAppInsights(); it errors saying 'InstrumentationKey is require.'
Expected behavior InstrumentationKey is not required and should work without InstrumentKey and can work with only ConnectionString
Additional context
When you use a connectionString a valid instrumentationKey should still be included in that string, so passing an invalid or incomplete connection string will trigger this generic message.
eg. InstrumentationKey=xxxxxxxx-xxxx-xxxx-xxxx-000000000000;IngestionEndpoint=https://southcentralus-0.in.applicationinsights.azure.com/
So when the connection string is parsed we internally populate the instrumentationKey field to avoid this error message
Which Sku are you using as currently the basic (@microsoft/applicationinsights-web-basic - AISKULight) does not support using a connection string.
And the documentation was only recently updated to include the announcement actual support does not stop until March 31st, 2025.
I'm using from '@microsoft/applicationinsights-web' so not basic. My connectionString works without a problem in my .Net Core. The format is exactly what you have as your example.
I think I am narrowing down the issue. If I have this as a variable to use in a service:
private applicationInsight = new ApplicationInsights({
config: {
connectionString: '',
},
});
Then I set the connectionString later, that is when I run into this issue.
public setupAppInsights(config: Snippet) {
if (!this.applicationInsight.config.connectionString) {
this.applicationInsight.config.connectionString = config.config.connectionString;
}
this.applicationInsight.loadAppInsights();
Thats not going to work. Updating the config after passing it into the SDK is not supported, you need to set the config values before passing into the SDK.
In a future release we will have limited support of updating the config after loading / initializing via a planned updateCfg method on the core -- should be in beta around the end of the month.
While the next release (v2.8.0 -- currently in beta and nightly) supports full unloading and then re-initialization of the SDK (via the initialize() method) which is also the foundation of the update. And this will be completely disconnecting the passed in config from the current running config, primarily so that when you "update" you can merge the original passed in values as opposed to merging with the resolved config.
Is there any problem if I parse the connection string out and update the config with the connection string and instrumentation key?
Well technically (at least currently -- I've not completely finalized the config changes for updating yet), if you pass in an Object as the config you should be able to update it BEFORE you call loadAppInsights() and it should work...
var theConfig = {
connectionString: ""
}
private applicationInsight = new ApplicationInsights(theConfig);
public setupAppInsights(config: Snippet) {
if (!theConfig.connectionString) {
theConfig.connectionString = myConfig.config.connectionString;
}
this.applicationInsight.loadAppInsights();
}
I believe we expose the internal parseConnectionString(connectionString?: string): ConnectionString so you could use that and then manually populate the config (BEFORE) calling loadAppInsights(), but as we use this internally your better off just setting the connectionString...
That is basically what I'm doing and I tried your code but it still fails.
Ok, just went to find the code that cracks the connection string and populate the values (which is here and it answers the problem...
Basically, the connection string is cracked during the constructor so you need to populate the connection string before creating the object or duplicate the code linked here in the above -- I'm not currently going to guarantee that (if) this works that it will continue to work... With my current local refactorings it looks like it might but I'm just looking at code and I don't currently have a working repro.
It seems to work with my approach if I populate both the connection string and instrumentation key.
Which Sku are you using as currently the basic (@microsoft/applicationinsights-web-basic - AISKULight) does not support using a connection string.
@MSNev could you please update documentation with this information as it's misleading since basic SKU constructor accepts connectionString as input parameter?