httpserver

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2023 License: MIT Imports: 9 Imported by: 1

README

httpserver

Test GoDoc

Basic instrumented HTTP server that I found myself writing over and over again.

Documentation

Authors

  • Christophe Lambin

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Documentation

Overview

Package httpserver provides a standard way of writing an HTTP server. It supports:

  1. Creating an HTTP Server listening on a static or dynamic port
  2. Running a Prometheus metrics server
  3. Creating an HTTP Server with one or more HTTP handlers
  4. Recording Prometheus metrics for latency (average or quantiles) & number of requests.
Example
package main

import (
	"errors"
	"github.com/clambin/go-common/httpserver"
	"github.com/prometheus/client_golang/prometheus"
	"net/http"
)

func main() {
	s, err := httpserver.New(
		httpserver.WithPort{Port: 8080},
		httpserver.WithPrometheus{},
		httpserver.WithMetrics{Application: "example", MetricsType: httpserver.Summary},
		httpserver.WithHandlers{Handlers: []httpserver.Handler{{
			Path:    "/",
			Methods: []string{http.MethodGet},
			Handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
				_, _ = w.Write([]byte("Hello world"))
			}),
		}},
		},
	)

	if err != nil {
		panic(err)
	}
	prometheus.MustRegister(s)
	err = s.Serve()
	if !errors.Is(err, http.ErrServerClosed) {
		panic(err)
	}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MethodFilter added in v0.4.0

func MethodFilter(methods ...string) func(next http.Handler) http.Handler

Types

type Handler

type Handler struct {
	// Path of the endpoint (e.g. "/health"). Can be any path that's valid for gorilla/mux router's Path() method.
	Path string
	// Methods that the handler should support. If empty, defaults to http.MethodGet.
	Methods []string
	// Handler that implements the endpoint.
	Handler http.Handler
}

Handler contains a path to be registered in the Server's HTTP server

type MetricsType added in v0.2.0

type MetricsType int

MetricsType specifies the type of metrics to record for request duration. Use Summary if you are only interested in the average latency. Use Histogram if you want to use a histogram to measure a service level indicator (eg latency of 95% of all requests).

const (
	// Summary measures the average duration.
	Summary MetricsType = iota
	// Histogram measures the latency in buckets and can be used to calculate a service level indicator. WithMetrics.Buckets
	// specify the buckets to be used. If none are provided, prometheus.DefBuckets will be used.
	Histogram
)

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option specified configuration options for Server

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server implements a configurable HTTP Server. See the different WithXXX structs for available options.

func New

func New(options ...Option) (*Server, error)

New returns a Server with the specified options

func (*Server) Collect added in v0.2.0

func (s *Server) Collect(c chan<- prometheus.Metric)

Collect implements the prometheus.Collector interface

func (*Server) Describe added in v0.2.0

func (s *Server) Describe(descs chan<- *prometheus.Desc)

Describe implements the prometheus.Collector interface

func (*Server) GetPort

func (s *Server) GetPort() int

GetPort returns the HTTP Server's listening port

func (*Server) Serve added in v0.2.0

func (s *Server) Serve() error

Serve starts the HTTP server. When the server is shut down, it returns http.ErrServerClosed.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP calls the server's handler. Mainly intended to be used in unit tests without starting the underlying HTTP server.

func (*Server) Shutdown

func (s *Server) Shutdown(timeout time.Duration) error

Shutdown performs a graceful shutdown of the HTTP server

type WithHandlers

type WithHandlers struct {
	Handlers []Handler
}

WithHandlers adds the specified handlers to the server

type WithMetrics

type WithMetrics struct {
	Namespace   string
	Subsystem   string
	Application string
	MetricsType MetricsType
	Buckets     []float64
}

WithMetrics will collect the specified metrics to instrument the Server's Handlers.

type WithPort

type WithPort struct {
	Port int
}

WithPort specifies the Server's listening port. If no port is specified, Server will listen on a random port. Use GetPort() to determine the actual listening port

type WithPrometheus

type WithPrometheus struct {
	Path string
}

WithPrometheus adds a Prometheus metrics endpoint to the server at the specified Path. Default path is "/metrics"

Directories

Path Synopsis
Package middleware contains HTTP middleware.
Package middleware contains HTTP middleware.

Jump to

Keyboard shortcuts

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