Skip to content

Commit 5ccccee

Browse files
committed
Add an enabled flag to filter config for better control.
1 parent c0967d1 commit 5ccccee

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

config.sample.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ request_timeout = "100ms"
117117

118118
# Custom go-plugin filter to load to filter messages when relaying
119119
[filters.test]
120+
enabled = false
121+
path = "test.bin"
120122
config = '''
121123
{
122124
"address": ["127.0.0.1:6379"],
@@ -130,4 +132,4 @@ config = '''
130132
"write_timeout": 3000,
131133
"idle_timeout": 30000
132134
}
133-
'''
135+
'''

init.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"log/slog"
99
"net/http"
1010
"os"
11-
"path/filepath"
1211
"plugin"
1312
"strconv"
1413
"strings"
@@ -196,13 +195,21 @@ func initKafkaConfig(ko *koanf.Koanf) ([]relay.ConsumerGroupCfg, relay.ProducerC
196195

197196
// initFilters loads the go plugin, initializes it and return a map of filter plugins.
198197
func initFilters(ko *koanf.Koanf, lo *slog.Logger) (map[string]filter.Provider, error) {
198+
if ko.String("mode") != "single" {
199+
log.Fatalf("filters can only be used in `single` mode.")
200+
}
201+
199202
out := make(map[string]filter.Provider)
200-
for _, name := range ko.MapKeys("filters") {
201-
plg, err := plugin.Open(name)
203+
for _, id := range ko.MapKeys("filters") {
204+
if !ko.Bool("filters." + id + ".enabled") {
205+
continue
206+
}
207+
208+
path := ko.String("filters." + id + ".path")
209+
plg, err := plugin.Open(path)
202210
if err != nil {
203-
return nil, fmt.Errorf("error loading provider plugin '%s': %v", name, err)
211+
return nil, fmt.Errorf("error loading provider plugin '%s': %s: %v", id, path, err)
204212
}
205-
id := strings.TrimSuffix(filepath.Base(name), filepath.Ext(name))
206213

207214
newFunc, err := plg.Lookup("New")
208215
if err != nil {
@@ -215,7 +222,7 @@ func initFilters(ko *koanf.Koanf, lo *slog.Logger) (map[string]filter.Provider,
215222

216223
var cfg filter.Config
217224
if err := ko.Unmarshal("filter."+id, &cfg); err != nil {
218-
log.Fatalf("error unmarshalling filter config: %s: %v", name, err)
225+
log.Fatalf("error unmarshalling filter config: %s: %v", id, err)
219226
}
220227
if cfg.Config == "" {
221228
lo.Info(fmt.Sprintf("WARNING: No config 'filter.%s' for '%s' in config", id, id))
@@ -226,7 +233,7 @@ func initFilters(ko *koanf.Koanf, lo *slog.Logger) (map[string]filter.Provider,
226233
if err != nil {
227234
return nil, fmt.Errorf("error initializing filter provider plugin '%s': %v", id, err)
228235
}
229-
lo.Info(fmt.Sprintf("loaded filter provider plugin '%s' from %s", id, name))
236+
lo.Info(fmt.Sprintf("loaded filter provider plugin '%s' from %s", id, id))
230237

231238
p, ok := prov.(filter.Provider)
232239
if !ok {

0 commit comments

Comments
 (0)