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

How to get metrics based on username

somaniab opened this issue · comments

i want to get data for my webservices . For each webservice , for each username , how much time is it taking .how can i do that ? can somebody help ?
i am using following code in webservice :
(var context = timer.NewContext(username))
and for getting the i have created a custom report , and in it i am overriding
protected override void ReportTimer(string name, TimerValue value, Unit unit, TimeUnit rateUnit, TimeUnit durationUnit, MetricTags tags)

but i am not getting username in this ? how do i get userName ?

@somaniab if you want to have a timer metric for each user, that will keep statistics for each user, you can create a timer for each user by including the username in the timer name:

using(var context = Metric.Timer("Request for user " + username,Unit.Requests).NewContext())
{
// measured operation goes here
}

The downside of this it that it will create and keep track of a timer instance for each user. If you have a very large amount of users that might not be optimal.

If you have multiple metrics that you want to group together by username, you could create a MetricsContext for each user:

using(var context = Metric.Context(username).Timer("Requests",Unit.Requests).NewContext())
{
// measured operation goes here
}

Also you could implement MetricsReport and get access to all metrics organized by contexts.

@etishor thanks for prompt reply and help .
1 ) is this possible with tags ? ( provide username:value tag in the timer , and then get the results organized )
2 ) is there any example to get metrics organized by context ?
the override method ReportTimer does not have parameter for context to fetch userName info .

  1. no, it is not possible to use tags for this. Tags are properties associated with a timer instance on the timer creation.
  2. some info is available in the wiki. You can also look at the samples

It is not a common usecase to keep track of metrics at user level, as you can have a large number of users. The most common usecase for a timer is to keep track of how much time a request takes regardless of the parameters of the request.

You can pass a custom string value to the timer.NewContext() method and the timer will remember this value for the request that took the least amount of time (MinUserValue), the most amount of time (MaxUserValue) and the last value (LastUserValue) it was called with. This can be used to investigate why a request took too long.

@etishor MaxuserValue will give value of most amount of the time , but for which user ? how to get that user info which i passed while creating context in my custom report. The sample provided , only deals with how to collect that info , i was looking for something how to read back this info in my custom report .

For that you would have to implement MetricsReport interface:

public class CustomReport : MetricsReport
{
    public void RunReport(MetricsData metricsData, Func<HealthStatus> healthStatus, CancellationToken token)
    {
        foreach (var timer in metricsData.Timers)
        {
            Console.WriteLine($"Timer {timer.Name} Max value {timer.Value.Histogram.Max} for {timer.Value.Histogram.MaxUserValue}");
        }
    }
}

@etishor ok i got the information from name parameter , i have to parse it though to get username . The MetricReport interface does not work . The values are null there . I have one question also , what is Unit.Requests and Unit.Errors for ?