2019-04-08 16:29:16 +03:00
|
|
|
// Package metrics implements Prometheus-compatible metrics for applications.
|
|
|
|
//
|
2019-04-08 20:51:06 +03:00
|
|
|
// 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.
|
2019-04-21 02:56:28 +03:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2019-06-01 23:18:41 +03:00
|
|
|
var defaultSet = NewSet()
|
|
|
|
|
2019-04-08 16:29:16 +03:00
|
|
|
// WritePrometheus writes all the registered metrics in Prometheus format to w.
|
|
|
|
//
|
2019-07-12 17:18:52 +03:00
|
|
|
// If exposeProcessMetrics is true, then various `go_*` and `process_*` metrics
|
|
|
|
// are exposed for the current process.
|
2019-04-08 20:51:06 +03:00
|
|
|
//
|
|
|
|
// 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) {
|
2019-06-01 23:18:41 +03:00
|
|
|
defaultSet.WritePrometheus(w)
|
2019-04-11 12:59:53 +03:00
|
|
|
if exposeProcessMetrics {
|
2020-02-26 20:52:38 +03:00
|
|
|
WriteProcessMetrics(w)
|
2019-04-08 16:29:16 +03:00
|
|
|
}
|
2019-04-11 12:59:53 +03:00
|
|
|
}
|
2020-02-26 20:52:38 +03:00
|
|
|
|
|
|
|
// WriteProcessMetrics writes additional process metrics in Prometheus format to w.
|
|
|
|
//
|
2021-03-18 19:49:19 +03:00
|
|
|
// The following `go_*` and `process_*` metrics are exposed for the currently
|
|
|
|
// running process. Below is a short description for the exposed `process_*` metrics:
|
|
|
|
//
|
|
|
|
// - process_cpu_seconds_system_total - CPU time spent in syscalls
|
|
|
|
// - process_cpu_seconds_user_total - CPU time spent in userspace
|
|
|
|
// - process_cpu_seconds_total - CPU time spent by the process
|
|
|
|
// - process_major_pagefaults_total - page faults resulted in disk IO
|
|
|
|
// - process_minor_pagefaults_total - page faults resolved without disk IO
|
|
|
|
// - process_resident_memory_bytes - recently accessed memory (aka RSS or resident memory)
|
|
|
|
// - process_resident_memory_peak_bytes - the maximum RSS memory usage
|
|
|
|
// - process_resident_memory_anon_bytes - RSS for memory-mapped files
|
|
|
|
// - process_resident_memory_file_bytes - RSS for memory allocated by the process
|
|
|
|
// - process_resident_memory_shared_bytes - RSS for memory shared between multiple processes
|
|
|
|
// - process_virtual_memory_bytes - virtual memory usage
|
|
|
|
// - process_virtual_memory_peak_bytes - the maximum virtual memory usage
|
|
|
|
// - process_num_threads - the number of threads
|
|
|
|
// - process_start_time_seconds - process start time as unix timestamp
|
|
|
|
//
|
|
|
|
// - process_io_read_bytes_total - the number of bytes read via syscalls
|
|
|
|
// - process_io_written_bytes_total - the number of bytes written via syscalls
|
|
|
|
// - process_io_read_syscalls_total - the number of read syscalls
|
|
|
|
// - process_io_write_syscalls_total - the number of write syscalls
|
|
|
|
// - process_io_storage_read_bytes_total - the number of bytes actually read from disk
|
|
|
|
// - process_io_storage_written_bytes_total - the number of bytes actually written to disk
|
2020-02-26 20:52:38 +03:00
|
|
|
//
|
2021-03-22 15:36:33 +03:00
|
|
|
// - go_memstats_alloc_bytes - memory usage for Go objects in the heap
|
|
|
|
// - go_memstats_alloc_bytes_total - the cumulative counter for total size of allocated Go objects
|
|
|
|
// - go_memstats_frees_total - the cumulative counter for number of freed Go objects
|
|
|
|
// - go_memstats_gc_cpu_fraction - the fraction of CPU spent in Go garbage collector
|
|
|
|
// - go_memstats_gc_sys_bytes - the size of Go garbage collector metadata
|
|
|
|
// - go_memstats_heap_alloc_bytes - the same as go_memstats_alloc_bytes
|
|
|
|
// - go_memstats_heap_idle_bytes - idle memory ready for new Go object allocations
|
|
|
|
// - go_memstats_heap_objects - the number of Go objects in the heap
|
|
|
|
// - go_memstats_heap_sys_bytes - memory requested for Go objects from the OS
|
|
|
|
// - go_memstats_mallocs_total - the number of allocations for Go objects
|
|
|
|
// - go_memstats_next_gc_bytes - the target heap size when the next garbage collection should start
|
|
|
|
// - go_memstats_stack_inuse_bytes - memory used for goroutine stacks
|
|
|
|
// - go_memstats_stack_sys_bytes - memory requested fromthe OS for goroutine stacks
|
|
|
|
// - go_memstats_sys_bytes - memory requested by Go runtime from the OS
|
|
|
|
//
|
2020-02-26 20:52:38 +03:00
|
|
|
// The WriteProcessMetrics func is usually called in combination with writing Set metrics
|
|
|
|
// inside "/metrics" handler:
|
|
|
|
//
|
|
|
|
// http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
// mySet.WritePrometheus(w)
|
|
|
|
// metrics.WriteProcessMetrics(w)
|
|
|
|
// })
|
|
|
|
//
|
2021-02-04 17:28:55 +03:00
|
|
|
// See also WrteFDMetrics.
|
2020-02-26 20:52:38 +03:00
|
|
|
func WriteProcessMetrics(w io.Writer) {
|
|
|
|
writeGoMetrics(w)
|
|
|
|
writeProcessMetrics(w)
|
|
|
|
}
|
2020-07-27 22:05:14 +03:00
|
|
|
|
2021-02-04 17:28:55 +03:00
|
|
|
// WriteFDMetrics writes `process_max_fds` and `process_open_fds` metrics to w.
|
|
|
|
func WriteFDMetrics(w io.Writer) {
|
|
|
|
writeFDMetrics(w)
|
|
|
|
}
|
|
|
|
|
2020-07-27 22:05:14 +03:00
|
|
|
// UnregisterMetric removes metric with the given name from default set.
|
|
|
|
func UnregisterMetric(name string) bool {
|
|
|
|
return defaultSet.UnregisterMetric(name)
|
|
|
|
}
|
2022-07-21 18:14:01 +03:00
|
|
|
|
|
|
|
// ListMetricNames returns a list of all the metric names from default set.
|
|
|
|
func ListMetricNames() []string {
|
|
|
|
return defaultSet.ListMetricNames()
|
|
|
|
}
|