server

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2022 License: MIT Imports: 8 Imported by: 1

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

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",

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

func New(port int) (server *Server)

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

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

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.

func (*Server) Run

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

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

Shutdown performs a graceful shutdown of the HTTP Server.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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