metrics/metrics.go

48 lines
1.2 KiB
Go
Raw Normal View History

2019-04-08 16:29:16 +03:00
// Package metrics implements Prometheus-compatible metrics for applications.
//
// This package is lightweight alternative to https://github.com/prometheus/client_golang
2019-04-11 14:04:01 +03:00
// with simpler API and smaller dependencies.
2019-04-08 21:15:17 +03:00
//
// Usage:
//
// 1. Register the required metrics via New* functions.
// 2. Expose them to `/metrics` page via WritePrometheus.
// 3. Update the registered metrics during application lifetime.
//
// The package has been extracted from https://victoriametrics.com/
2019-04-08 16:29:16 +03:00
package metrics
import (
"io"
)
type namedMetric struct {
name string
metric metric
}
type metric interface {
marshalTo(prefix string, w io.Writer)
}
var defaultSet = NewSet()
2019-04-08 16:29:16 +03:00
// WritePrometheus writes all the registered metrics in Prometheus format to w.
//
// If exposeProcessMetrics is true, then various `go_*` and `process_*` metrics
// are exposed for the current process.
//
// The WritePrometheus func is usually called inside "/metrics" handler:
//
// http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
// metrics.WritePrometheus(w, true)
// })
//
2019-04-08 16:29:16 +03:00
func WritePrometheus(w io.Writer, exposeProcessMetrics bool) {
defaultSet.WritePrometheus(w)
2019-04-11 12:59:53 +03:00
if exposeProcessMetrics {
writeGoMetrics(w)
2019-04-11 12:59:53 +03:00
writeProcessMetrics(w)
2019-04-08 16:29:16 +03:00
}
2019-04-11 12:59:53 +03:00
}