Skip to content

Commit eaf5270

Browse files
author
Qiao Liang
committed
allow basic logging for http Get err with RegisterErrLogFn
rename to follow hook function name convention, log more ctx info
1 parent 72d04f9 commit eaf5270

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

groupcache.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package groupcache
2626

2727
import (
2828
"errors"
29+
"fmt"
2930
"math/rand"
3031
"strconv"
3132
"sync"
@@ -60,6 +61,7 @@ var (
6061

6162
initPeerServerOnce sync.Once
6263
initPeerServer func()
64+
errLogHook func(error)
6365
)
6466

6567
// GetGroup returns the named group previously created with NewGroup, or
@@ -121,6 +123,20 @@ func RegisterNewGroupHook(fn func(*Group)) {
121123
newGroupHook = fn
122124
}
123125

126+
// RegisterErrLogHook registers a log func that is used for HTTP error logging.
127+
func RegisterErrLogHook(fn func(error)) {
128+
if errLogHook != nil {
129+
panic("RegisterErrLogHook called more than once")
130+
}
131+
errLogHook = fn
132+
}
133+
134+
func logErr(err error) {
135+
if errLogHook != nil {
136+
errLogHook(err)
137+
}
138+
}
139+
124140
// RegisterServerStart registers a hook that is run when the first
125141
// group is created.
126142
func RegisterServerStart(fn func()) {
@@ -271,10 +287,7 @@ func (g *Group) load(ctx Context, key string, dest Sink) (value ByteView, destPo
271287
return value, nil
272288
}
273289
g.Stats.PeerErrors.Add(1)
274-
// TODO(bradfitz): log the peer's error? keep
275-
// log of the past few for /groupcachez? It's
276-
// probably boring (normal task movement), so not
277-
// worth logging I imagine.
290+
logErr(fmt.Errorf("%+v, %+v, %+v, %+v", ctx, peer, key, err))
278291
}
279292
value, err = g.getLocally(ctx, key, dest)
280293
if err != nil {

0 commit comments

Comments
 (0)