-
Notifications
You must be signed in to change notification settings - Fork 666
/
metrics.go
151 lines (130 loc) · 5.87 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
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package api
import (
"context"
"time"
"github.com/absmach/magistrala"
mgclients "github.com/absmach/magistrala/pkg/clients"
"github.com/absmach/magistrala/things"
"github.com/go-kit/kit/metrics"
)
var _ things.Service = (*metricsMiddleware)(nil)
type metricsMiddleware struct {
counter metrics.Counter
latency metrics.Histogram
svc things.Service
}
// MetricsMiddleware returns a new metrics middleware wrapper.
func MetricsMiddleware(svc things.Service, counter metrics.Counter, latency metrics.Histogram) things.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
svc: svc,
}
}
func (ms *metricsMiddleware) CreateThings(ctx context.Context, token string, clients ...mgclients.Client) ([]mgclients.Client, error) {
defer func(begin time.Time) {
ms.counter.With("method", "register_things").Add(1)
ms.latency.With("method", "register_things").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.CreateThings(ctx, token, clients...)
}
func (ms *metricsMiddleware) ViewClient(ctx context.Context, token, id string) (mgclients.Client, error) {
defer func(begin time.Time) {
ms.counter.With("method", "view_thing").Add(1)
ms.latency.With("method", "view_thing").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.ViewClient(ctx, token, id)
}
func (ms *metricsMiddleware) ViewClientPerms(ctx context.Context, token, id string) ([]string, error) {
defer func(begin time.Time) {
ms.counter.With("method", "view_thing_permissions").Add(1)
ms.latency.With("method", "view_thing_permissions").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.ViewClientPerms(ctx, token, id)
}
func (ms *metricsMiddleware) ListClients(ctx context.Context, token, reqUserID string, pm mgclients.Page) (mgclients.ClientsPage, error) {
defer func(begin time.Time) {
ms.counter.With("method", "list_things").Add(1)
ms.latency.With("method", "list_things").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.ListClients(ctx, token, reqUserID, pm)
}
func (ms *metricsMiddleware) UpdateClient(ctx context.Context, token string, client mgclients.Client) (mgclients.Client, error) {
defer func(begin time.Time) {
ms.counter.With("method", "update_thing_name_and_metadata").Add(1)
ms.latency.With("method", "update_thing_name_and_metadata").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.UpdateClient(ctx, token, client)
}
func (ms *metricsMiddleware) UpdateClientTags(ctx context.Context, token string, client mgclients.Client) (mgclients.Client, error) {
defer func(begin time.Time) {
ms.counter.With("method", "update_thing_tags").Add(1)
ms.latency.With("method", "update_thing_tags").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.UpdateClientTags(ctx, token, client)
}
func (ms *metricsMiddleware) UpdateClientSecret(ctx context.Context, token, oldSecret, newSecret string) (mgclients.Client, error) {
defer func(begin time.Time) {
ms.counter.With("method", "update_thing_secret").Add(1)
ms.latency.With("method", "update_thing_secret").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.UpdateClientSecret(ctx, token, oldSecret, newSecret)
}
func (ms *metricsMiddleware) EnableClient(ctx context.Context, token, id string) (mgclients.Client, error) {
defer func(begin time.Time) {
ms.counter.With("method", "enable_thing").Add(1)
ms.latency.With("method", "enable_thing").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.EnableClient(ctx, token, id)
}
func (ms *metricsMiddleware) DisableClient(ctx context.Context, token, id string) (mgclients.Client, error) {
defer func(begin time.Time) {
ms.counter.With("method", "disable_thing").Add(1)
ms.latency.With("method", "disable_thing").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.DisableClient(ctx, token, id)
}
func (ms *metricsMiddleware) ListClientsByGroup(ctx context.Context, token, groupID string, pm mgclients.Page) (mp mgclients.MembersPage, err error) {
defer func(begin time.Time) {
ms.counter.With("method", "list_things_by_channel").Add(1)
ms.latency.With("method", "list_things_by_channel").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.ListClientsByGroup(ctx, token, groupID, pm)
}
func (ms *metricsMiddleware) Identify(ctx context.Context, key string) (string, error) {
defer func(begin time.Time) {
ms.counter.With("method", "identify_thing").Add(1)
ms.latency.With("method", "identify_thing").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.Identify(ctx, key)
}
func (ms *metricsMiddleware) Authorize(ctx context.Context, req *magistrala.AuthorizeReq) (id string, err error) {
defer func(begin time.Time) {
ms.counter.With("method", "authorize").Add(1)
ms.latency.With("method", "authorize").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.Authorize(ctx, req)
}
func (ms *metricsMiddleware) Share(ctx context.Context, token, id, relation string, userids ...string) error {
defer func(begin time.Time) {
ms.counter.With("method", "share").Add(1)
ms.latency.With("method", "share").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.Share(ctx, token, id, relation, userids...)
}
func (ms *metricsMiddleware) Unshare(ctx context.Context, token, id, relation string, userids ...string) error {
defer func(begin time.Time) {
ms.counter.With("method", "unshare").Add(1)
ms.latency.With("method", "unshare").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.Unshare(ctx, token, id, relation, userids...)
}
func (ms *metricsMiddleware) DeleteClient(ctx context.Context, token, id string) error {
defer func(begin time.Time) {
ms.counter.With("method", "delete_client").Add(1)
ms.latency.With("method", "delete_client").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.DeleteClient(ctx, token, id)
}