Oskar
Oskar

Reputation: 2083

Get TraceTelemetry to appear in the Application Insights Timeline

I have a small profiling class that implements IDisposable that starts a stopwatch in the constructor and stops it in Dispose() and log the elapsed time in milliseconds to Azure using TelemetryClient.TrackTrace. It works fine but I want it to appear on the Timeline in Azure Portal's Application Insights-page. I can get it appear if I use TelemetryClient.TrackDependency but since it's not a dependency I don't want to use that. Is there any solution to get it to appear on the timeline? I tried to add the elapsed time as a property called duration but that didn't work.

I don't know if it helps but a simplified version of the profiler would look something like this:

public class Profiler : IDisposable
{
    private readonly Stopwatch _stopwatch;
    private readonly ILogger _logger;
    private readonly string _name;
    private readonly DateTimeOffset _timestamp;

    public Profiler(string name)
    {
        _logger = LogFactory.GetLogger();

        _stopwatch = Stopwatch.StartNew();
        _timestamp = DateTimeOffset.UtcNow;
        _name = name;
    }

    public static Profiler Step(string name)
    {
        return new Profiler(name);
    }

    public void Dispose()
    {
        var telemetryClient = new TelemetryClient();

        _stopwatch.Stop();

        var message = $"Step - {_name} {_stopwatch.ElapsedMilliseconds} ms";

        var traceTelemetry = new TraceTelemetry(message, SeverityLevel.Verbose)
        {
            Timestamp = _timestamp
        };

        traceTelemetry.Properties.Add("Elapsed Milliseconds", $"{_stopwatch.ElapsedMilliseconds}");

        telemetryClient.TrackTrace(traceTelemetry);
    }
}

Upvotes: 3

Views: 1400

Answers (1)

Dmitry Matveev
Dmitry Matveev

Reputation: 2679

Timeline view mainly displays event types containing timestamp and duration as a base properties (but not as custom properties) such as Request, Dependency (displayed over time) or critical events with timestamp such as an exception (displayed as a point in time).

Traces and events are displayed in this view as a flat ordered list but not as a timeline (if you switch to the flat list view).

Considering this current implementation (unless changed) your suggestion to use Dependencies is one of the easiest ways to achieve timeline view in your case.

You can play with definitions here a bit - use Request type for overarching operation, use Dependencies for the methods, use Exceptions in case of the failures. This should fit the model nicely and provide a good timeline representation.

Upvotes: 7

Related Questions