-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprometheus.go
90 lines (76 loc) · 2.21 KB
/
prometheus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package fiberprom
import (
"log"
"strconv"
"time"
"github.com/gofiber/adaptor/v2"
"github.com/gofiber/fiber/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var defaultMetricPath = "/metrics"
// Prometheus contains the metrics gathered by the instance and its path
type Prometheus struct {
reqDur *prometheus.HistogramVec
router fiber.Router
MetricsPath string
}
// NewPrometheus generates a new set of metrics with a certain subsystem name
func NewPrometheus(subsystem string) *Prometheus {
p := &Prometheus{
MetricsPath: defaultMetricPath,
}
p.registerMetrics(subsystem)
return p
}
func (p *Prometheus) registerMetrics(subsystem string) {
p.reqDur = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: subsystem,
Name: "request_duration_seconds",
Help: "request latencies",
Buckets: []float64{.005, .01, .02, 0.04, .06, 0.08, .1, 0.15, .25, 0.4, .6, .8, 1, 1.5, 2, 3, 5},
},
[]string{"code", "path"},
)
err := prometheus.Register(p.reqDur)
if err != nil {
log.Printf("failed to register metrics: %v", err)
}
}
// Use adds the middleware to a fiber
func (p *Prometheus) Use(r fiber.Router) {
r.Use(p.HandlerFunc())
r.Get(p.MetricsPath, prometheusHandler())
}
// HandlerFunc is onion or wrapper to handler for fasthttp listenandserve
func (p *Prometheus) HandlerFunc() fiber.Handler {
return func(ctx *fiber.Ctx) error {
uri := string(ctx.Request().URI().Path())
if uri == p.MetricsPath {
// next
return ctx.Next()
}
start := time.Now()
// next
err := ctx.Next()
status := fiber.StatusInternalServerError
if err != nil {
if e, ok := err.(*fiber.Error); ok {
// Get correct error code from fiber.Error type
status = e.Code
}
} else {
status = ctx.Response().StatusCode()
}
uri = ctx.Route().Path
elapsed := float64(time.Since(start)) / float64(time.Second)
ep := ctx.Method() + "_" + uri
p.reqDur.WithLabelValues(strconv.Itoa(status), ep).Observe(elapsed)
return err
}
}
// since prometheus/client_golang use net/http we need this net/http adapter for fiber
func prometheusHandler() fiber.Handler {
return adaptor.HTTPHandler(promhttp.Handler())
}