forked from openfaas/faas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
65 lines (51 loc) · 1.48 KB
/
metrics.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
package metrics
import (
"context"
"fmt"
"log"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// MetricsServer provides instrumentation for HTTP calls
type MetricsServer struct {
s *http.Server
port int
}
// Register binds a HTTP server to expose Prometheus metrics
func (m *MetricsServer) Register(metricsPort int) {
m.port = metricsPort
readTimeout := time.Millisecond * 500
writeTimeout := time.Millisecond * 500
metricsMux := http.NewServeMux()
metricsMux.Handle("/metrics", promhttp.Handler())
m.s = &http.Server{
Addr: fmt.Sprintf(":%d", metricsPort),
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
MaxHeaderBytes: 1 << 20, // Max header of 1MB
Handler: metricsMux,
}
}
// Serve http traffic in go routine, non-blocking
func (m *MetricsServer) Serve(cancel chan bool) {
log.Printf("Metrics server. Port: %d\n", m.port)
go func() {
if err := m.s.ListenAndServe(); err != http.ErrServerClosed {
panic(fmt.Sprintf("metrics error ListenAndServe: %v\n", err))
}
}()
go func() {
select {
case <-cancel:
log.Printf("metrics server shutdown\n")
m.s.Shutdown(context.Background())
}
}()
}
// InstrumentHandler returns a handler which records HTTP requests
// as they are made
func InstrumentHandler(next http.HandlerFunc, _http Http) http.HandlerFunc {
return promhttp.InstrumentHandlerCounter(_http.RequestsTotal,
promhttp.InstrumentHandlerDuration(_http.RequestDurationHistogram, next))
}