forked from statping-ng/statping-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
100 lines (88 loc) · 2.35 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package metrics
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
)
var (
utilsHttpRequestDur = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "statping",
Name: "http_requests_duration",
Help: "Duration for h",
},
[]string{"url", "method"},
)
utilsHttpRequestBytes = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "statping",
Name: "http_response_bytes",
Help: "Response in bytes for a HTTP services",
},
[]string{"url", "method"},
)
httpDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "statping",
Name: "http_duration_seconds",
Help: "Duration of HTTP requests from the utils package",
}, []string{"path"})
)
func InitMetrics() {
prometheus.MustRegister(
serviceOnline,
serviceFailures,
serviceSuccess,
serviceStatusCode,
serviceDuration,
utilsHttpRequestDur,
utilsHttpRequestBytes,
httpDuration,
databaseStats,
queryStats,
)
}
func Histo(method string, value float64, labels ...interface{}) {
switch method {
case "duration":
utilsHttpRequestDur.WithLabelValues(convert(labels)...).Observe(value)
case "bytes":
utilsHttpRequestBytes.WithLabelValues(convert(labels)...).Observe(value)
}
}
func Timer(labels ...interface{}) prometheus.Observer {
return httpDuration.WithLabelValues(convert(labels)...)
}
func ServiceTimer(labels ...interface{}) prometheus.Observer {
return serviceDuration.WithLabelValues(convert(labels)...)
}
func Gauge(method string, value float64, labels ...interface{}) {
switch method {
case "status_code":
serviceStatusCode.WithLabelValues(convert(labels)...).Set(value)
case "online":
serviceOnline.WithLabelValues(convert(labels)...).Set(value)
}
}
func Inc(method string, labels ...interface{}) {
switch method {
case "failure":
serviceFailures.WithLabelValues(convert(labels)...).Inc()
case "success":
serviceSuccess.WithLabelValues(convert(labels)...).Inc()
}
}
func Add(method string, value float64, labels ...interface{}) {
switch method {
case "failure":
serviceFailures.WithLabelValues(convert(labels)...).Add(value)
case "success":
serviceSuccess.WithLabelValues(convert(labels)...).Add(value)
}
}
func convert(vals []interface{}) []string {
var out []string
for _, v := range vals {
out = append(out, fmt.Sprintf("%v", v))
}
return out
}