https://github.com/orian/counters
A GoLang implementation of counters.
https://github.com/orian/counters
counter go golang golang-library metrics-gathering
Last synced: 3 months ago
JSON representation
A GoLang implementation of counters.
- Host: GitHub
- URL: https://github.com/orian/counters
- Owner: orian
- License: mit
- Created: 2015-05-04T17:23:54.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2017-10-31T17:19:32.000Z (over 8 years ago)
- Last Synced: 2026-04-08T20:54:13.970Z (3 months ago)
- Topics: counter, go, golang, golang-library, metrics-gathering
- Language: Go
- Size: 20.5 KB
- Stars: 17
- Watchers: 1
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# counters
A GoLang implementation of counters.
It provides an object: `counters.CounterBox` and `Counter`, `Min` and `Max` implementation.
Installation:
$ go get -u github.com/orian/counters
The example usages:
import "github.com/orian/counters"
//... some code
cb := counters.NewCounterBox()
c := cb.GetCounter("ex")
c.Increment()
c.IncrementBy(6)
c.Value() // returns 7
cb.GetCounter("ex").Value() // Returns 7
For convenience there is also an `http.HandleFunc` provided which prints values of counters.
One may use subpackage `globals` if want to use a global counters.
import "github.com/orian/counters/global"
//... some code
c := global.GetCounter("ex")
c.Increment()
c.IncrementBy(6)
c.Value() // returns 7
global.GetCounter("ex").Value() // Returns 7
The library and all objects are thread safe.
## Print into log or stdout on SIGINT
Please check the `example/example.go` to see how the interrupt can be handled in a nice way.
## Performance and benchmarks
The library has two benchmarks. It's blazingly fast. One should cache counters
which are used often. Every time the counter is requested, it's necessary to
look up in map for a counter, this slows down the lib by a factor of 4.
$ go test --bench=. .
PASS
BenchmarkCounters 1000000 1287 ns/op
BenchmarkCountersCached 5000000 252 ns/op
ok github.com/orian/counters 2.822s