Skip to content

Commit dc4c064

Browse files
authored
Merge pull request #244 from signalfx/gan858
Make zk libs less chatty, but can be enabled if you wish to.
2 parents 0350d7c + eed1d27 commit dc4c064

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

zkplus/zkplus.go

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@ import (
1313
"github.com/signalfx/golib/v3/zkplus/zktest"
1414
)
1515

16+
const (
17+
// DEBUG all the annoying zk message
18+
DEBUG = iota
19+
// NORMAL enough to be useful (he says)
20+
NORMAL
21+
)
22+
23+
// LogLevel sets the log level of this module
24+
// I'm sorry to have to put this here but i just can't deal with rewriting the entire
25+
// log library or swapping it out for zap at the moment. sorry.
26+
var LogLevel = NORMAL
27+
28+
func normalit(logger log.Logger, keyvals ...interface{}) {
29+
if LogLevel <= NORMAL {
30+
logger.Log(keyvals...)
31+
}
32+
}
33+
34+
func debugit(logger log.Logger, keyvals ...interface{}) {
35+
if LogLevel <= DEBUG {
36+
logger.Log(keyvals...)
37+
}
38+
}
39+
1640
// ZkConnector tells ZkPlus how to create a zk connection
1741
type ZkConnector interface {
1842
Conn() (zktest.ZkConnSupported, <-chan zk.Event, error)
@@ -129,7 +153,7 @@ func (z *ZkPlus) eventLoop() {
129153
for {
130154
select {
131155
case eventToSend = <-whenI(!haveEventToSend && z.connectedChan != nil, z.connectedChan):
132-
z.logger.Log(logkey.ZkEvent, eventToSend, logkey.ZkPrefix, z.pathPrefix, log.Msg, "ZK node modification event")
156+
normalit(z.logger, logkey.ZkEvent, eventToSend, logkey.ZkPrefix, z.pathPrefix, log.Msg, "ZK node modification event")
133157
if strings.HasPrefix(eventToSend.Path, z.pathPrefix) {
134158
eventToSend.Path = eventToSend.Path[len(z.pathPrefix):]
135159
if eventToSend.Path == "" {
@@ -158,7 +182,7 @@ func (z *ZkPlus) onQuit(c chan struct{}) {
158182
z.connectedConn.Close()
159183
z.connectedConn = nil
160184
}
161-
z.logger.Log("Close on event loop")
185+
normalit(z.logger, "Close on event loop")
162186
}
163187

164188
func (z *ZkPlus) setupConn() {
@@ -169,7 +193,7 @@ func (z *ZkPlus) setupConn() {
169193
z.connectedConn = c
170194
z.connectedChan = e
171195
if err := z.ensureRootPath(c); err != nil {
172-
z.logger.Log("err", err, "Unable to ensure root path")
196+
normalit(z.logger, "err", err, "Unable to ensure root path")
173197
z.connectedConn.Close()
174198
z.connectedConn = nil
175199
z.connectedChan = nil
@@ -205,50 +229,50 @@ func (z *ZkPlus) blockOnConn() zktest.ZkConnSupported {
205229

206230
// Exists returns true if the path exists
207231
func (z *ZkPlus) Exists(path string) (bool, *zk.Stat, error) {
208-
z.forPath(path).Log(logkey.ZkMethod, "Exists")
232+
debugit(z.forPath(path), logkey.ZkMethod, "Exists")
209233
return z.blockOnConn().Exists(z.realPath(path))
210234
}
211235

212236
// ExistsW is like Exists but also sets a watch. Note: We DO NOT change paths on the returned
213237
// channel nor do we reconnect it. Use the global channel instead
214238
func (z *ZkPlus) ExistsW(path string) (bool, *zk.Stat, <-chan zk.Event, error) {
215-
z.forPath(path).Log(logkey.ZkMethod, "ExistsW")
239+
debugit(z.forPath(path), logkey.ZkMethod, "ExistsW")
216240
return z.blockOnConn().ExistsW(z.realPath(path))
217241
}
218242

219243
// Get the bytes of a zk path
220244
func (z *ZkPlus) Get(path string) ([]byte, *zk.Stat, error) {
221-
z.forPath(path).Log(logkey.ZkMethod, "Get")
245+
debugit(z.forPath(path), logkey.ZkMethod, "Get")
222246
return z.blockOnConn().Get(z.realPath(path))
223247
}
224248

225249
// GetW is like Get, but also sets a watch
226250
func (z *ZkPlus) GetW(path string) ([]byte, *zk.Stat, <-chan zk.Event, error) {
227-
z.forPath(path).Log(logkey.ZkMethod, "GetW")
251+
debugit(z.forPath(path), logkey.ZkMethod, "GetW")
228252
return z.blockOnConn().GetW(z.realPath(path))
229253
}
230254

231255
// Children gets children of a path
232256
func (z *ZkPlus) Children(path string) ([]string, *zk.Stat, error) {
233-
z.forPath(path).Log(logkey.ZkMethod, "Children")
257+
debugit(z.forPath(path), logkey.ZkMethod, "Children")
234258
return z.blockOnConn().Children(z.realPath(path))
235259
}
236260

237261
// ChildrenW is like children but also sets a watch
238262
func (z *ZkPlus) ChildrenW(path string) ([]string, *zk.Stat, <-chan zk.Event, error) {
239-
z.forPath(path).Log(logkey.ZkMethod, "ChildrenW")
263+
debugit(z.forPath(path), logkey.ZkMethod, "ChildrenW")
240264
return z.blockOnConn().ChildrenW(z.realPath(path))
241265
}
242266

243267
// Delete a Zk node
244268
func (z *ZkPlus) Delete(path string, version int32) error {
245-
z.forPath(path).Log(logkey.ZkMethod, "Delete")
269+
normalit(z.forPath(path), logkey.ZkMethod, "Delete")
246270
return z.blockOnConn().Delete(z.realPath(path), version)
247271
}
248272

249273
// Create a Zk node
250274
func (z *ZkPlus) Create(path string, data []byte, flags int32, acl []zk.ACL) (string, error) {
251-
z.forPath(path).Log(logkey.ZkMethod, "Create")
275+
normalit(z.forPath(path), logkey.ZkMethod, "Create")
252276
p, err := z.blockOnConn().Create(z.realPath(path), data, flags, acl)
253277
if strings.HasPrefix(p, z.pathPrefix) && z.pathPrefix != "" {
254278
p = p[len(z.pathPrefix)+1:]
@@ -258,7 +282,7 @@ func (z *ZkPlus) Create(path string, data []byte, flags int32, acl []zk.ACL) (st
258282

259283
// Set the data of a zk node
260284
func (z *ZkPlus) Set(path string, data []byte, version int32) (*zk.Stat, error) {
261-
z.forPath(path).Log(logkey.ZkMethod, "Set")
285+
normalit(z.forPath(path), logkey.ZkMethod, "Set")
262286
return z.blockOnConn().Set(z.realPath(path), data, version)
263287
}
264288

zkplus/zkplus_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,14 @@ func TestBadConnection(t *testing.T) {
243243
assert.Nil(t, conn)
244244
z.Close()
245245
}
246+
247+
func TestLogger(t *testing.T) {
248+
logger := &log.Counter{}
249+
normalit(logger, "normal level")
250+
assert.Equal(t, logger.Count, int64(1))
251+
debugit(logger, "debug level")
252+
assert.Equal(t, logger.Count, int64(1))
253+
LogLevel = DEBUG
254+
debugit(logger, "debug level")
255+
assert.Equal(t, logger.Count, int64(2))
256+
}

0 commit comments

Comments
 (0)