-
Notifications
You must be signed in to change notification settings - Fork 2
/
prometheus.go
84 lines (74 loc) · 2.47 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
package main
import (
"fmt"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
)
var (
awsReqCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "awscsm",
Name: "req_count",
Help: "Total reqests made to AWS",
}, []string{"result"})
awsReqAttemptCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "awscsm",
Name: "req_attempt_count",
Help: "Total reqest attempts made to AWS",
}, []string{"result"})
awsThrottleCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "awscsm",
Name: "throttle_count",
Help: "Number of requests that failed due to throttling",
}, []string{"service"})
awsReqLatency = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "awscsm",
Name: "req_latency",
Help: "Latency in ms of aws requests",
Buckets: prometheus.LinearBuckets(100, 500, 20),
}, []string{"service"})
)
func registerPrometheusMetrics() {
prometheus.MustRegister(awsReqCount)
prometheus.MustRegister(awsThrottleCount)
prometheus.MustRegister(awsReqLatency)
prometheus.MustRegister(awsReqAttemptCount)
logrus.Infof("Finished registering metrics")
}
func serveMetrics(servePort *int) {
logrus.Infof("Serving metrics on :%d", *servePort)
http.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(fmt.Sprintf(":%d", *servePort), nil)
if err != nil {
logrus.Errorf("Error serving metrics: %v", err)
}
}
func checkSuccessCode(statusCode int) bool {
return statusCode >= 200 && statusCode <= 299
}
func recordMetric(metricData *AWSMetricsData) {
switch metricData.Type {
case "ApiCall":
if checkSuccessCode(metricData.FinalHTTPStatusCode) {
awsReqCount.WithLabelValues("success").Add(1)
} else {
awsReqCount.WithLabelValues("error").Add(1)
}
logrus.Infof("%f", float64(metricData.Latency))
awsReqLatency.WithLabelValues(metricData.Service).Observe(float64(metricData.Latency))
break
case "ApiCallAttempt":
if checkSuccessCode(metricData.HTTPStatusCode) {
awsReqAttemptCount.WithLabelValues("success").Add(1)
} else {
awsReqAttemptCount.WithLabelValues("error").Add(1)
}
logrus.Infof("%f", float64(metricData.AttemptLatency))
awsReqLatency.WithLabelValues(metricData.Service).Observe(float64(metricData.AttemptLatency))
break
}
if metricData.FinalHTTPStatusCode == 419 {
awsThrottleCount.WithLabelValues(metricData.Service).Add(1)
}
}