-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
110 lines (96 loc) · 2.91 KB
/
provider.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
101
102
103
104
105
106
107
108
109
110
package metrics
import (
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/zacscoding/go-rest-template/internal/config"
)
//go:generate mockery --name Provider --filename provider.go
type Provider interface {
// RecordApiCount increases count of api request with given code, method, path labels
RecordApiCount(code int, method, path string)
// RecordApiLatency observes given elapsed mills with given code, method, path labels
RecordApiLatency(code int, method, path string, elapsed time.Duration)
// RecordCache increases count of cache request with given key, hit
RecordCache(key string, hit bool)
}
type provider struct {
namespace string
subsystem string
apiMetricsProvider apiMetricsProvider
cacheMetricsProvider cacheMetricsProvider
}
type apiMetricsProvider struct {
requestCounter *prometheus.CounterVec
requestLatency *prometheus.SummaryVec
}
type cacheMetricsProvider struct {
cacheTotalCounter *prometheus.CounterVec
cacheHitCounter *prometheus.CounterVec
}
// NewProvider returns a new Provider with given conf config.Config.
func NewProvider(conf *config.Config) Provider {
var (
ns = conf.Metric.Namespace
ss = conf.Metric.Subsystem
)
p := provider{
namespace: ns,
subsystem: ss,
apiMetricsProvider: apiMetricsProvider{
requestCounter: promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: ns,
Subsystem: ss,
Name: "api_request_count",
Help: "Total count of request",
},
[]string{"code", "method", "path"},
),
requestLatency: promauto.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: ns,
Subsystem: ss,
Name: "api_request_latency",
Help: "Elapsed time of request",
},
[]string{"code", "method", "path"},
),
},
cacheMetricsProvider: cacheMetricsProvider{
cacheTotalCounter: promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: ns,
Subsystem: ss,
Name: "cache_total",
Help: "Total count of cache requests",
},
[]string{"key"},
),
cacheHitCounter: promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: ns,
Subsystem: ss,
Name: "cache_hit",
Help: "Total cache hit count",
},
[]string{"key"},
),
},
}
return &p
}
func (p *provider) RecordApiCount(code int, method, path string) {
p.apiMetricsProvider.requestCounter.WithLabelValues(strconv.Itoa(code), method, path).Inc()
}
func (p *provider) RecordApiLatency(code int, method, path string, elapsed time.Duration) {
mills := float64(elapsed.Milliseconds())
p.apiMetricsProvider.requestLatency.WithLabelValues(strconv.Itoa(code), method, path).Observe(mills)
}
func (p *provider) RecordCache(key string, hit bool) {
p.cacheMetricsProvider.cacheTotalCounter.WithLabelValues(key).Inc()
if hit {
p.cacheMetricsProvider.cacheHitCounter.WithLabelValues(key).Inc()
}
}