Skip to content

Commit e51f51e

Browse files
mosajjalfolbricht
andauthored
move from logrus to slog (#422)
* Migrate from logrus to slog * fully removing logrus * should be working now * Update pipeline.go Co-authored-by: Frank Olbricht <[email protected]> * Update response-blocklist-name.go Co-authored-by: Frank Olbricht <[email protected]> * added null logger * Update pipeline.go --------- Co-authored-by: Frank Olbricht <[email protected]>
1 parent 8cb29b5 commit e51f51e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+361
-285
lines changed

adminlistener.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/quic-go/quic-go"
1313
"github.com/quic-go/quic-go/http3"
14-
"github.com/sirupsen/logrus"
1514
)
1615

1716
// Read/Write timeout in the admin server
@@ -65,7 +64,10 @@ func NewAdminListener(id, addr string, opt AdminListenerOptions) (*AdminListener
6564

6665
// Start the admin server.
6766
func (s *AdminListener) Start() error {
68-
Log.WithFields(logrus.Fields{"id": s.id, "protocol": s.opt.Transport, "addr": s.addr}).Info("starting listener")
67+
Log.Info("starting listener",
68+
"id", s.id,
69+
"protocol", s.opt.Transport,
70+
"addr", s.addr)
6971
if s.opt.Transport == "quic" {
7072
return s.startQUIC()
7173
}
@@ -103,7 +105,10 @@ func (s *AdminListener) startQUIC() error {
103105

104106
// Stop the server.
105107
func (s *AdminListener) Stop() error {
106-
Log.WithFields(logrus.Fields{"id": s.id, "protocol": s.opt.Transport, "addr": s.addr}).Info("stopping listener")
108+
Log.Info("stopping listener",
109+
"id", s.id,
110+
"protocol", s.opt.Transport,
111+
"addr", s.addr)
107112
if s.opt.Transport == "quic" {
108113
return s.quicServer.Close()
109114
}

blocklist.go

+22-13
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package rdns
33
import (
44
"errors"
55
"expvar"
6+
"log/slog"
67
"net"
78
"sync"
89
"time"
910

1011
"github.com/miekg/dns"
11-
"github.com/sirupsen/logrus"
1212
)
1313

1414
// Blocklist is a resolver that returns NXDOMAIN or a spoofed IP for every query that
@@ -103,25 +103,33 @@ func (r *Blocklist) Resolve(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
103103
// Forward to upstream or the optional allowlist-resolver immediately if there's a match in the allowlist
104104
if allowlistDB != nil {
105105
if _, _, match, ok := allowlistDB.Match(question); ok {
106-
log = log.WithFields(logrus.Fields{"list": match.List, "rule": match.Rule})
106+
log = log.With(
107+
slog.String("list", match.List),
108+
slog.String("rule", match.Rule),
109+
)
107110
r.metrics.allowed.Add(1)
108111
if r.AllowListResolver != nil {
109-
log.WithField("resolver", r.AllowListResolver.String()).Debug("matched allowlist, forwarding")
112+
log.Debug("matched allowlist, forwarding",
113+
"resolver", r.AllowListResolver.String())
110114
return r.AllowListResolver.Resolve(q, ci)
111115
}
112-
log.WithField("resolver", r.resolver.String()).Debug("matched allowlist, forwarding")
116+
log.Debug("matched allowlist, forwarding",
117+
"resolver", r.resolver.String())
113118
return r.resolver.Resolve(q, ci)
114119
}
115120
}
116121

117122
ips, names, match, ok := blocklistDB.Match(question)
118123
if !ok {
119-
// Didn't match anything, pass it on to the next resolver
120-
log.WithField("resolver", r.resolver.String()).Debug("forwarding unmodified query to resolver")
124+
log.Debug("forwarding unmodified query to resolver",
125+
"resolver", r.resolver.String())
121126
r.metrics.allowed.Add(1)
122127
return r.resolver.Resolve(q, ci)
123128
}
124-
log = log.WithFields(logrus.Fields{"list": match.List, "rule": match.Rule})
129+
log = log.With(
130+
slog.String("list", match.List),
131+
slog.String("rule", match.Rule),
132+
)
125133
r.metrics.blocked.Add(1)
126134

127135
// If we got names for the PTR query, respond to it
@@ -135,7 +143,8 @@ func (r *Blocklist) Resolve(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
135143

136144
// If an optional blocklist-resolver was given, send the query to that instead of returning NXDOMAIN.
137145
if r.BlocklistResolver != nil {
138-
log.WithField("resolver", r.BlocklistResolver.String()).Debug("matched blocklist, forwarding")
146+
log.Debug("matched blocklist, forwarding",
147+
"resolver", r.BlocklistResolver.String())
139148
return r.BlocklistResolver.Resolve(q, ci)
140149
}
141150

@@ -178,7 +187,7 @@ func (r *Blocklist) Resolve(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
178187
// Block the request with NXDOMAIN if there was a match but no valid spoofed IP is given
179188
log.Debug("blocking request")
180189
if err := r.EDNS0EDETemplate.Apply(answer, EDNS0EDEInput{q, match}); err != nil {
181-
log.WithError(err).Error("failed to apply edns0ede template")
190+
log.Error("failed to apply edns0ede template", "error", err)
182191
}
183192
answer.SetRcode(q, dns.RcodeNameError)
184193
return answer, nil
@@ -191,11 +200,11 @@ func (r *Blocklist) String() string {
191200
func (r *Blocklist) refreshLoopBlocklist(refresh time.Duration) {
192201
for {
193202
time.Sleep(refresh)
194-
log := Log.WithField("id", r.id)
203+
log := Log.With(slog.String("id", r.id))
195204
log.Debug("reloading blocklist")
196205
db, err := r.BlocklistDB.Reload()
197206
if err != nil {
198-
log.WithError(err).Error("failed to load rules")
207+
log.Error("failed to load rules", "error", err)
199208
continue
200209
}
201210
r.mu.Lock()
@@ -206,11 +215,11 @@ func (r *Blocklist) refreshLoopBlocklist(refresh time.Duration) {
206215
func (r *Blocklist) refreshLoopAllowlist(refresh time.Duration) {
207216
for {
208217
time.Sleep(refresh)
209-
log := Log.WithField("id", r.id)
218+
log := Log.With(slog.String("id", r.id))
210219
log.Debug("reloading allowlist")
211220
db, err := r.AllowlistDB.Reload()
212221
if err != nil {
213-
log.WithError(err).Error("failed to load rules")
222+
log.Error("failed to load rules", "error", err)
214223
continue
215224
}
216225
r.mu.Lock()

blocklistloader-http.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ func NewHTTPLoader(url string, opt HTTPLoaderOptions) *HTTPLoader {
3737
}
3838

3939
func (l *HTTPLoader) Load() (rules []string, err error) {
40-
log := Log.WithField("url", l.url)
41-
log.Trace("loading blocklist")
40+
log := Log.With("url", l.url)
41+
log.Debug("loading blocklist")
4242

4343
// If AllowFailure is enabled, return the last successfully loaded list
4444
// and nil
4545
defer func() {
4646
if err != nil && l.opt.AllowFailure {
47-
log.WithError(err).Warn("failed to load blocklist, continuing with previous ruleset")
47+
log.Warn("failed to load blocklist, continuing with previous ruleset",
48+
"error", err)
4849
rules = l.lastSuccess
4950
err = nil
5051
} else {
@@ -58,10 +59,11 @@ func (l *HTTPLoader) Load() (rules []string, err error) {
5859
l.fromDisk = false
5960
rules, err := l.loadFromDisk()
6061
if err == nil {
61-
log.WithField("load-time", time.Since(start)).Trace("loaded blocklist from cache-dir")
62+
log.With("load-time", time.Since(start)).Debug("loaded blocklist from cache-dir")
6263
return rules, err
6364
}
64-
log.WithError(err).Warn("unable to load cached list from disk, loading from upstream")
65+
log.Warn("unable to load cached list from disk, loading from upstream",
66+
"error", err)
6567
}
6668

6769
ctx, cancel := context.WithTimeout(context.Background(), httpTimeout)
@@ -87,13 +89,13 @@ func (l *HTTPLoader) Load() (rules []string, err error) {
8789
for scanner.Scan() {
8890
rules = append(rules, scanner.Text())
8991
}
90-
log.WithField("load-time", time.Since(start)).Trace("completed loading blocklist")
92+
log.With("load-time", time.Since(start)).Debug("completed loading blocklist")
9193

9294
// Cache the content to disk if the read from the remote server was successful
9395
if scanner.Err() == nil && l.opt.CacheDir != "" {
94-
log.Trace("writing rules to cache-dir")
96+
log.Debug("writing rules to cache-dir")
9597
if err := l.writeToDisk(rules); err != nil {
96-
log.WithError(err).Error("failed to write rules to cache")
98+
Log.Error("failed to write rules to cache", "error", err)
9799
}
98100
}
99101
return rules, scanner.Err()

blocklistloader-local.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ func NewFileLoader(filename string, opt FileLoaderOptions) *FileLoader {
2626
}
2727

2828
func (l *FileLoader) Load() (rules []string, err error) {
29-
log := Log.WithField("file", l.filename)
30-
log.Trace("loading blocklist")
29+
log := Log.With("file", l.filename)
30+
log.Debug("loading blocklist")
3131

3232
// If AllowFailure is enabled, return the last successfully loaded list
3333
// and nil
3434
defer func() {
3535
if err != nil && l.opt.AllowFailure {
36-
log.WithError(err).Warn("failed to load blocklist, continuing with previous ruleset")
36+
log.Warn("failed to load blocklist, continuing with previous ruleset",
37+
"error", err)
3738
rules = l.lastSuccess
3839
err = nil
3940
} else {
@@ -50,6 +51,6 @@ func (l *FileLoader) Load() (rules []string, err error) {
5051
for scanner.Scan() {
5152
rules = append(rules, scanner.Text())
5253
}
53-
log.Trace("completed loading blocklist")
54+
log.Debug("completed loading blocklist")
5455
return rules, scanner.Err()
5556
}

cache-memory.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"sync"
66
"time"
77

8+
"log/slog"
9+
810
"github.com/miekg/dns"
9-
"github.com/sirupsen/logrus"
1011
)
1112

1213
type memoryBackend struct {
@@ -142,7 +143,12 @@ func (b *memoryBackend) startGC(period time.Duration) {
142143
total = b.lru.size()
143144
b.mu.Unlock()
144145

145-
Log.WithFields(logrus.Fields{"total": total, "removed": removed}).Trace("cache garbage collection")
146+
Log.Debug("cache garbage collection",
147+
slog.Group("details",
148+
slog.Int("total", total),
149+
slog.Int("removed", removed),
150+
),
151+
)
146152
}
147153
}
148154

@@ -162,17 +168,17 @@ func (b *memoryBackend) Close() error {
162168
func (b *memoryBackend) writeToFile(filename string) error {
163169
b.mu.Lock()
164170
defer b.mu.Unlock()
165-
log := Log.WithField("filename", filename)
171+
log := Log.With("filename", filename)
166172
log.Info("writing cache file")
167173
f, err := os.Create(filename)
168174
if err != nil {
169-
log.WithError(err).Warn("failed to create cache file")
175+
log.Warn("failed to create cache file", "error", err)
170176
return err
171177
}
172178
defer f.Close()
173179

174180
if err := b.lru.serialize(f); err != nil {
175-
log.WithError(err).Warn("failed to persist cache to disk")
181+
log.Warn("failed to persist cache to disk", "error", err)
176182
return err
177183
}
178184
return nil
@@ -181,17 +187,17 @@ func (b *memoryBackend) writeToFile(filename string) error {
181187
func (b *memoryBackend) loadFromFile(filename string) error {
182188
b.mu.Lock()
183189
defer b.mu.Unlock()
184-
log := Log.WithField("filename", filename)
190+
log := Log.With("filename", filename)
185191
log.Info("reading cache file")
186192
f, err := os.Open(filename)
187193
if err != nil {
188-
log.WithError(err).Warn("failed to open cache file")
194+
log.Warn("failed to open cache file", "error", err)
189195
return err
190196
}
191197
defer f.Close()
192198

193199
if err := b.lru.deserialize(f); err != nil {
194-
log.WithError(err).Warn("failed to read cache from disk")
200+
log.Warn("failed to read cache from disk", "error", err)
195201
return err
196202
}
197203
return nil

cache-redis.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ func (b *redisBackend) Store(query *dns.Msg, item *cacheAnswer) {
3838
key := b.keyFromQuery(query)
3939
value, err := json.Marshal(item)
4040
if err != nil {
41-
Log.WithError(err).Error("failed to marshal cache record")
41+
Log.Error("failed to marshal cache record", "error", err)
4242
return
4343
}
4444
if err := b.client.Set(ctx, key, value, time.Until(item.Expiry)).Err(); err != nil {
45-
Log.WithError(err).Error("failed to write to redis")
45+
Log.Error("failed to write to redis", "error", err)
4646
}
4747
}
4848

@@ -55,12 +55,12 @@ func (b *redisBackend) Lookup(q *dns.Msg) (*dns.Msg, bool, bool) {
5555
if errors.Is(err, redis.Nil) { // Return a cache-miss if there's no such key
5656
return nil, false, false
5757
}
58-
Log.WithError(err).Error("failed to read from redis")
58+
Log.Error("failed to read from redis", "error", err)
5959
return nil, false, false
6060
}
6161
var a *cacheAnswer
6262
if err := json.Unmarshal([]byte(value), &a); err != nil {
63-
Log.WithError(err).Error("failed to unmarshal cache record from redis")
63+
Log.Error("failed to unmarshal cache record from redis", "error", err)
6464
return nil, false, false
6565
}
6666

@@ -95,7 +95,7 @@ func (b *redisBackend) Flush() {
9595
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
9696
defer cancel()
9797
if _, err := b.client.Del(ctx, b.opt.KeyPrefix+"*").Result(); err != nil {
98-
Log.WithError(err).Error("failed to delete keys in redis")
98+
Log.Error("failed to delete keys in redis", "error", err)
9999
}
100100
}
101101

@@ -104,7 +104,7 @@ func (b *redisBackend) Size() int {
104104
defer cancel()
105105
size, err := b.client.DBSize(ctx).Result()
106106
if err != nil {
107-
Log.WithError(err).Error("failed to run dbsize command on redis")
107+
Log.Error("failed to run dbsize command on redis", "error", err)
108108
}
109109
return int(size)
110110
}

cache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func (r *Cache) Resolve(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
193193
}
194194
r.metrics.miss.Add(1)
195195

196-
log.WithField("resolver", r.resolver.String()).Debug("cache-miss, forwarding")
196+
log.With("resolver", r.resolver.String()).Debug("cache-miss, forwarding")
197197

198198
// Get a response from upstream
199199
a, err := r.resolver.Resolve(q.Copy(), ci)

client-blocklist.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"sync"
55
"time"
66

7+
"log/slog"
8+
79
"github.com/miekg/dns"
8-
"github.com/sirupsen/logrus"
910
)
1011

1112
// ClientBlocklist is a resolver that matches the IPs of clients against a blocklist
@@ -50,10 +51,18 @@ func NewClientBlocklist(id string, resolver Resolver, opt ClientBlocklistOptions
5051
// resolver if one is configured.
5152
func (r *ClientBlocklist) Resolve(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
5253
if match, ok := r.BlocklistDB.Match(ci.SourceIP); ok {
53-
log := Log.WithFields(logrus.Fields{"id": r.id, "qname": qName(q), "list": match.List, "rule": match.Rule, "ip": ci.SourceIP})
54+
log := Log.With(
55+
slog.String("id", r.id),
56+
slog.String("qname", qName(q)),
57+
slog.String("list", match.List),
58+
slog.String("rule", match.Rule),
59+
slog.String("ip", ci.SourceIP.String()),
60+
)
5461
r.metrics.blocked.Add(1)
5562
if r.BlocklistResolver != nil {
56-
log.WithField("resolver", r.BlocklistResolver).Debug("client on blocklist, forwarding to blocklist-resolver")
63+
log.With(
64+
slog.String("resolver", r.BlocklistResolver.String()),
65+
).Debug("client on blocklist, forwarding to blocklist-resolver")
5766
return r.BlocklistResolver.Resolve(q, ci)
5867
}
5968
log.Debug("blocking client")
@@ -71,11 +80,14 @@ func (r *ClientBlocklist) String() string {
7180
func (r *ClientBlocklist) refreshLoopBlocklist(refresh time.Duration) {
7281
for {
7382
time.Sleep(refresh)
74-
log := Log.WithField("id", r.id)
83+
log := Log.With(
84+
slog.String("id", r.id),
85+
)
7586
log.Debug("reloading blocklist")
7687
db, err := r.BlocklistDB.Reload()
7788
if err != nil {
78-
Log.WithError(err).Error("failed to load rules")
89+
log.Error("failed to load rules",
90+
"error", err)
7991
continue
8092
}
8193
r.mu.Lock()

0 commit comments

Comments
 (0)