lukevenediger / statsd-csharp-client

A simple c# client library for statsd and statsd.net
MIT License
47 stars 20 forks source link

LogCount does not record negative count values #24

Open DevByStarlight opened 3 years ago

DevByStarlight commented 3 years ago

Greetings,

A) statsd supports negative counts - ie, increasing negative amount. However, the client is explicitly culling those. From Statsd.cs:

    private async Task SendMetricAsync(string metricType, string name, string prefix, long value, string postFix = null)
    {
        if (value < 0)
        {
            Trace.TraceWarning("Metric value for {0} was less than zero: {1}. Not sending.", name, value);
            return;
        }
        // ...

B) Presumably this is to help avoid user error. Although seeing negative stats show up would be indicative of a problem compared to the absence of said metric and a probably unseen trace/warning log.

C) Perhaps statsdcsharpclient could include a service init configuration override to allow user to pass negative metric values?

Default would be false for backwards compatibility, of course, but allow users to knowingly enable it for specific cases.

Let me know if would like me to do a pull request for such a change described above

D) Use case example:

App Start: LogCount ( "Birth", 1 );
Every 60s: LogCount ( "Alive", 1 );   // record a heartbeat every 60s
App Shutdown: LogCount ( "Death", -1 );

When charting the above metrics we can easily obtain an up/down graphic over time without additional data manipulation - eg, without inverting a positive death count for every time bucket.

Support for negatives is simply for convenience and reducing downstream overhead. Current workaround is to simply use a positive "death" and negate it when generating view data.

Sincerely

niemyjski commented 3 years ago

Nice find and thanks for reaching out. If you want to submit a pr for this and make sure tests pass, I'll merge it for you. I think we just go with option a.