-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
middleware_user_metrics.go
98 lines (80 loc) · 3.11 KB
/
middleware_user_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
package tenant
import (
"context"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/metric"
"github.com/prometheus/client_golang/prometheus"
)
var _ influxdb.UserService = (*UserMetrics)(nil)
var _ influxdb.PasswordsService = (*PasswordMetrics)(nil)
type UserMetrics struct {
// RED metrics
rec *metric.REDClient
userService influxdb.UserService
}
// NewUserMetrics returns a metrics service middleware for the User Service.
func NewUserMetrics(reg prometheus.Registerer, s influxdb.UserService, opts ...metric.MetricsOption) *UserMetrics {
o := metric.ApplyMetricOpts(opts...)
return &UserMetrics{
rec: metric.New(reg, o.ApplySuffix("user")),
userService: s,
}
}
func (m *UserMetrics) FindUserByID(ctx context.Context, id influxdb.ID) (*influxdb.User, error) {
rec := m.rec.Record("find_user_by_id")
user, err := m.userService.FindUserByID(ctx, id)
return user, rec(err)
}
func (m *UserMetrics) FindUser(ctx context.Context, filter influxdb.UserFilter) (*influxdb.User, error) {
rec := m.rec.Record("find_user")
user, err := m.userService.FindUser(ctx, filter)
return user, rec(err)
}
func (m *UserMetrics) FindUsers(ctx context.Context, filter influxdb.UserFilter, opt ...influxdb.FindOptions) ([]*influxdb.User, int, error) {
rec := m.rec.Record("find_users")
users, n, err := m.userService.FindUsers(ctx, filter, opt...)
return users, n, rec(err)
}
func (m *UserMetrics) CreateUser(ctx context.Context, u *influxdb.User) error {
rec := m.rec.Record("create_user")
err := m.userService.CreateUser(ctx, u)
return rec(err)
}
func (m *UserMetrics) UpdateUser(ctx context.Context, id influxdb.ID, upd influxdb.UserUpdate) (*influxdb.User, error) {
rec := m.rec.Record("update_user")
updatedUser, err := m.userService.UpdateUser(ctx, id, upd)
return updatedUser, rec(err)
}
func (m *UserMetrics) DeleteUser(ctx context.Context, id influxdb.ID) error {
rec := m.rec.Record("delete_user")
err := m.userService.DeleteUser(ctx, id)
return rec(err)
}
type PasswordMetrics struct {
// RED metrics
rec *metric.REDClient
pwdService influxdb.PasswordsService
}
// NewPasswordMetrics returns a metrics service middleware for the Password Service.
func NewPasswordMetrics(reg prometheus.Registerer, s influxdb.PasswordsService, opts ...metric.MetricsOption) *PasswordMetrics {
o := metric.ApplyMetricOpts(opts...)
return &PasswordMetrics{
rec: metric.New(reg, o.ApplySuffix("password")),
pwdService: s,
}
}
func (m *PasswordMetrics) SetPassword(ctx context.Context, userID influxdb.ID, password string) error {
rec := m.rec.Record("set_password")
err := m.pwdService.SetPassword(ctx, userID, password)
return rec(err)
}
func (m *PasswordMetrics) ComparePassword(ctx context.Context, userID influxdb.ID, password string) error {
rec := m.rec.Record("compare_password")
err := m.pwdService.ComparePassword(ctx, userID, password)
return rec(err)
}
func (m *PasswordMetrics) CompareAndSetPassword(ctx context.Context, userID influxdb.ID, old, new string) error {
rec := m.rec.Record("compare_and_set_password")
err := m.pwdService.CompareAndSetPassword(ctx, userID, old, new)
return rec(err)
}