Recognos/Metrics.NET

Add Frequency Counter Metric

Closed this issue · 6 comments

Hi,

I needed to measure the exact frequency of a certain field showing up in a data structure.
First, I considered to use a histogram, but I needed an exact measurement, not median, average or 95% percentile etc.. So I ended up writing my own class - FrequencyMap, that maintains an inner Concurrent Dictionary of <Tkey, Counter> and creates a child context of the provided context, so that all the counters will not pollute the current context. something like:

public class FrequencyMap<Tkey>
{
    private MetricsContext _context;
    private ConcurrentDictionary<Tkey, Counter> _cache;
    private Unit _unit;
    public FrequencyMap(MetricsContext context, string name, Unit unit)
    {
        _context = context.Context(name);
        _unit = unit;
        _cache = new ConcurrentDictionary<Tkey, Counter>();
    }

    public Counter GetCounter(Tkey value)
    {
        return _chace.GetOrAdd(value, _context.Counter(value.ToString(), _unit));
    }
}

I think it would be handy to have a support for this kind of metric in the library, without the need to create "hacky" child contexts explicitly. I imagine it would be like some kind of hierarchy? It's up to you.
I think this need is common enough to be addressed officially.

Thanks :)

How is this different from rate, which provided by Timer?

I missed that the rate exists (it is not in the wiki, for example).
However, I'm not sure how one would use it to query the frequency of a certain field, like I implemented in my custom class.
Could you elaborate on how user code would use this property?

Actually, I didn't quite get your case. Here is a Timer's wiki There is a timer in general and also context based counter, where you can set the counter name per message type, lets say.

I'm not interested in the timing aspect.
Let's say I have a class that processes a certain structure, and one of its fields is a
byte requestType
I want to measure the exact frequency of each type (in the entire class lifetime). That's why I used a dictionary of TKey to Counter in my implementation.

But frequency/rate is about count per unit of time, isn't it?

Yeah, I think I get it.
I just had a thought, am I completely missing something? What about the Counter.Increment(string item) method, Wouldn't it solve my problem?