|
4 | 4 | [](https://godoc.org/github.com/zserge/metric)
|
5 | 5 | [](https://goreportcard.com/report/github.com/zserge/metric)
|
6 | 6 |
|
| 7 | +Package provides simple uniform interface for metrics such as counters, |
| 8 | +gauges and histograms. It keeps track of metrics in runtime and can be used for |
| 9 | +some basic web service instrumentation in Go, where complex tools such as |
| 10 | +Prometheus or InfluxDB are not required. |
| 11 | + |
| 12 | +It is compatible with [expvar](https://golang.org/pkg/expvar/) package, that is |
| 13 | +also commonly used for monitoring. |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +```go |
| 18 | +// Create new metric. All metrics may take time frames if you want them to keep |
| 19 | +// history. If no time frames are given the metric only keeps track of a single |
| 20 | +// current value. |
| 21 | +c := metric.NewCounter("15m10s") // 15 minutes of history with 10 second precision |
| 22 | +// Increment counter |
| 23 | +c.Add(1) |
| 24 | +// Return JSON with all recorded counter values |
| 25 | +c.String() // Or json.Marshal(c) |
| 26 | + |
| 27 | +// With expvar |
| 28 | + |
| 29 | +// Register a metric |
| 30 | +expvar.Publish("latency", metric.NewHistogram("5m1s", "15m30s", "1h1m")) |
| 31 | +// Register HTTP handler to visualize metrics |
| 32 | +http.Handle("/debug/metrics", metric.Handler(metric.Exposed)) |
| 33 | + |
| 34 | +// Measure time and update the metric |
| 35 | +start := time.Now() |
| 36 | +... |
| 37 | +expvar.Get("latency").(metric.Metric).Add(time.Since(start).Seconds()) |
| 38 | +``` |
| 39 | + |
| 40 | +Metrics are thread-safe and can be updated from background goroutines. |
| 41 | + |
| 42 | +## Web UI |
| 43 | + |
| 44 | +Nothing fancy, really, but still better than reading plain JSON. No javascript, |
| 45 | +only good old HTML, CSS and SVG. |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +Of course you may customize a list of metrics to show in the web UI. |
| 50 | + |
| 51 | +If you need precise values - you may use `/debug/vars` HTTP endpoint provided |
| 52 | +by `expvar`. |
| 53 | + |
| 54 | +## License |
| 55 | + |
| 56 | +Code is distributed under MIT license, feel free to use it in your proprietary |
| 57 | +projects as well. |
0 commit comments