Documentation
¶
Overview ¶
Package metrics implements Prometheus-compatible metrics for applications.
This package is lightweight alternative to https://github.com/prometheus/client_golang with simpler API and smaller dependencies.
Usage:
- Register the required metrics via New* functions.
- Expose them to `/metrics` page via WritePrometheus.
- Update the registered metrics during application lifetime.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WritePrometheus ¶
WritePrometheus writes all the registered metrics in Prometheus format to w.
If exposeProcessMetrics is true, then various `go_*` 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)
})
Example ¶
package main
import (
"net/http"
"github.com/VictoriaMetrics/metrics"
)
func main() {
// Export all the registered metrics in Prometheus format at `/metrics` http path.
http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
metrics.WritePrometheus(w, true)
})
}
Types ¶
type Counter ¶
type Counter struct {
// contains filtered or unexported fields
}
Counter is a counter.
It may be used as a gauge if Dec and Set are called.
Example ¶
package main
import (
"fmt"
"github.com/VictoriaMetrics/metrics"
)
func main() {
// Define a counter in global scope.
var c = metrics.NewCounter(`metric_total{label1="value1", label2="value2"}`)
// Increment the counter when needed.
for i := 0; i < 10; i++ {
c.Inc()
}
n := c.Get()
fmt.Println(n)
}
Output: 10
Example (Vec) ¶
package main
import (
"fmt"
"github.com/VictoriaMetrics/metrics"
)
func main() {
for i := 0; i < 3; i++ {
// Dynamically construct metric name and pass it to GetOrCreateCounter.
name := fmt.Sprintf(`metric_total{label1=%q, label2="%d"}`, "value1", i)
metrics.GetOrCreateCounter(name).Add(i + 1)
}
// Read counter values.
for i := 0; i < 3; i++ {
name := fmt.Sprintf(`metric_total{label1=%q, label2="%d"}`, "value1", i)
n := metrics.GetOrCreateCounter(name).Get()
fmt.Println(n)
}
}
Output: 1 2 3
func GetOrCreateCounter ¶ added in v1.2.0
GetOrCreateCounter returns registered counter with the given name or creates new counter if the registry doesn't contain counter with the given name.
name must be valid Prometheus-compatible metric with possible lables. For instance,
- foo
- foo{bar="baz"}
- foo{bar="baz",aaa="b"}
The returned counter is safe to use from concurrent goroutines.
Performance tip: prefer NewCounter instead of GetOrCreateCounter.
func NewCounter ¶
NewCounter registers and returns new counter with the given name.
name must be valid Prometheus-compatible metric with possible lables. For instance,
- foo
- foo{bar="baz"}
- foo{bar="baz",aaa="b"}
The returned counter is safe to use from concurrent goroutines.
type Gauge ¶ added in v1.1.0
type Gauge struct {
// contains filtered or unexported fields
}
Gauge is a float64 gauge.
See also Counter, which could be used as a gauge with Set and Dec calls.
Example ¶
package main
import (
"fmt"
"runtime"
"github.com/VictoriaMetrics/metrics"
)
func main() {
// Define a gauge exporting the number of goroutines.
var g = metrics.NewGauge(`goroutines_count`, func() float64 {
return float64(runtime.NumGoroutine())
})
// Obtain gauge value.
fmt.Println(g.Get())
}
Example (Vec) ¶
package main
import (
"fmt"
"github.com/VictoriaMetrics/metrics"
)
func main() {
for i := 0; i < 3; i++ {
// Dynamically construct metric name and pass it to GetOrCreateGauge.
name := fmt.Sprintf(`metric{label1=%q, label2="%d"}`, "value1", i)
iLocal := i
metrics.GetOrCreateGauge(name, func() float64 {
return float64(iLocal + 1)
})
}
// Read counter values.
for i := 0; i < 3; i++ {
name := fmt.Sprintf(`metric{label1=%q, label2="%d"}`, "value1", i)
n := metrics.GetOrCreateGauge(name, func() float64 { return 0 }).Get()
fmt.Println(n)
}
}
Output: 1 2 3
func GetOrCreateGauge ¶ added in v1.4.0
GetOrCreateGauge returns registered gauge with the given name or creates new gauge if the registry doesn't contain gauge with the given name.
name must be valid Prometheus-compatible metric with possible lables. For instance,
- foo
- foo{bar="baz"}
- foo{bar="baz",aaa="b"}
The returned gauge is safe to use from concurrent goroutines.
Performance tip: prefer NewGauge instead of GetOrCreateGauge.
func NewGauge ¶
NewGauge registers and returns gauge with the given name, which calls f to obtain gauge value.
name must be valid Prometheus-compatible metric with possible labels. For instance,
- foo
- foo{bar="baz"}
- foo{bar="baz",aaa="b"}
f must be safe for concurrent calls.
The returned gauge is safe to use from concurrent goroutines.
type Summary ¶
type Summary struct {
// contains filtered or unexported fields
}
Summary implements summary.
Example ¶
package main
import (
"time"
"github.com/VictoriaMetrics/metrics"
)
func main() {
// Define a summary in global scope.
var s = metrics.NewSummary(`request_duration_seconds{path="/foo/bar"}`)
// Update the summary with the duration of processRequest call.
startTime := time.Now()
processRequest()
s.UpdateDuration(startTime)
}
func processRequest() string {
return "foobar"
}
Example (Vec) ¶
package main
import (
"fmt"
"github.com/VictoriaMetrics/metrics"
)
func main() {
for i := 0; i < 3; i++ {
// Dynamically construct metric name and pass it to GetOrCreateSummary.
name := fmt.Sprintf(`response_size_bytes{path=%q}`, "/foo/bar")
response := processRequest()
metrics.GetOrCreateSummary(name).Update(float64(len(response)))
}
}
func processRequest() string {
return "foobar"
}
func GetOrCreateSummary ¶ added in v1.3.0
GetOrCreateSummary returns registered summary with the given name or creates new summary if the registry doesn't contain summary with the given name.
name must be valid Prometheus-compatible metric with possible lables. For instance,
- foo
- foo{bar="baz"}
- foo{bar="baz",aaa="b"}
The returned summary is safe to use from concurrent goroutines.
Performance tip: prefer NewSummary instead of GetOrCreateSummary.
func GetOrCreateSummaryExt ¶ added in v1.3.0
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.
name must be valid Prometheus-compatible metric with possible lables. For instance,
- foo
- foo{bar="baz"}
- foo{bar="baz",aaa="b"}
The returned summary is safe to use from concurrent goroutines.
Performance tip: prefer NewSummaryExt instead of GetOrCreateSummaryExt.
func NewSummary ¶
NewSummary creates and returns new summary with the given name.
name must be valid Prometheus-compatible metric with possible lables. For instance,
- foo
- foo{bar="baz"}
- foo{bar="baz",aaa="b"}
The returned summary is safe to use from concurrent goroutines.
func NewSummaryExt ¶
NewSummaryExt creates and returns new summary with the given name, window and quantiles.
name must be valid Prometheus-compatible metric with possible lables. For instance,
- foo
- foo{bar="baz"}
- foo{bar="baz",aaa="b"}
The returned summary is safe to use from concurrent goroutines.
func (*Summary) UpdateDuration ¶
UpdateDuration updates request duration based on the given startTime.