-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
auth_service.go
132 lines (116 loc) · 4.82 KB
/
auth_service.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
package prometheus
import (
"context"
"fmt"
"time"
platform "github.com/influxdata/influxdb/v2"
"github.com/prometheus/client_golang/prometheus"
)
// AuthorizationService manages authorizations.
type AuthorizationService struct {
requestCount *prometheus.CounterVec
requestDuration *prometheus.HistogramVec
AuthorizationService platform.AuthorizationService
}
// NewAuthorizationService creates an instance of AuthorizationService.
func NewAuthorizationService() *AuthorizationService {
// TODO: what to make these values
namespace := "auth"
subsystem := "prometheus"
s := &AuthorizationService{
requestCount: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "requests_total",
Help: "Number of http requests received",
}, []string{"method", "error"}),
requestDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "request_duration_seconds",
Help: "Time taken to respond to HTTP request",
// TODO(desa): determine what spacing these buckets should have.
Buckets: prometheus.ExponentialBuckets(0.001, 1.5, 25),
}, []string{"method", "error"}),
}
return s
}
// FindAuthorizationByID returns an authorization given a id, records function call latency, and counts function calls.
func (s *AuthorizationService) FindAuthorizationByID(ctx context.Context, id platform.ID) (a *platform.Authorization, err error) {
defer func(start time.Time) {
labels := prometheus.Labels{
"method": "FindAuthorizationByID",
"error": fmt.Sprint(err != nil),
}
s.requestCount.With(labels).Add(1)
s.requestDuration.With(labels).Observe(time.Since(start).Seconds())
}(time.Now())
return s.AuthorizationService.FindAuthorizationByID(ctx, id)
}
// FindAuthorizationByToken returns an authorization given a token, records function call latency, and counts function calls.
func (s *AuthorizationService) FindAuthorizationByToken(ctx context.Context, t string) (a *platform.Authorization, err error) {
defer func(start time.Time) {
labels := prometheus.Labels{
"method": "FindAuthorizationByToken",
"error": fmt.Sprint(err != nil),
}
s.requestCount.With(labels).Add(1)
s.requestDuration.With(labels).Observe(time.Since(start).Seconds())
}(time.Now())
return s.AuthorizationService.FindAuthorizationByToken(ctx, t)
}
// FindAuthorizations returns authorizations given a filter, records function call latency, and counts function calls.
func (s *AuthorizationService) FindAuthorizations(ctx context.Context, filter platform.AuthorizationFilter, opt ...platform.FindOptions) (as []*platform.Authorization, i int, err error) {
defer func(start time.Time) {
labels := prometheus.Labels{
"method": "FindAuthorizations",
"error": fmt.Sprint(err != nil),
}
s.requestCount.With(labels).Add(1)
s.requestDuration.With(labels).Observe(time.Since(start).Seconds())
}(time.Now())
return s.AuthorizationService.FindAuthorizations(ctx, filter, opt...)
}
// CreateAuthorization creates an authorization, records function call latency, and counts function calls.
func (s *AuthorizationService) CreateAuthorization(ctx context.Context, a *platform.Authorization) (err error) {
defer func(start time.Time) {
labels := prometheus.Labels{
"method": "CreateAuthorization",
"error": fmt.Sprint(err != nil),
}
s.requestCount.With(labels).Add(1)
s.requestDuration.With(labels).Observe(time.Since(start).Seconds())
}(time.Now())
return s.AuthorizationService.CreateAuthorization(ctx, a)
}
// DeleteAuthorization deletes an authorization, records function call latency, and counts function calls.
func (s *AuthorizationService) DeleteAuthorization(ctx context.Context, id platform.ID) (err error) {
defer func(start time.Time) {
labels := prometheus.Labels{
"method": "DeleteAuthorization",
"error": fmt.Sprint(err != nil),
}
s.requestCount.With(labels).Add(1)
s.requestDuration.With(labels).Observe(time.Since(start).Seconds())
}(time.Now())
return s.AuthorizationService.DeleteAuthorization(ctx, id)
}
// UpdateAuthorization updates the status and description.
func (s *AuthorizationService) UpdateAuthorization(ctx context.Context, id platform.ID, upd *platform.AuthorizationUpdate) (a *platform.Authorization, err error) {
defer func(start time.Time) {
labels := prometheus.Labels{
"method": "setAuthorizationStatus",
"error": fmt.Sprint(err != nil),
}
s.requestCount.With(labels).Add(1)
s.requestDuration.With(labels).Observe(time.Since(start).Seconds())
}(time.Now())
return s.AuthorizationService.UpdateAuthorization(ctx, id, upd)
}
// PrometheusCollectors returns all authorization service prometheus collectors.
func (s *AuthorizationService) PrometheusCollectors() []prometheus.Collector {
return []prometheus.Collector{
s.requestCount,
s.requestDuration,
}
}