/
module.go
64 lines (53 loc) · 1.47 KB
/
module.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
package event
import (
"context"
"github.com/cloudtrust/common-service/log"
"github.com/cloudtrust/common-service/metrics"
influx "github.com/influxdata/influxdb/client/v2"
)
// ConsoleModule is the interface of the console module.
type ConsoleModule interface {
Print(context.Context, map[string]string) error
}
type consoleModule struct {
logger log.Logger
}
// NewConsoleModule returns a Console module.
func NewConsoleModule(logger log.Logger) ConsoleModule {
return &consoleModule{
logger: logger,
}
}
func (cm *consoleModule) Print(ctx context.Context, m map[string]string) error {
// Log
for k, v := range m {
cm.logger.Info(ctx, k, v)
}
return nil
}
// StatisticModule is the interface of the keycloak statistic module.
type StatisticModule interface {
Stats(context.Context, map[string]string) error
}
// Influx is the influx DB interface.
type Influx interface {
Write(bp influx.BatchPoints) error
Close()
}
type statisticModule struct {
influx metrics.Metrics
}
//NewStatisticModule returns a Statistic module.
func NewStatisticModule(influx metrics.Metrics) StatisticModule {
return &statisticModule{
influx: influx,
}
}
func (sm *statisticModule) Stats(ctx context.Context, m map[string]string) error {
// Create a point and add to batch
var tags = map[string]string{"type": m["type"], "realm": m["realmId"], "userId": m["userId"]}
var fields = map[string]interface{}{
"uid": m["uid"],
}
return sm.influx.Stats(ctx, "event_statistics", tags, fields)
}