Documentation ¶
Overview ¶
Package server provides utilities for running and testing a prometheus scrape server.
Server provides an HTTP server with a Prometheus metrics endpoint configured. Running it is as simple as:
server := metrics.New(8080) server.Run()
This will start an HTTP server on port 8080, with a /metrics endpoint for Prometheus scraping.
For HTTP servers, you may add additional handlers by using NewWithHandlers:
server := metrics.NewWithHandlers(8080, []metrics.Handler{ { Path: "/hello", Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { if req.Method == "POST" { _, _ = w.Write([]byte("hello!")) return } http.Error(w, "only POST is allowed", http.StatusBadRequest) }), Methods: []string{http.MethodPost}, }, }) server.Run()
If you need to build your own HTTP server, you can use GetRouter() instead:
r := metrics.GetRouter() r.Path("/hello").Handler(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte("hello!")) })) server := &http.Server{ Handler: r, Addr: ":8080", } server.ListenAndServe()
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetRouter ¶
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",
Types ¶
type Handler ¶
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 NewWithHandlers.
type Server ¶
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 New ¶
New 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 NewWithHandlers ¶
NewWithHandlers 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.