/
metrics.go
71 lines (58 loc) · 2.21 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
66
67
68
69
70
71
package metrics
import (
"fmt"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"github.com/unanet/go/pkg/log"
)
var (
StatRequestSaturationGauge = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "http_request_saturation",
Help: "The total number of requests inside the server (transactions serving)",
}, []string{"method", "protocol", "path"})
StatBuildInfo = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "service_build_info",
Help: "A metric with a constant '1' value labeled by version, revision, branch, and goversion from which the service was built",
}, []string{"service", "revision", "branch", "version", "author", "build_date", "build_user", "build_host"})
StatHTTPRequestCount = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "http_request_total",
Help: "The total number of incoming requests to the service",
}, []string{"method", "protocol", "path"})
StatHTTPResponseCount = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "http_response_total",
Help: "The total number of outgoing responses to the client",
}, []string{"code", "method", "protocol", "path"})
StatRequestDurationHistogram = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "time spent processing an http request in seconds",
Buckets: prometheus.ExponentialBuckets(0.01, 1.6, 20),
}, []string{"method", "protocol", "path"})
)
func StartMetricsServer(port int) *http.Server {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
server := &http.Server{
ReadTimeout: time.Duration(5) * time.Second,
WriteTimeout: time.Duration(30) * time.Second,
IdleTimeout: time.Duration(90) * time.Second,
Addr: fmt.Sprintf(":%d", port),
Handler: mux,
}
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Logger.Panic("Failed to Start Metrics Server", zap.Error(err))
}
log.Logger.Info("Metrics Server Shutdown")
}()
log.Logger.Info("Metrics Listener", zap.Int("port", port))
return server
}