Documentation ¶
Overview ¶
Package prom provides a wrapper around a prometheus metrics registry so that all services are unified in how they expose prometheus metrics.
Example ¶
package main import ( "fmt" "io" "math/rand" "net/http" "time" "github.com/influxdata/influxdb/v2/kit/prom" "github.com/prometheus/client_golang/prometheus" "go.uber.org/zap" ) // RandomHandler implements an HTTP endpoint that prints a random float, // and it tracks prometheus metrics about the numbers it returns. type RandomHandler struct { // Cumulative sum of values served. valueCounter prometheus.Counter // Total times page served. serveCounter prometheus.Counter } var ( _ http.Handler = (*RandomHandler)(nil) _ prom.PrometheusCollector = (*RandomHandler)(nil) ) func NewRandomHandler() *RandomHandler { return &RandomHandler{ valueCounter: prometheus.NewCounter(prometheus.CounterOpts{ Name: "value_counter", Help: "Cumulative sum of values served.", }), serveCounter: prometheus.NewCounter(prometheus.CounterOpts{ Name: "serve_counter", Help: "Counter of times page has been served.", }), } } // ServeHTTP serves a random float value and updates rh's internal metrics. func (rh *RandomHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Increment serveCounter every time we serve a page. rh.serveCounter.Inc() n := rand.Float64() // Track the cumulative values served. rh.valueCounter.Add(n) fmt.Fprintf(w, "%v", n) } // PrometheusCollectors implements prom.PrometheusCollector. func (rh *RandomHandler) PrometheusCollectors() []prometheus.Collector { return []prometheus.Collector{rh.valueCounter, rh.serveCounter} } func main() { // A collection of endpoints and http.Handlers. handlers := map[string]http.Handler{ "/random": NewRandomHandler(), "/time": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, time.Now().String()) }), } // Use a local registry, not the global registry in the prometheus package. reg := prom.NewRegistry(zap.NewNop()) // Build the mux out of handlers from above. mux := http.NewServeMux() for path, h := range handlers { mux.Handle(path, h) // Only register those handlers which implement prom.PrometheusCollector. if pc, ok := h.(prom.PrometheusCollector); ok { reg.MustRegister(pc.PrometheusCollectors()...) } } // Add metrics to registry. mux.Handle("/metrics", reg.HTTPHandler()) http.ListenAndServe("localhost:8080", mux) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PrometheusCollector ¶
type PrometheusCollector interface { // PrometheusCollectors returns a slice of prometheus collectors // containing metrics for the underlying instance. PrometheusCollectors() []prometheus.Collector }
PrometheusCollector is the interface for a type to expose prometheus metrics. This interface is provided as a convention, so that you can optionally check if a type implements it and then pass its collectors to (*Registry).MustRegister.
type Registry ¶
type Registry struct { *prometheus.Registry // contains filtered or unexported fields }
Registry embeds a prometheus registry and adds a couple convenience methods.
func (*Registry) HTTPHandler ¶
HTTPHandler returns an http.Handler for the registry, so that the /metrics HTTP handler is uniformly configured across all apps in the platform.
Click to show internal directories.
Click to hide internal directories.