简体   繁体   中英

Build version information in Application Insights telemetry (client-side)

We have a SPA hosted on ASP.NET application. We want to track the build version of the whole app.

So this telemetry initializer

public class VersionInfoTelemetryInitializer : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        telemetry.Context.Component.Version =
          typeof(Startup).Assembly.GetName().Version.ToString();
    }
}

will be used in Gloabal.asax

public class MvcApplication : HttpApplication
{
    protected void Application_Start()
    {
        var tc = TelemetryConfiguration.Active;
        tc.InstrumentationKey = ConfigurationManager.AppSettings["AI Instrumentation Key"];
        tc.TelemetryInitializers.Add(new VersionInfoTelemetryInitializer());
        ...
    }
}

Server-side telemetry will have version information appended. But I am not able to do the same for the client-side telemetry. I have tried this

var appInsights = window.appInsights || function(config) {
    // standard js snippet from azure portal
}({
    instrumentationKey: '{{INSTRUMENTATIONKEY}}'
});
window.appInsights = appInsights;
window.appInsights.context.application.ver = 'some version number';

which results in following JS error

Uncaught TypeError: Cannot read property 'application' of undefined

I also tried

appInsights.queue.push(function () {
    appInsights.context.addTelemetryInitializer(versionInfoTelemetryInitialier);
});

function versionInfoTelemetryInitialier(envelope) {
    var telemetryItem = envelope.data.baseData;
    telemetry.context.component.version = 'some version number';
}

which will warn with following message

AI: TelemetryInitializerFailed message:"One of telemetry initializers failed, telemetry
item will not be sent: TypeError" props:"{exception:[object Error]{ stack: 'TypeError:
Unable to get property 'component' of undefined or null reference\n   at
versionInfoTelemetryInitialier (https://localhost:44301/landing/index:107:9)\n   at
n.prototype._track (https://az416426.vo.msecnd.net/scripts/a/ai.0.js:1:65589)\n   at
n.prototype.track...

What should I do so that client-side telemetry has version information attached.

i think your second attempt is very close. you need to do it via the queue, to make sure it occurs after all the AI scripts are actually loaded, so i think this is correct:

appInsights.queue.push(function () {
    appInsights.context.addTelemetryInitializer(versionInfoTelemetryInitialier);
});

but in your initializer you switched from context.application.ver in your first example, to context.component.version in your second.

the javascript SDK is documented on the github repo: https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md

and the examples there show:

 context.application.ver: string
 context.application.build : string

so shouldn't that initializer method be:

function versionInfoTelemetryInitialier(envelope) {
   var telemetryItem = envelope.data.baseData;
   telemetry.context.application.ver = 'some version number';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM