/
metrics.go
156 lines (128 loc) · 4.39 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package repository
import (
"context"
"errors"
"time"
"github.com/gopad/gopad-api/pkg/metrics"
"github.com/gopad/gopad-api/pkg/model"
"github.com/prometheus/client_golang/prometheus"
)
// MetricsRepository implements UsersRepository interface.
type MetricsRepository struct {
upstream UsersRepository
requestLatency *prometheus.HistogramVec
errorsCount *prometheus.CounterVec
requestCount *prometheus.CounterVec
}
// NewMetricsRepository wraps the UsersRepository and provides metrics for its methods.
func NewMetricsRepository(repository UsersRepository, metricz *metrics.Metrics) UsersRepository {
return &MetricsRepository{
upstream: repository,
requestLatency: metricz.RegisterHistogram(
prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: metricz.Namespace,
Subsystem: "users_repository",
Name: "request_latency_microseconds",
Help: "Histogram of latencies for requests to the users repository.",
Buckets: []float64{0.001, 0.01, 0.1, 0.5, 1.0, 2.0, 5.0, 10.0},
},
[]string{"method"},
),
),
errorsCount: metricz.RegisterCounter(
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: metricz.Namespace,
Subsystem: "users_repository",
Name: "errors_count",
Help: "Total number of errors within the users repository.",
},
[]string{"method"},
),
),
requestCount: metricz.RegisterCounter(
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: metricz.Namespace,
Subsystem: "users_repository",
Name: "request_count",
Help: "Total number of requests to the users repository.",
},
[]string{"method"},
),
),
}
}
// List implements the UsersRepository interface.
func (r *MetricsRepository) List(ctx context.Context) ([]*model.User, error) {
defer func(start time.Time) {
r.requestCount.WithLabelValues("list").Add(1)
r.requestLatency.WithLabelValues("list").Observe(time.Since(start).Seconds())
}(time.Now())
records, err := r.upstream.List(ctx)
if err != nil {
r.errorsCount.WithLabelValues("list").Add(1)
}
return records, err
}
// Create implements the UsersRepository interface.
func (r *MetricsRepository) Create(ctx context.Context, user *model.User) (*model.User, error) {
defer func(start time.Time) {
r.requestCount.WithLabelValues("create").Add(1)
r.requestLatency.WithLabelValues("create").Observe(time.Since(start).Seconds())
}(time.Now())
record, err := r.upstream.Create(ctx, user)
if err != nil {
r.errorsCount.WithLabelValues("create").Add(1)
}
return record, err
}
// Update implements the UsersRepository interface.
func (r *MetricsRepository) Update(ctx context.Context, user *model.User) (*model.User, error) {
defer func(start time.Time) {
r.requestCount.WithLabelValues("update").Add(1)
r.requestLatency.WithLabelValues("update").Observe(time.Since(start).Seconds())
}(time.Now())
record, err := r.upstream.Update(ctx, user)
if err != nil && !errors.Is(err, ErrUserNotFound) {
r.errorsCount.WithLabelValues("update").Add(1)
}
return record, err
}
// Show implements the UsersRepository interface.
func (r *MetricsRepository) Show(ctx context.Context, id string) (*model.User, error) {
defer func(start time.Time) {
r.requestCount.WithLabelValues("show").Add(1)
r.requestLatency.WithLabelValues("show").Observe(time.Since(start).Seconds())
}(time.Now())
record, err := r.upstream.Show(ctx, id)
if err != nil && !errors.Is(err, ErrUserNotFound) {
r.errorsCount.WithLabelValues("show").Add(1)
}
return record, err
}
// Delete implements the UsersRepository interface.
func (r *MetricsRepository) Delete(ctx context.Context, name string) error {
defer func(start time.Time) {
r.requestCount.WithLabelValues("delete").Add(1)
r.requestLatency.WithLabelValues("delete").Observe(time.Since(start).Seconds())
}(time.Now())
err := r.upstream.Delete(ctx, name)
if err != nil {
r.errorsCount.WithLabelValues("delete").Add(1)
}
return err
}
// Exists implements the UsersRepository interface.
func (r *MetricsRepository) Exists(ctx context.Context, name string) (bool, error) {
defer func(start time.Time) {
r.requestCount.WithLabelValues("exists").Add(1)
r.requestLatency.WithLabelValues("exists").Observe(time.Since(start).Seconds())
}(time.Now())
exists, err := r.upstream.Exists(ctx, name)
if err != nil {
r.errorsCount.WithLabelValues("exists").Add(1)
}
return exists, err
}