Tony Basallo
Tony Basallo

Reputation: 3074

Timing Events in Application Insight (Azure)

I was looking for a way to time events and have them graphed on Azure. Looking for hot spots where the events are slower for further analysis.

I can currently do the following, for instance:

var p = new Dictionary<string, string> {{ "StartTime", startTime.Value.ToString("g") }, { "EndTime", endTime.Value.ToString("g") }};
var m = new Dictionary<string, double> {{ "ElapsedSeconds", (endTime.Value - startTime.Value).TotalSeconds }};

ai.TrackEvent(eventName, p, m);

This will allow me to see one event at a time and know how long it took. But there's no easy way to view it charted. However, I noticed that he javascript library has a startTrackEvent and stopTrackEvent (AI docs) which would seem ideal.

Has anyone seen a built-in way or an existing way to track timed server events?

Upvotes: 2

Views: 2843

Answers (4)

XavierAM
XavierAM

Reputation: 1775

After looking for a while, I think that what you were looking for was TrackDependency method.

It shows nicely on the dependency graph how long does the program spends on each part of the code.

Upvotes: 2

OzBob
OzBob

Reputation: 4520

AI has a feature that offers a solution using TrackRequest:

//Establish an operation context and associated telemetry item:
using (var operation = telemetryClient.StartOperation<RequestTelemetry>("operationName"))
{
    // Telemetry sent in here will use the same operation ID.
    ...
    telemetryClient.TrackTrace(...); // or other Track* calls
    ...
    // Set properties of containing telemetry item--for example:
    operation.Telemetry.ResponseCode = "200";

    // Optional: explicitly send telemetry item:
    telemetryClient.StopOperation(operation);

} // When operation is disposed, telemetry item is sent.

Upvotes: 0

John Gardner
John Gardner

Reputation: 25116

And you can then take something like Ketan's answer, and wrap it with a disposable, like:

internal class TimedEvent : IDisposable
{
    private string name;
    private Dictionary<string,string> properties;
    private Stopwatch timer = Stopwatch.StartNew();

    public TimedEvent(string name, Dictionary<string,string> properties = null)
    {
        this.name = name;
        this.properties = properties;
    }

    public void Dispose()
    {
        timer.Stop();
        YourTelemetryClientHere.TrackEvent(this.name, this.properties, 
            new Dictionary<string, double> { { "duration", timer.ElapsedMilliseconds } });
    }
}

and then in your code you can do something like

using (new TimedEvent("myEvent"))
{
     // do something that takes time
}

Upvotes: 7

Ketan
Ketan

Reputation: 76

We have the functionality in SDK already to send custom metrics along with the event, but currently those metrics are not shown in UI. In couple weeks you'll be able to see the custom metrics in Application Insights. So you should elapseSeconds as a metric for the event. IDictionary<string, double> mDictionary = new Dictionary<string, double>(); mDictionary.Add("ElaspsedSeconds", m); ai.TrackEvent(eventName, mDictionary); In couple weeks you'll be be chart these metrics as any other metrics you see in Application Insights.

Upvotes: 1

Related Questions