/
configdbinstrumenting.go
94 lines (82 loc) · 4.16 KB
/
configdbinstrumenting.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
package keycloakb
import (
"context"
"time"
cs "github.com/cloudtrust/common-service"
"github.com/cloudtrust/common-service/configuration"
"github.com/cloudtrust/common-service/database"
cm "github.com/cloudtrust/common-service/metrics"
)
// Instrumenting middleware at module level.
type configDBModuleInstrumentingMW struct {
h cm.Histogram
next ConfigurationDBModule
}
// ConfigurationDBModule is the interface of the configuration module.
type ConfigurationDBModule interface {
NewTransaction(context context.Context) (database.Transaction, error)
StoreOrUpdate(context.Context, string, configuration.RealmConfiguration) error
GetConfiguration(context.Context, string) (configuration.RealmConfiguration, error)
GetAuthorizations(context context.Context, realmID string, groupName string) ([]configuration.Authorization, error)
CreateAuthorization(context context.Context, authz configuration.Authorization) error
DeleteAuthorizations(context context.Context, realmID string, groupName string) error
DeleteAllAuthorizationsWithGroup(context context.Context, realmName, groupName string) error
}
// MakeConfigurationDBModuleInstrumentingMW makes an instrumenting middleware at module level.
func MakeConfigurationDBModuleInstrumentingMW(h cm.Histogram) func(ConfigurationDBModule) ConfigurationDBModule {
return func(next ConfigurationDBModule) ConfigurationDBModule {
return &configDBModuleInstrumentingMW{
h: h,
next: next,
}
}
}
// configDBModuleInstrumentingMW implements Module.
func (m *configDBModuleInstrumentingMW) NewTransaction(ctx context.Context) (database.Transaction, error) {
defer func(begin time.Time) {
m.h.With("correlation_id", ctx.Value(cs.CtContextCorrelationID).(string)).Observe(time.Since(begin).Seconds())
}(time.Now())
return m.next.NewTransaction(ctx)
}
// configDBModuleInstrumentingMW implements Module.
func (m *configDBModuleInstrumentingMW) StoreOrUpdate(ctx context.Context, realmName string, config configuration.RealmConfiguration) error {
defer func(begin time.Time) {
m.h.With("correlation_id", ctx.Value(cs.CtContextCorrelationID).(string)).Observe(time.Since(begin).Seconds())
}(time.Now())
return m.next.StoreOrUpdate(ctx, realmName, config)
}
// configDBModuleInstrumentingMW implements Module.
func (m *configDBModuleInstrumentingMW) GetConfiguration(ctx context.Context, realmName string) (configuration.RealmConfiguration, error) {
defer func(begin time.Time) {
m.h.With("correlation_id", ctx.Value(cs.CtContextCorrelationID).(string)).Observe(time.Since(begin).Seconds())
}(time.Now())
return m.next.GetConfiguration(ctx, realmName)
}
// configDBModuleInstrumentingMW implements Module.
func (m *configDBModuleInstrumentingMW) GetAuthorizations(ctx context.Context, realmID string, groupID string) ([]configuration.Authorization, error) {
defer func(begin time.Time) {
m.h.With("correlation_id", ctx.Value(cs.CtContextCorrelationID).(string)).Observe(time.Since(begin).Seconds())
}(time.Now())
return m.next.GetAuthorizations(ctx, realmID, groupID)
}
// configDBModuleInstrumentingMW implements Module.
func (m *configDBModuleInstrumentingMW) CreateAuthorization(ctx context.Context, auth configuration.Authorization) error {
defer func(begin time.Time) {
m.h.With("correlation_id", ctx.Value(cs.CtContextCorrelationID).(string)).Observe(time.Since(begin).Seconds())
}(time.Now())
return m.next.CreateAuthorization(ctx, auth)
}
// configDBModuleInstrumentingMW implements Module.
func (m *configDBModuleInstrumentingMW) DeleteAuthorizations(ctx context.Context, realmID string, groupID string) error {
defer func(begin time.Time) {
m.h.With("correlation_id", ctx.Value(cs.CtContextCorrelationID).(string)).Observe(time.Since(begin).Seconds())
}(time.Now())
return m.next.DeleteAuthorizations(ctx, realmID, groupID)
}
// configDBModuleInstrumentingMW implements Module.
func (m *configDBModuleInstrumentingMW) DeleteAllAuthorizationsWithGroup(ctx context.Context, realmID, groupName string) error {
defer func(begin time.Time) {
m.h.With("correlation_id", ctx.Value(cs.CtContextCorrelationID).(string)).Observe(time.Since(begin).Seconds())
}(time.Now())
return m.next.DeleteAllAuthorizationsWithGroup(ctx, realmID, groupName)
}