|
1 | | -# prom-stats |
2 | | -Prometheus adapter for bool64/stats |
| 1 | +# Prometheus Adapter for Contextualized Stats Tracker |
| 2 | + |
| 3 | +This library provides context-driven stats tracker implementation. |
| 4 | + |
| 5 | +[](https://github.com/bool64/prom-stats/actions?query=branch%3Amaster+workflow%3Atest-unit) |
| 6 | +[](https://codecov.io/gh/bool64/prom-stats) |
| 7 | +[](https://pkg.go.dev/github.com/bool64/prom-stats) |
| 8 | +[](https://wakatime.com/badge/github/bool64/prom-stats) |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +* Context-driven labels control. |
| 15 | +* Zero allocation. |
| 16 | +* A simple interface with variadic number of key-value pairs for labels. |
| 17 | +* Easily mockable interface free from 3rd party dependencies. |
| 18 | + |
| 19 | +## Example |
| 20 | + |
| 21 | +```go |
| 22 | +// Bring your own Prometheus registry. |
| 23 | +registry := prometheus.NewRegistry() |
| 24 | +tr := prom.Tracker{ |
| 25 | + Registry: registry, |
| 26 | +} |
| 27 | + |
| 28 | +// Add custom Prometheus configuration where necessary. |
| 29 | +tr.DeclareHistogram("my_latency_seconds", prometheus.HistogramOpts{ |
| 30 | + Buckets: []float64{1e-4, 1e-3, 1e-2, 1e-1, 1, 10, 100}, |
| 31 | +}) |
| 32 | + |
| 33 | +ctx := context.Background() |
| 34 | + |
| 35 | +// Add labels to context. |
| 36 | +ctx = stats.AddKeysAndValues(ctx, "ctx-label", "ctx-value0") |
| 37 | + |
| 38 | +// Override label values. |
| 39 | +ctx = stats.AddKeysAndValues(ctx, "ctx-label", "ctx-value1") |
| 40 | + |
| 41 | +// Collect stats with last mile labels. |
| 42 | +tr.Add(ctx, "my_count", 1, |
| 43 | + "some-label", "some-value", |
| 44 | +) |
| 45 | + |
| 46 | +tr.Add(ctx, "my_latency_seconds", 1.23) |
| 47 | + |
| 48 | +tr.Set(ctx, "temperature", 33.3) |
| 49 | +``` |
| 50 | + |
| 51 | +## Performance |
| 52 | + |
| 53 | +Sample benchmark result with go1.16. |
| 54 | +``` |
| 55 | +goos: darwin |
| 56 | +goarch: amd64 |
| 57 | +pkg: github.com/bool64/stats/prom-stats |
| 58 | +cpu: Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz |
| 59 | +``` |
| 60 | +``` |
| 61 | +name time/op |
| 62 | +Tracker_Add-8 635ns ± 3% |
| 63 | +RawPrometheus-8 472ns ± 2% |
| 64 | +
|
| 65 | +name alloc/op |
| 66 | +Tracker_Add-8 0.00B |
| 67 | +RawPrometheus-8 336B ± 0% |
| 68 | +
|
| 69 | +name allocs/op |
| 70 | +Tracker_Add-8 0.00 |
| 71 | +RawPrometheus-8 2.00 ± 0% |
| 72 | +``` |
| 73 | + |
| 74 | +## Caveats |
| 75 | + |
| 76 | +Prometheus client does not support metrics with same name and different label sets. |
| 77 | +If you add a label to context, make sure you have it in all cases, at least with an empty value `""`. |
| 78 | + |
| 79 | +## Versioning |
| 80 | + |
| 81 | +This project adheres to [Semantic Versioning](https://semver.org/#semantic-versioning-200). |
| 82 | + |
| 83 | +Before version `1.0.0`, breaking changes are tagged with `MINOR` bump, features and fixes are tagged with `PATCH` bump. |
| 84 | +After version `1.0.0`, breaking changes are tagged with `MAJOR` bump. |
0 commit comments