metrics

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2021 License: MIT Imports: 11 Imported by: 1

Documentation

Overview

Package metrics provides utilities for running and testing a prometheus scrape server.

Server provides an HTTP server with an /metrics endpoint configured. Running it is as simple as:

func main() {
	...
	initialize your application
	...
	server := metrics.NewServer(8080)
	server.Run()
}

MetricName, MetricLabel and MetricValue provide an easy way to validate metrics during unit testing:

func TestMetricTools(t *testing.T) {
	...
	ch := make(chan prometheus.Metric)
	go yourCollector.Collect(ch)

	m := <-ch
	assert.Equal(t, "foo_bar_gauge", metrics.MetricName(m))
	assert.Equal(t, 1.0, metrics.MetricValue(m).GetGauge().GetValue())
	assert.Equal(t, "valueA", metrics.MetricLabel(m, "labelA"))
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetRouter added in v0.4.4

func GetRouter() (router *mux.Router)

GetRouter returns an HTTP router with a prometheus metrics endpoint. Use this if you do not want to use Server.Run(), but instead prefer to incorporate the metrics endpoint into your application's existing HTTP server:

r := metrics.GetRouter()
r.Path("/some-endpoint").Handler(someHandler)
server := http.Server{
	Addr: ":8080",

func MetricLabel added in v0.3.91

func MetricLabel(metric prometheus.Metric, labelName string) (value string)

MetricLabel returns the value of a metric's label. Panics if metric is not a valid Prometheus metric.

func MetricName added in v0.3.91

func MetricName(metric prometheus.Metric) (name string)

MetricName returns the name of the provided Prometheus metric.

func MetricValue added in v0.3.91

func MetricValue(metric prometheus.Metric) *pcg.Metric

MetricValue converts a Prometheus metric to a prometheus/client_model/go metric, so the caller can read its value:

ch := make(chan prometheus.Metric)
go c.Collect(ch)

m := <-ch
metric := metrics.MetricValue(m)
assert.Equal(t, 1.0, metric.GetGauge().GetValue())

Panics if metric is not a valid Prometheus metric.

Types

type Handler added in v0.4.3

type Handler struct {
	// Path of the endpoint (e.g. "/health"). Must include the leading /
	Path string
	// Handler that implements the endpoint
	Handler http.Handler
	// Methods that the handler should support. If empty, http.MethodGet is the default
	Methods []string
}

Handler contains an endpoint to be registered in the Server's HTTP server, using NewServerWithHandlers.

type Server added in v0.4.1

type Server struct {
	// the Port that the HTTP server listens on
	Port int
	// contains filtered or unexported fields
}

Server runs an HTTP Server for a Prometheus scrape (i.e. /metric) endpoint. It includes a "http_duration_seconds" Counter metric that measures the time of each HTTP server request.

func NewServer added in v0.4.1

func NewServer(port int) (server *Server)

NewServer creates a new Server, which will listen on the specified TCP port. If Port is zero, Server will listen on a randomly chosen free port. The selected can be found in Server's Port field.

func NewServerWithHandlers added in v0.4.3

func NewServerWithHandlers(port int, handlers []Handler) *Server

NewServerWithHandlers creates a new Server with additional handlers. If Port is zero, Server will listen on a randomly chosen free port. The selected can be found in Server's Port field.

func (*Server) Run added in v0.4.1

func (server *Server) Run() (err error)

Run starts the HTTP Server. This calls server's http.Server's Serve method and returns that method's return value.

func (*Server) Shutdown added in v0.4.1

func (server *Server) Shutdown(timeout time.Duration) (err error)

Shutdown performs a graceful shutdown of the HTTP Server.

Jump to

Keyboard shortcuts

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