Skip to content

Instantly share code, notes, and snippets.

@tembleking
Created October 4, 2018 21:06
Show Gist options
  • Save tembleking/0b8968dbdf36dfef6227fbfdd9bb1a82 to your computer and use it in GitHub Desktop.
Save tembleking/0b8968dbdf36dfef6227fbfdd9bb1a82 to your computer and use it in GitHub Desktop.
Prometheus Golang Example
package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
counter = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: "golang",
Name: "my_counter",
Help: "This is my counter",
})
gauge = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: "golang",
Name: "my_gauge",
Help: "This is my gauge",
})
histogram = prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: "golang",
Name: "my_histogram",
Help: "This is my histogram",
})
summary = prometheus.NewSummary(
prometheus.SummaryOpts{
Namespace: "golang",
Name: "my_summary",
Help: "This is my summary",
})
)
func main() {
rand.Seed(time.Now().Unix())
histogramVec := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "prom_request_time",
Help: "Time it has taken to retrieve the metrics",
}, []string{"time"})
prometheus.Register(histogramVec)
http.Handle("/metrics", newHandlerWithHistogram(promhttp.Handler(), histogramVec))
prometheus.MustRegister(counter)
prometheus.MustRegister(gauge)
prometheus.MustRegister(histogram)
prometheus.MustRegister(summary)
go func() {
for {
counter.Add(rand.Float64() * 5)
gauge.Add(rand.Float64()*15 - 5)
histogram.Observe(rand.Float64() * 10)
summary.Observe(rand.Float64() * 10)
time.Sleep(time.Second)
}
}()
log.Fatal(http.ListenAndServe(":8080", nil))
}
func newHandlerWithHistogram(handler http.Handler, histogram *prometheus.HistogramVec) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
start := time.Now()
status := http.StatusOK
defer func() {
histogram.WithLabelValues(fmt.Sprintf("%d", status)).Observe(time.Since(start).Seconds())
}()
if req.Method == http.MethodGet {
handler.ServeHTTP(w, req)
return
}
status = http.StatusBadRequest
w.WriteHeader(status)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment