alexcesaro / statsd

An efficient Statsd Go client.
MIT License
301 stars 79 forks source link

Crash when 'Timing' sending data #19

Closed windless0530 closed 4 years ago

windless0530 commented 7 years ago

I am writing a code, using statsd to send data to a localhost statsd server, which transfers data to a Graphite server.

timing := p.latencyStatsd.NewTiming() newsEntryStorage, code := p.dataApiClient.GetNewsEntryStorage(content.EntryId) timing.Send("api.external.data_api.get_news_entry_storage.latency")

However, it crashes as following:

2017/05/08 10:46:08 http: panic serving 172.17.11.206:33475: runtime error: invalid memory address or nil pointer dereference goroutine 555 [running]: net/http.(conn).serve.func1(0xc4201a9500) /usr/local/go/src/net/http/server.go:1491 +0x12a panic(0x709be0, 0xc4200100d0) /usr/local/go/src/runtime/panic.go:458 +0x243 github.com/alexcesaro/statsd.(Client).skip(0x0, 0x410073) /Users/zhangyi/work/go/src/github.com/alexcesaro/statsd/statsd.go:87 +0x22 github.com/alexcesaro/statsd.(Client).Timing(0x0, 0x777762, 0x34, 0x6f0f00, 0xc420197440) /Users/zhangyi/work/go/src/github.com/alexcesaro/statsd/statsd.go:105 +0x2f github.com/alexcesaro/statsd.Timing.Send(0xed0a24370, 0xc41135c985, 0x8d9a80, 0x0, 0x777762, 0x34) /Users/zhangyi/work/go/src/github.com/alexcesaro/statsd/statsd.go:132 +0xd0 opera-news-message/message.(MessageMongoClient).dbMsg2MsgContent(0xc42006c720, 0xc4203783f0, 0x0, 0xc42002a6f8) /Users/zhangyi/work/go/src/opera-news-message/message/mongo_client.go:270 +0xa3a opera-news-message/message.(MessageMongoClient).RequestMessageList(0xc42006c720, 0xc420197a90, 0xc42002a000, 0x8ba640) /Users/zhangyi/work/go/src/opera-news-message/message/mongo_client.go:172 +0xa1c opera-news-message/message.MessageListHandler(0x8be000, 0xc4201815f0, 0xc4204240f0) /Users/zhangyi/work/go/src/opera-news-message/message/handler.go:330 +0x7d1 net/http.HandlerFunc.ServeHTTP(0x796158, 0x8be000, 0xc4201815f0, 0xc4204240f0) /usr/local/go/src/net/http/server.go:1726 +0x44 github.com/go-zoo/bone.(Route).serveMatchedRequest(0xc4203b41e0, 0x8be000, 0xc4201815f0, 0xc420424000, 0xc4203bb620) /Users/zhangyi/work/go/src/github.com/go-zoo/bone/helper_17.go:38 +0x10e github.com/go-zoo/bone.(Route).parse(0xc4203b41e0, 0x8be000, 0xc4201815f0, 0xc420424000, 0xc42040a000) /Users/zhangyi/work/go/src/github.com/go-zoo/bone/route.go:147 +0x168 github.com/go-zoo/bone.(Mux).parse(0xc420393540, 0x8be000, 0xc4201815f0, 0xc420424000, 0xc4203cb080) /Users/zhangyi/work/go/src/github.com/go-zoo/bone/helper.go:22 +0xa0 github.com/go-zoo/bone.(Mux).DefaultServe(0xc420393540, 0x8be000, 0xc4201815f0, 0xc420424000) /Users/zhangyi/work/go/src/github.com/go-zoo/bone/bone.go:54 +0x4d github.com/go-zoo/bone.(Mux).DefaultServe-fm(0x8be000, 0xc4201815f0, 0xc420424000) /Users/zhangyi/work/go/src/github.com/go-zoo/bone/bone.go:40 +0x48 github.com/go-zoo/bone.(Mux).ServeHTTP(0xc420393540, 0x8be000, 0xc4201815f0, 0xc420424000) /Users/zhangyi/work/go/src/github.com/go-zoo/bone/bone.go:70 +0x54 net/http.serverHandler.ServeHTTP(0xc4203aa680, 0x8be000, 0xc4201815f0, 0xc420424000) /usr/local/go/src/net/http/server.go:2202 +0x7d net/http.(conn).serve(0xc4201a9500, 0x8be780, 0xc4203cb080) /usr/local/go/src/net/http/server.go:1579 +0x4b7 created by net/http.(*Server).Serve /usr/local/go/src/net/http/server.go:2293 +0x44d

I don't know if there is anything wrong with the statsd server, but after all, crash is always unreasonable, isn't it.

Expecting an answer.

Thanks all. Best regards.

windless0530 commented 7 years ago

If I fix the codes of statsd like this, the crash would not happen:

diff --git a/statsd.go b/statsd.go index f19204d..69bd4f4 100644 --- a/statsd.go +++ b/statsd.go @@ -84,7 +84,7 @@ func (c *Client) Count(bucket string, n interface{}) { }

func (c *Client) skip() bool { - return c.muted || (c.rate != 1 && randFloat() > c.rate) + return nil == c || c.muted || (c.rate != 1 && randFloat() > c.rate) }

However, I can't figure out why c could be null.

timing := p.latencyStatsd.NewTiming() newsEntryStorage, code := p.dataApiClient.GetNewsEntryStorage(content.EntryId) timing.Send("api.external.data_api.get_news_entry_storage.latency")

The crashed nil 'c' should be 'p.latencyStatsd', however, if it is nil, the crash should happen at the line of 'NewTiming()' instead of 'Send', in my opinion.

So I still dare not to commit the above change.