| 1 | [](https://github.com/VictoriaMetrics/metrics/actions) |
| 2 | [](http://godoc.org/github.com/VictoriaMetrics/metrics) |
| 3 | [](https://goreportcard.com/report/github.com/VictoriaMetrics/metrics) |
| 4 | [](https://codecov.io/gh/VictoriaMetrics/metrics) |
| 5 | |
| 6 | |
| 7 | # metrics - lightweight package for exporting metrics in Prometheus format |
| 8 | |
| 9 | |
| 10 | ### Features |
| 11 | |
| 12 | * Lightweight. Has minimal number of third-party dependencies and all these deps are small. |
| 13 | See [this article](https://medium.com/@valyala/stripping-dependency-bloat-in-victoriametrics-docker-image-983fb5912b0d) for details. |
| 14 | * Easy to use. See the [API docs](http://godoc.org/github.com/VictoriaMetrics/metrics). |
| 15 | * Fast. |
| 16 | * Allows exporting distinct metric sets via distinct endpoints. See [Set](http://godoc.org/github.com/VictoriaMetrics/metrics#Set). |
| 17 | * Supports [easy-to-use histograms](http://godoc.org/github.com/VictoriaMetrics/metrics#Histogram), which just work without any tuning. |
| 18 | Read more about VictoriaMetrics histograms at [this article](https://medium.com/@valyala/improving-histogram-usability-for-prometheus-and-grafana-bc7e5df0e350). |
| 19 | * Can push metrics to VictoriaMetrics or to any other remote storage, which accepts metrics |
| 20 | in [Prometheus text exposition format](https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md#text-based-format). |
| 21 | See [these docs](http://godoc.org/github.com/VictoriaMetrics/metrics#InitPush). |
| 22 | |
| 23 | |
| 24 | ### Limitations |
| 25 | |
| 26 | * It doesn't implement advanced functionality from [github.com/prometheus/client_golang](https://godoc.org/github.com/prometheus/client_golang). |
| 27 | |
| 28 | |
| 29 | ### Usage |
| 30 | |
| 31 | ```go |
| 32 | import "github.com/VictoriaMetrics/metrics" |
| 33 | |
| 34 | // Register various metrics. |
| 35 | // Metric name may contain labels in Prometheus format - see below. |
| 36 | var ( |
| 37 | // Register counter without labels. |
| 38 | requestsTotal = metrics.NewCounter("requests_total") |
| 39 | |
| 40 | // Register summary with a single label. |
| 41 | requestDuration = metrics.NewSummary(`requests_duration_seconds{path="/foobar/baz"}`) |
| 42 | |
| 43 | // Register gauge with two labels. |
| 44 | queueSize = metrics.NewGauge(`queue_size{queue="foobar",topic="baz"}`, func() float64 { |
| 45 | return float64(foobarQueue.Len()) |
| 46 | }) |
| 47 | |
| 48 | // Register histogram with a single label. |
| 49 | responseSize = metrics.NewHistogram(`response_size{path="/foo/bar"}`) |
| 50 | ) |
| 51 | |
| 52 | // ... |
| 53 | func requestHandler() { |
| 54 | // Increment requestTotal counter. |
| 55 | requestsTotal.Inc() |
| 56 | |
| 57 | startTime := time.Now() |
| 58 | processRequest() |
| 59 | // Update requestDuration summary. |
| 60 | requestDuration.UpdateDuration(startTime) |
| 61 | |
| 62 | // Update responseSize histogram. |
| 63 | responseSize.Update(responseSize) |
| 64 | } |
| 65 | |
| 66 | // Expose the registered metrics at `/metrics` path. |
| 67 | http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) { |
| 68 | metrics.WritePrometheus(w, true) |
| 69 | }) |
| 70 | |
| 71 | // ... or push registered metrics every 10 seconds to http://victoria-metrics:8428/api/v1/import/prometheus |
| 72 | // with the added `instance="foobar"` label to all the pushed metrics. |
| 73 | metrics.InitPush("http://victoria-metrics:8428/api/v1/import/prometheus", 10*time.Second, `instance="foobar"`, true) |
| 74 | ``` |
| 75 | |
| 76 | By default, exposed metrics [do not have](https://github.com/VictoriaMetrics/metrics/issues/48#issuecomment-1620765811) |
| 77 | `TYPE` or `HELP` meta information. Call [`ExposeMetadata(true)`](https://pkg.go.dev/github.com/VictoriaMetrics/metrics#ExposeMetadata) |
| 78 | in order to generate `TYPE` and `HELP` meta information per each metric. |
| 79 | |
| 80 | See [docs](https://pkg.go.dev/github.com/VictoriaMetrics/metrics) for more info. |
| 81 | |
| 82 | ### Users |
| 83 | |
| 84 | * `Metrics` has been extracted from [VictoriaMetrics](https://github.com/VictoriaMetrics/VictoriaMetrics) sources. |
| 85 | See [this article](https://medium.com/devopslinks/victoriametrics-creating-the-best-remote-storage-for-prometheus-5d92d66787ac) |
| 86 | for more info about `VictoriaMetrics`. |
| 87 | |
| 88 | |
| 89 | ### FAQ |
| 90 | |
| 91 | #### Why the `metrics` API isn't compatible with `github.com/prometheus/client_golang`? |
| 92 | |
| 93 | Because the `github.com/prometheus/client_golang` is too complex and is hard to use. |
| 94 | |
| 95 | |
| 96 | #### Why the `metrics.WritePrometheus` doesn't expose documentation for each metric? |
| 97 | |
| 98 | Because this documentation is ignored by Prometheus. The documentation is for users. |
| 99 | Just give [meaningful names to the exported metrics](https://prometheus.io/docs/practices/naming/#metric-names) |
| 100 | or add comments in the source code or in other suitable place explaining each metric exposed from your application. |
| 101 | |
| 102 | |
| 103 | #### How to implement [CounterVec](https://godoc.org/github.com/prometheus/client_golang/prometheus#CounterVec) in `metrics`? |
| 104 | |
| 105 | Just use [GetOrCreateCounter](http://godoc.org/github.com/VictoriaMetrics/metrics#GetOrCreateCounter) |
| 106 | instead of `CounterVec.With`. See [this example](https://pkg.go.dev/github.com/VictoriaMetrics/metrics#example-Counter-Vec) for details. |
| 107 | |
| 108 | |
| 109 | #### Why [Histogram](http://godoc.org/github.com/VictoriaMetrics/metrics#Histogram) buckets contain `vmrange` labels instead of `le` labels like in Prometheus histograms? |
| 110 | |
| 111 | Buckets with `vmrange` labels occupy less disk space compared to Prometheus-style buckets with `le` labels, |
| 112 | because `vmrange` buckets don't include counters for the previous ranges. [VictoriaMetrics](https://github.com/VictoriaMetrics/VictoriaMetrics) provides `prometheus_buckets` |
| 113 | function, which converts `vmrange` buckets to Prometheus-style buckets with `le` labels. This is useful for building heatmaps in Grafana. |
| 114 | Additionally, its `histogram_quantile` function transparently handles histogram buckets with `vmrange` labels. |
| 115 | |
| 116 | However, for compatibility purposes package provides classic [Prometheus Histograms](http://godoc.org/github.com/VictoriaMetrics/metrics#PrometheusHistogram) with `le` labels. |