Search code examples
azureazure-application-insights

Pair events in Application Insights Analytics


I need to app insights traces with following pattern for messages:

  • "D1 connected"
  • "D2 connected"
  • "D3 connected"
  • "D1 disconnected"
  • "D3 disconnected"
  • "D1 connected"
  • "D2 disconnected"
  • etc.

I'm basically monitoring some devices and the connection time. How can I write a query that "pairs" events (D1 connected/disconnected, D2 connected/disconnected, etc.) and evaluates how long the "sessions" are?

I'd need to get information like:

  • total connection time for a day
  • distribution of the connection for a specific device on a day
  • etc.

Solution

  • Doing this just based on the text of the trace will be hard. I suggest using custom properties to assist in this.

    By far the easiest option is to send some additional properties along with the disconnected event that have all the info required. Like:

    // Start of session
    var tt = new TraceTelemetry("D1 connected");
    tt.Properties.Add("Event", "SessionStart");    
    telemetryClient.TrackTrace(tt);
    var startTime = DateTime.Now;
    
    // Do your thing
    ....
    
    tt = new TraceTelemetry("D1 disconnected");
    tt.Properties.Add("Event", "SessionEnd");
    tt.Properties.Add("SessionLength", (startTime - DateTime.Now).TotalMilliseconds.ToString());
    telemetryClient.TrackTrace(tt);
    

    Custom properties are stored in the customDimensions field of an event.

    Now in AI analytics you can query these values like this:

    Count:

    traces
    | where customDimensions.Event == "SessionEnd"   
    | summarize count() 
    

    Session lengths:

    traces
    | where customDimensions.Event == "SessionEnd"
    | project message, customDimensions.Length
    

    Total duration of all sessions:

    traces
    | where customDimensions.Event == "SessionEnd"
    | extend duration = tolong(customDimensions.SessionLength)
    | summarize sum(duration)    
    

    I would also suggest adding the device Id as a custom property for all emitted events. It will make querying easier. You can then calculate min, max and average session lengths per device, for example:

    traces
    | where customDimensions.Event == "SessionEnd"
    | extend duration = tolong(customDimensions.SessionLength)
    | extend device = tostring(customDimensions.DeviceName)
    | summarize sum(duration) by device   
    

    If you want to join the start events as well or cannot or will not do the above you have to join the start events with the end events to make these queries. You will still need to use some custom properties since querying on text alone will be hard because you will then need to analyze the text to determine what event and what device is involved.

    take a look here azure AI QUERY combine start and response to calculate average to see how joins work in AI Analytics.