metrics

package
v0.0.0-...-23fd86d Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package metrics is the primary entrypoint into LabKit's metrics utilities.

Provided Functionality

- Provides http middlewares for recording metrics for an http server

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HandlerFactory

type HandlerFactory func(next http.Handler, opts ...HandlerOption) http.Handler

HandlerFactory creates handler middleware instances. Created by NewHandlerFactory.

func NewHandlerFactory

func NewHandlerFactory(opts ...HandlerFactoryOption) HandlerFactory

NewHandlerFactory will create a function for creating metric middlewares. The resulting function can be called multiple times to obtain multiple middleware instances. Each instance can be configured with different options that will be applied to the same underlying metrics.

Example
package main

import (
	"fmt"
	"log"
	"net/http"

	"gitlab.com/gitlab-org/labkit/metrics"
)

func main() {
	// Tell prometheus to include a "route" label for the http metrics
	newMetricHandlerFunc := metrics.NewHandlerFactory(metrics.WithLabels("route"))

	http.Handle("/foo",
		newMetricHandlerFunc(
			http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				fmt.Fprintf(w, "Hello foo")
			}),
			metrics.WithLabelValues(map[string]string{"route": "/foo"}), // Add instrumentation with a custom label of route="/foo"
		))

	http.Handle("/bar",
		newMetricHandlerFunc(
			http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				fmt.Fprintf(w, "Hello bar")
			}),
			metrics.WithLabelValues(map[string]string{"route": "/bar"}), // Add instrumentation with a custom label of route="/bar"
		))

	log.Fatal(http.ListenAndServe(":8080", nil))
}
Output:

type HandlerFactoryOption

type HandlerFactoryOption func(*handlerFactoryConfig)

HandlerFactoryOption is used to pass options in NewHandlerFactory.

func WithByteSizeBuckets

func WithByteSizeBuckets(buckets []float64) HandlerFactoryOption

WithByteSizeBuckets will configure the byte size histogram buckets for request and response payloads.

func WithLabels

func WithLabels(labels ...string) HandlerFactoryOption

WithLabels will configure additional labels to apply to the metrics.

func WithNamespace

func WithNamespace(namespace string) HandlerFactoryOption

WithNamespace will configure the namespace to apply to the metrics.

func WithRequestDurationBuckets

func WithRequestDurationBuckets(buckets []float64) HandlerFactoryOption

WithRequestDurationBuckets will configure the duration buckets used for incoming request histogram buckets.

func WithTimeToWriteHeaderDurationBuckets

func WithTimeToWriteHeaderDurationBuckets(buckets []float64) HandlerFactoryOption

WithTimeToWriteHeaderDurationBuckets will configure the time to write header duration histogram buckets.

type HandlerOption

type HandlerOption func(*handlerConfig)

HandlerOption is used to pass options to the HandlerFactory instance.

func WithLabelValues

func WithLabelValues(labelValues map[string]string) HandlerOption

WithLabelValues will configure labels values to apply to this handler.

Jump to

Keyboard shortcuts

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