Skip to content

Commit

Permalink
Try a Prometheus histogram
Browse files Browse the repository at this point in the history
Co-authored-by: Jose Luis Lucas <[email protected]>
  • Loading branch information
2 people authored and iknite committed Feb 19, 2019
1 parent 275b69d commit bb9ab67
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api/apihttp/apihttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package apihttp
import (
"bytes"
"encoding/json"
"math/rand"
"net/http"
"time"

Expand All @@ -33,6 +34,12 @@ import (
)

var (
requestDuration = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "example_request_duration_seconds",
Help: "Histogram for the runtime of a simple example function.",
Buckets: prometheus.LinearBuckets(0.01, 0.01, 10),
})

// Prometheus:
opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
Name: "qed_healthcheck_ops_total",
Expand All @@ -58,6 +65,12 @@ type HealthCheckResponse struct {
// If everything is allright, the HTTP status is 200 and the body contains:
// {"version": "0", "status":"ok"}
func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
timer := prometheus.NewTimer(requestDuration)
defer timer.ObserveDuration()

// Do something here that takes time.
time.Sleep(time.Duration(rand.NormFloat64()*10000+50000) * time.Microsecond)

// Prometheus
opsProcessed.Inc()

Expand Down

0 comments on commit bb9ab67

Please sign in to comment.