prometheus

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2024 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package prometheus exposes go-metrics into a Prometheus format.

Index

Constants

View Source
const UsePrometheusClient = false

Variables

View Source
var (
	DefaultRegistry    = NewRegistry()
	EphemeralRegistry  = NewRegistry()
	AccountingRegistry = NewRegistry() // registry used in swarm
)
View Source
var EnabledExpensive = false

Functions

func Get

func Get(name string) interface{}

Get the metric by the given name or nil if none is registered.

func GetOrCreateGaugeFunc

func GetOrCreateGaugeFunc(s string, f func() float64) prometheus.GaugeFunc

func GetOrCreateHistogram

func GetOrCreateHistogram(s string) prometheus.Histogram

func Handler

func Handler(reg Registry) http.Handler

Handler returns an HTTP handler which dump metrics in Prometheus format. Output format can be cheched here: https://o11y.tools/metricslint/

func NewCounter

func NewCounter(name string, help ...string) (prometheus.Counter, error)

func NewGauge

func NewGauge(name string, help ...string) (prometheus.Gauge, error)

func NewGaugeFunc

func NewGaugeFunc(name string, f func() float64, help ...string) (prometheus.GaugeFunc, error)

func NewHistogram

func NewHistogram(name string, help ...string) (prometheus.Histogram, error)

func NewSummary

func NewSummary(name string, window time.Duration, quantiles map[float64]float64, help ...string) (prometheus.Summary, error)

NewSummary creates and returns new summary in s with the given name, window and quantiles.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned summary is safe to use from concurrent goroutines.

func Setup

func Setup(address string, log log.Logger) *http.ServeMux

Setup starts a dedicated metrics server at the given address. This function enables metrics reporting separate from pprof.

Types

type Counter

type Counter interface {
	Inc()
	Dec()
	Add(n int)
	Set(n uint64)
	Get() uint64
}

func GetOrCreateCounter

func GetOrCreateCounter(s string, isGauge ...bool) Counter

type DuplicateMetric

type DuplicateMetric string

DuplicateMetric is the error returned by Registry.Register when a metric already exists. If you mean to Register that metric you must first Unregister the existing metric.

func (DuplicateMetric) Error

func (err DuplicateMetric) Error() string

type Registry

type Registry interface {

	// Call the given function for each registered metric.
	Each(func(string, interface{}))

	// Get the metric by the given name or nil if none is registered.
	Get(string) interface{}

	// Gets an existing metric or registers the given one.
	// The interface can be the metric to register if not found in registry,
	// or a function returning the metric for lazy instantiation.
	GetOrRegister(string, interface{}) interface{}

	// Register the given metric under the given name.
	Register(string, interface{}) error

	// Unregister the metric with the given name.
	Unregister(string)

	// Unregister all metrics.  (Mostly for testing.)
	UnregisterAll()
}

A Registry holds references to a set of metrics by name and can iterate over them, calling callback functions provided by the user.

This is an interface so as to encourage other structs to implement the Registry API as appropriate.

func NewRegistry

func NewRegistry() Registry

Create a new registry.

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set is a set of metrics.

Metrics belonging to a set are exported separately from global metrics.

Set.WritePrometheus must be called for exporting metrics from the set.

func NewSet

func NewSet() *Set

NewSet creates new set of metrics.

Pass the set to RegisterSet() function in order to export its metrics via global WritePrometheus() call.

func (*Set) Collect

func (s *Set) Collect(ch chan<- prometheus.Metric)

func (*Set) Describe

func (s *Set) Describe(ch chan<- *prometheus.Desc)

func (*Set) GetOrCreateCounter

func (s *Set) GetOrCreateCounter(name string, help ...string) prometheus.Counter

GetOrCreateCounter returns registered counter in s with the given name or creates new counter if s doesn't contain counter with the given name.

name must be valid Prometheus-compatible metric with possible labels. 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 (*Set) GetOrCreateGauge

func (s *Set) GetOrCreateGauge(name string, help ...string) prometheus.Gauge

GetOrCreateGaugeFunc returns registered gauge with the given name in s or creates new gauge if s doesn't contain gauge with the given name.

name must be valid Prometheus-compatible metric with possible labels. 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 (*Set) GetOrCreateGaugeFunc

func (s *Set) GetOrCreateGaugeFunc(name string, f func() float64, help ...string) prometheus.GaugeFunc

GetOrCreateGaugeFunc returns registered gauge with the given name in s or creates new gauge if s doesn't contain gauge with the given name.

name must be valid Prometheus-compatible metric with possible labels. 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 (*Set) GetOrCreateHistogram

func (s *Set) GetOrCreateHistogram(name string, help ...string) prometheus.Histogram

GetOrCreateHistogram returns registered histogram in s with the given name or creates new histogram if s doesn't contain histogram with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned histogram is safe to use from concurrent goroutines.

Performance tip: prefer NewHistogram instead of GetOrCreateHistogram.

func (*Set) GetOrCreateSummary

func (s *Set) GetOrCreateSummary(name string, help ...string) prometheus.Summary

GetOrCreateSummary returns registered summary with the given name in s or creates new summary if s doesn't contain summary with the given name.

name must be valid Prometheus-compatible metric with possible labels. 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 (*Set) GetOrCreateSummaryExt

func (s *Set) GetOrCreateSummaryExt(name string, window time.Duration, quantiles map[float64]float64, help ...string) prometheus.Summary

GetOrCreateSummaryExt returns registered summary with the given name, window and quantiles in s or creates new summary if s doesn't contain summary with the given name.

name must be valid Prometheus-compatible metric with possible labels. 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 (*Set) ListMetricNames

func (s *Set) ListMetricNames() []string

ListMetricNames returns sorted list of all the metrics in s.

func (*Set) NewCounter

func (s *Set) NewCounter(name string, help ...string) (prometheus.Counter, error)

NewCounter registers and returns new counter with the given name in the s.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned counter is safe to use from concurrent goroutines.

func (*Set) NewGauge

func (s *Set) NewGauge(name string, help ...string) (prometheus.Gauge, error)

NewGauge registers and returns gauge with the given name in s, 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.

func (*Set) NewGaugeFunc

func (s *Set) NewGaugeFunc(name string, f func() float64, help ...string) (prometheus.GaugeFunc, error)

NewGaugeFunc registers and returns gauge with the given name in s, 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.

func (*Set) NewHistogram

func (s *Set) NewHistogram(name string, help ...string) (prometheus.Histogram, error)

NewHistogram creates and returns new histogram in s with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned histogram is safe to use from concurrent goroutines.

func (*Set) NewSummary

func (s *Set) NewSummary(name string, help ...string) (prometheus.Summary, error)

NewSummary creates and returns new summary with the given name in s.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned summary is safe to use from concurrent goroutines.

func (*Set) UnregisterAllMetrics

func (s *Set) UnregisterAllMetrics()

UnregisterAllMetrics de-registers all metrics registered in s.

func (*Set) UnregisterMetric

func (s *Set) UnregisterMetric(name string) bool

UnregisterMetric removes metric with the given name from s.

True is returned if the metric has been removed. False is returned if the given metric is missing in s.

type StandardRegistry

type StandardRegistry struct {
	// contains filtered or unexported fields
}

The standard implementation of a Registry is a mutex-protected map of names to metrics.

func (*StandardRegistry) Each

func (r *StandardRegistry) Each(f func(string, interface{}))

Call the given function for each registered metric.

func (*StandardRegistry) Get

func (r *StandardRegistry) Get(name string) interface{}

Get the metric by the given name or nil if none is registered.

func (*StandardRegistry) GetOrRegister

func (r *StandardRegistry) GetOrRegister(name string, i interface{}) interface{}

Gets an existing metric or creates and registers a new one. Threadsafe alternative to calling Get and Register on failure. The interface can be the metric to register if not found in registry, or a function returning the metric for lazy instantiation.

func (*StandardRegistry) Register

func (r *StandardRegistry) Register(name string, i interface{}) error

Register the given metric under the given name. Returns a DuplicateMetric if a metric by the given name is already registered.

func (*StandardRegistry) Unregister

func (r *StandardRegistry) Unregister(name string)

Unregister the metric with the given name.

func (*StandardRegistry) UnregisterAll

func (r *StandardRegistry) UnregisterAll()

Unregister all metrics. (Mostly for testing.)

type Stoppable

type Stoppable interface {
	Stop()
}

Stoppable defines the metrics which has to be stopped.

type Summary

type Summary interface {
	UpdateDuration(time.Time)
}

func GetOrCreateSummary

func GetOrCreateSummary(s string) Summary

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL