-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
74 lines (64 loc) · 1.66 KB
/
plugin.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
package pluginPersistency
import (
"strings"
"github.com/log-rush/distribution-server/domain"
"github.com/log-rush/distribution-server/pkg/app"
"github.com/log-rush/distribution-server/pkg/devkit"
)
type Config struct {
Adapter StorageAdapter
LogDelimiter string
StreamsBlacklist []string
StreamsWhitelist []string
ExposeLogHistory bool
}
type PersistencyPlugin struct {
config Config
logPlugin *logPlugin
routerPlugin *routerPlugin
Plugin app.Plugin
}
func NewPersistencyPlugin(config Config) PersistencyPlugin {
plugin := PersistencyPlugin{
config: config,
logPlugin: newLogPlugin(config),
routerPlugin: newRouterPlugin(config),
}
p := devkit.NewPlugin(
"persistency",
devkit.PluginHandlers{
LogHandler: plugin.logPlugin.HandleLog,
RouterHandler: plugin.routerPlugin.SetupRouter,
},
devkit.PluginHooks{
OnDeInit: func(context *app.Context) {
plugin.logPlugin.config.Adapter.Shutdown()
},
},
)
plugin.Plugin = p
return plugin
}
type logPlugin struct {
config Config
allowList map[string]bool
}
func newLogPlugin(config Config) *logPlugin {
allowList := map[string]bool{}
for _, whiteListed := range config.StreamsWhitelist {
allowList[whiteListed] = true
}
for _, blackListed := range config.StreamsBlacklist {
allowList[blackListed] = false
}
return &logPlugin{config, allowList}
}
func (p *logPlugin) HandleLog(log domain.Log) {
if allowed, ok := p.allowList[log.Stream]; !ok || allowed {
message := log.Message
if !strings.HasSuffix(message, p.config.LogDelimiter) {
message = message + p.config.LogDelimiter
}
p.config.Adapter.AppendLogs(log.Stream, message)
}
}