Documentation
¶
Overview ¶
Package httpserver provides a standard way of writing an HTTP server. It supports:
- Creating an HTTP Server listening on a static or dynamic port
- Running a Prometheus metrics server
- Creating an HTTP Server with one or more HTTP handlers
- 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 ¶
This section is empty.
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 InstrumentedHandler ¶ added in v0.3.0
type InstrumentedHandler struct {
// contains filtered or unexported fields
}
type MethodFilteredHandler ¶ added in v0.3.0
type MethodFilteredHandler struct {
// contains filtered or unexported fields
}
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 (*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) Serve ¶ added in v0.2.0
Serve starts the HTTP server. When the server is shut down, it returns http.ErrServerClosed.
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"