Skip to content

otoolec/go-counter

 
 

Repository files navigation

GoLang Counter Utility

Why a counter utility?

When I programmed in C, we'd routinely add a counter to each place in the code where there was a control-flow affecting test. Then we had commands to print out the counters and since our code was able to run for a long time without crashing, we'd get a sort of free profile of the application flow.

Is it good to use?

I'm using it.

*What is it? *

This code offers an "increment a counter" API that is non-blocking, using only channels not locking, and which requires no upfront configuration (you provide a string name of the counter). There is also a sync version using atomic mutuxes; this is faster in the normal case (using less CPU and lower latency) but of course could be a source of contention in the general case. The channel API uses ten channels so that there's less contention for the channel.

At pretty high rates of counting (5 million calls/second, maybe a dozen things counted each call).

Each minute the counts will be printed out. The numbers will be old-school aligned and tabularized. If you provide a callback, then each minute you'll get a callback with all string names of the existing counters and values for sending to a TSDB type system for aggregating these metrics.

Who owns this code?

Chris Lane

Adivce for starting out

If you integrate, please let me know of your experience and any suggestions for improvement.

The current API can best be seen in the _test files probably.

One thing to be aware of is that calling InitCounters() will start up a go routine or 2 to manage per minute processing, and also will startup 10 go routines to listen to the ten channels used to reduce contention for the sending channel.

Requirements

None at present.

TODO

o String builder to speed up

o precalculate tables for deriveDistName maths

About

Simple thread-safe counting.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 97.6%
  • Makefile 2.4%