Categorygithub.com/VictoriaMetrics/metrics
modulepackage
1.35.2
Repository: https://github.com/victoriametrics/metrics.git
Documentation: pkg.go.dev

# README

Build Status GoDoc Go Report codecov

metrics - lightweight package for exporting metrics in Prometheus format

Features

  • Lightweight. Has minimal number of third-party dependencies and all these deps are small. See this article for details.
  • Easy to use. See the API docs.
  • Fast.
  • Allows exporting distinct metric sets via distinct endpoints. See Set.
  • Supports easy-to-use histograms, which just work without any tuning. Read more about VictoriaMetrics histograms at this article.
  • Can push metrics to VictoriaMetrics or to any other remote storage, which accepts metrics in Prometheus text exposition format. See these docs.

Limitations

Usage

import "github.com/VictoriaMetrics/metrics"

// Register various metrics.
// Metric name may contain labels in Prometheus format - see below.
var (
	// Register counter without labels.
	requestsTotal = metrics.NewCounter("requests_total")

	// Register summary with a single label.
	requestDuration = metrics.NewSummary(`requests_duration_seconds{path="/foobar/baz"}`)

	// Register gauge with two labels.
	queueSize = metrics.NewGauge(`queue_size{queue="foobar",topic="baz"}`, func() float64 {
		return float64(foobarQueue.Len())
	})

	// Register histogram with a single label.
	responseSize = metrics.NewHistogram(`response_size{path="/foo/bar"}`)
)

// ...
func requestHandler() {
	// Increment requestTotal counter.
	requestsTotal.Inc()

	startTime := time.Now()
	processRequest()
	// Update requestDuration summary.
	requestDuration.UpdateDuration(startTime)

	// Update responseSize histogram.
	responseSize.Update(responseSize)
}

// Expose the registered metrics at `/metrics` path.
http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
	metrics.WritePrometheus(w, true)
})

// ... or push registered metrics every 10 seconds to http://victoria-metrics:8428/api/v1/import/prometheus
// with the added `instance="foobar"` label to all the pushed metrics.
metrics.InitPush("http://victoria-metrics:8428/api/v1/import/prometheus", 10*time.Second, `instance="foobar"`, true)

By default, exposed metrics do not have TYPE or HELP meta information. Call ExposeMetadata(true) in order to generate TYPE and HELP meta information per each metric.

See docs for more info.

Users

FAQ

Why the metrics API isn't compatible with github.com/prometheus/client_golang?

Because the github.com/prometheus/client_golang is too complex and is hard to use.

Why the metrics.WritePrometheus doesn't expose documentation for each metric?

Because this documentation is ignored by Prometheus. The documentation is for users. Just give meaningful names to the exported metrics or add comments in the source code or in other suitable place explaining each metric exposed from your application.

How to implement CounterVec in metrics?

Just use GetOrCreateCounter instead of CounterVec.With. See this example for details.

Why Histogram buckets contain vmrange labels instead of le labels like in Prometheus histograms?

Buckets with vmrange labels occupy less disk space compared to Promethes-style buckets with le labels, because vmrange buckets don't include counters for the previous ranges. VictoriaMetrics provides prometheus_buckets function, which converts vmrange buckets to Prometheus-style buckets with le labels. This is useful for building heatmaps in Grafana. Additionally, its' histogram_quantile function transparently handles histogram buckets with vmrange labels.

# Functions

ExposeMetadata allows enabling adding TYPE and HELP metadata to the exposed metrics globally.
GetDefaultSet returns the default metrics set.
GetOrCreateCounter returns registered counter with the given name or creates new counter if the registry doesn't contain counter with the given name.
GetOrCreateFloatCounter returns registered FloatCounter with the given name or creates new FloatCounter if the registry doesn't contain FloatCounter with the given name.
GetOrCreateGauge returns registered gauge with the given name or creates new gauge if the registry doesn't contain gauge with the given name.
GetOrCreateHistogram returns registered histogram with the given name or creates new histogram if the registry doesn't contain histogram with the given name.
GetOrCreateSummary returns registered summary with the given name or creates new summary if the registry doesn't contain summary with the given name.
GetOrCreateSummaryExt returns registered summary with the given name, window and quantiles or creates new summary if the registry doesn't contain summary with the given name.
InitPush sets up periodic push for globally registered metrics to the given pushURL with the given interval.
InitPushExt sets up periodic push for metrics obtained by calling writeMetrics with the given interval.
InitPushExtWithOptions sets up periodic push for metrics obtained by calling writeMetrics with the given interval.
InitPushProcessMetrics sets up periodic push for 'process_*' metrics to the given pushURL with the given interval.
InitPushWithOptions sets up periodic push for globally registered metrics to the given pushURL with the given interval.
ListMetricNames returns sorted list of all the metric names from default set.
NewCounter registers and returns new counter with the given name.
NewFloatCounter registers and returns new counter of float64 type with the given name.
NewGauge registers and returns gauge with the given name, which calls f to obtain gauge value.
NewHistogram creates and returns new histogram with the given name.
NewSet creates new set of metrics.
NewSummary creates and returns new summary with the given name.
NewSummaryExt creates and returns new summary with the given name, window and quantiles.
PushMetrics pushes globally registered metrics to pushURL.
PushMetricsExt pushes metrics generated by wirteMetrics to pushURL.
RegisterMetricsWriter registers writeMetrics callback for including metrics in the output generated by WritePrometheus.
RegisterSet registers the given set s for metrics export via global WritePrometheus() call.
UnregisterAllMetrics unregisters all the metrics from default set.
UnregisterMetric removes metric with the given name from default set.
UnregisterSet stops exporting metrics for the given s via global WritePrometheus() call.
WriteCounterFloat64 writes counter metric with the given name and value to w in Prometheus text exposition format.
WriteCounterUint64 writes counter metric with the given name and value to w in Prometheus text exposition format.
WriteFDMetrics writes `process_max_fds` and `process_open_fds` metrics to w.
WriteGaugeFloat64 writes gauge metric with the given name and value to w in Prometheus text exposition format.
WriteGaugeUint64 writes gauge metric with the given name and value to w in Prometheus text exposition format.
WriteMetadataIfNeeded writes HELP and TYPE metadata for the given metricName and metricType if this is globally enabled via ExposeMetadata().
WriteProcessMetrics writes additional process metrics in Prometheus format to w.
WritePrometheus writes all the metrics in Prometheus format from the default set, all the added sets and metrics writers to w.

# Structs

Counter is a counter.
FloatCounter is a float64 counter guarded by RWmutex.
Gauge is a float64 gauge.
Histogram is a histogram for non-negative values with automatically created buckets.
PushOptions is the list of options, which may be applied to InitPushWithOptions().
Set is a set of metrics.
Summary implements summary.