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
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 APIClientMetrics ¶ added in v0.6.0
type APIClientMetrics struct { Latency *prometheus.SummaryVec // measures latency of an API call Errors *prometheus.CounterVec // measures any errors returned by an API call }
APIClientMetrics contains Prometheus metrics to capture during API calls
func (APIClientMetrics) MakeLatencyTimer ¶ added in v0.6.0
func (pm APIClientMetrics) MakeLatencyTimer(labelValues ...string) (timer *prometheus.Timer)
MakeLatencyTimer creates a prometheus.Timer to measure the duration (latency) of an API client call If no Latency metric was created, timer will be nil:
timer := metric.MakeLatencyTimer(server, endpoint) // API call if timer != nil { timer.ObserveDuration() }
func (APIClientMetrics) ReportErrors ¶ added in v0.6.0
func (pm APIClientMetrics) ReportErrors(err error, labelValues ...string)
ReportErrors measures any API client call failures. Simply add this before calling the API:
defer func() { metric.ReportErrors(err, server, endpoint) }() response, err := call(server, endpoint)
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 LoggingResponseWriter ¶ added in v0.5.4
type LoggingResponseWriter struct { http.ResponseWriter // contains filtered or unexported fields }
LoggingResponseWriter records the HTTP status code of a ResponseWriter, so we can use it to log response times for individual status codes.
func NewLoggingResponseWriter ¶ added in v0.5.4
func NewLoggingResponseWriter(w http.ResponseWriter) *LoggingResponseWriter
NewLoggingResponseWriter creates a new LoggingResponseWriter
func (*LoggingResponseWriter) Write ¶ added in v0.5.4
func (w *LoggingResponseWriter) Write(body []byte) (int, error)
Write implements the http.ResponseWriter interface
func (*LoggingResponseWriter) WriteHeader ¶ added in v0.5.4
func (w *LoggingResponseWriter) WriteHeader(code int)
WriteHeader implements the http.ResponseWriter interface
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
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
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.