Skip to content
rbirkby edited this page Nov 5, 2014 · 2 revisions

A meter measures the rate at which an event occurs. From the Java library documentation:

Meters measure the rate of the events in a few different ways. The mean rate is the average rate of events. It’s generally useful for trivia, but as it represents the total rate for your application’s entire lifetime (e.g., the total number of requests handled, divided by the number of seconds the process has been running), it doesn’t offer a sense of recency. Luckily, meters also record three different exponentially-weighted moving average rates: the 1-, 5-, and 15-minute moving averages. Just like the Unix load averages visible in uptime or top.

Usage Sample
public class RequestProcessor
{
    private readonly Meter meter =
        Metric.Meter("Errors", Unit.Requests, TimeUnit.Seconds);

    public void ProcessRequest()
    {
        try
        {
            // do actual processing
        }
        catch
        {
            meter.Mark(); // records an error 
            throw;
        }
    }
}

The Meter also provides the ability to track the rate for each item from a finite set. For example, you have a system that processes a set of commands. You want to record the error rate for the command execution. You also would like to know the error rate for each type of command separately. If the number of commands is large, manually defining a meter for each command can be hard.

Luckily the meter can keep track of sub-meters for you:

public class SetMeterSample
{
    private readonly Meter errorMeter = Metric.Meter("Errors", Unit.Errors);

    public interface Command { }
    public class SendEmail : Command { }
    public class ShipProduct : Command { }
    public class BillCustomer : Command { }
    public class MakeInvoice : Command { }
    public class MarkAsPreffered : Command { }

    public void Process(Command command)
    {
        try
        {
            ActualCommandProcessing(command);
        }
        catch
        {
            errorMeter.Mark(command.GetType().Name);
        }
    }
}

After running some requests, the output of the Meter in text format looks like this:

   Errors
             Count = 450 Errors
        Mean Value = 35.68 Errors/s
     1 Minute Rate = 25.44 Errors/s
     5 Minute Rate = 24.30 Errors/s
    15 Minute Rate = 24.10 Errors/s
       Total Items = 5
            Item 0 = 19.56%    88 Errors [BillCustomer]
             Count = 88 Errors
        Mean Value = 6.98 Errors/s
     1 Minute Rate = 6.05 Errors/s
     5 Minute Rate = 6.01 Errors/s
    15 Minute Rate = 6.00 Errors/s
            Item 1 = 18.67%    84 Errors [MakeInvoice]
             Count = 84 Errors
        Mean Value = 6.66 Errors/s
     1 Minute Rate = 4.23 Errors/s
     5 Minute Rate = 3.89 Errors/s
    15 Minute Rate = 3.83 Errors/s
            Item 2 = 20.22%    91 Errors [MarkAsPreffered]
             Count = 91 Errors
        Mean Value = 7.22 Errors/s
     1 Minute Rate = 5.38 Errors/s
     5 Minute Rate = 5.24 Errors/s
    15 Minute Rate = 5.21 Errors/s
            Item 3 = 19.78%    89 Errors [SendEmail]
             Count = 89 Errors
        Mean Value = 7.06 Errors/s
     1 Minute Rate = 4.92 Errors/s
     5 Minute Rate = 4.67 Errors/s
    15 Minute Rate = 4.62 Errors/s
            Item 4 = 21.78%    98 Errors [ShipProduct]
             Count = 98 Errors
        Mean Value = 7.77 Errors/s
     1 Minute Rate = 4.86 Errors/s
     5 Minute Rate = 4.50 Errors/s
    15 Minute Rate = 4.43 Errors/s

As you can see the overall error rate is recorded, but also for each command the error rate is recorded.