etishor / Metrics.NET

The Metrics.NET library provides a way of instrumenting applications with custom metrics (timers, histograms, counters etc) that can be reported in various ways and can provide insights on what is happening inside a running application.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow setting Timer context name after the context has started

lee-elenbaas opened this issue · comments

Sometimes feguring out the actual context is part of the processing that needs to be times.
In such a case, i want to be able to use my timer like this:

var timer = Metric.Timer("My Timer", Units.Requests);

using(var timerContext = timer.NewContext()){
     // perform the part of the processing enough for feguring out the context name
     timerContext.Name = "Name found";
     // complete the rest of the processing
}

This makes sense. The name (user value) is needed only when disposing the context.

I vote for this. It would be very useful.

Exposing a property does not work (as TimerContext is a struct) but exposing a method TrackUserValue(string) that sets the new user value seems to work. I don't particularly like mutating the struct but I guess the impact in this case is minimal.

See 8b823eb for details.

Keep in mind that TimerContext is a struct and don't pass it to another method that sets the user value. Stick to using code similar to this:

using(var x = timer.NewContext())
{
    var val = TimedMethod();
    x.TrackUserValue(val);
}