Browse Source

[tm-monitor] rewrite eventmeter to use go-kit/log

pull/1943/head
Anton Kaliaev 8 years ago
parent
commit
dee06a92d9
No known key found for this signature in database GPG Key ID: 7B6881D965918214
4 changed files with 17 additions and 8 deletions
  1. +13
    -7
      tm-monitor/eventmeter/eventmeter.go
  2. +0
    -1
      tm-monitor/glide.yaml
  3. +2
    -0
      tm-monitor/mock/mock.go
  4. +2
    -0
      tm-monitor/monitor/node.go

+ 13
- 7
tm-monitor/eventmeter/eventmeter.go View File

@ -6,16 +6,14 @@ import (
"sync"
"time"
"github.com/go-kit/kit/log"
"github.com/gorilla/websocket"
"github.com/pkg/errors"
metrics "github.com/rcrowley/go-metrics"
events "github.com/tendermint/go-events"
client "github.com/tendermint/go-rpc/client"
log15 "github.com/tendermint/log15"
)
// Log allows you to set your own logger.
var Log log15.Logger
//------------------------------------------------------
// Generic system to subscribe to events and record their frequency
//------------------------------------------------------
@ -96,6 +94,8 @@ type EventMeter struct {
unmarshalEvent EventUnmarshalFunc
quit chan struct{}
logger log.Logger
}
func NewEventMeter(addr string, unmarshalEvent EventUnmarshalFunc) *EventMeter {
@ -106,10 +106,16 @@ func NewEventMeter(addr string, unmarshalEvent EventUnmarshalFunc) *EventMeter {
receivedPong: true,
unmarshalEvent: unmarshalEvent,
quit: make(chan struct{}),
logger: log.NewNopLogger(),
}
return em
}
// SetLogger lets you set your own logger
func (em *EventMeter) SetLogger(l log.Logger) {
em.logger = l
}
func (em *EventMeter) String() string {
return em.wsc.Address
}
@ -225,11 +231,11 @@ func (em *EventMeter) receiveRoutine() {
select {
case <-pingTicker.C:
if pingAttempts, err = em.pingForLatency(pingAttempts); err != nil {
Log.Error("Failed to write ping message on websocket", err)
em.logger.Log("err", errors.Wrap(err, "Failed to write ping message on websocket"))
em.StopAndReconnect()
return
} else if pingAttempts >= maxPingsPerPong {
Log.Error(fmt.Sprintf("Have not received a pong in %v", time.Duration(pingAttempts)*pingTime))
em.logger.Log("err", errors.Errorf("Have not received a pong in %v", time.Duration(pingAttempts)*pingTime))
em.StopAndReconnect()
return
}
@ -240,7 +246,7 @@ func (em *EventMeter) receiveRoutine() {
}
eventID, data, err := em.unmarshalEvent(r)
if err != nil {
Log.Error(err.Error())
em.logger.Log("err", err)
continue
}
if eventID != "" {


+ 0
- 1
tm-monitor/glide.yaml View File

@ -15,7 +15,6 @@ import:
- package: github.com/tendermint/go-rpc
subpackages:
- client
- package: github.com/tendermint/log15
- package: github.com/go-kit/kit
subpackages:
- log


+ 2
- 0
tm-monitor/mock/mock.go View File

@ -4,6 +4,7 @@ import (
"log"
"reflect"
gokitlog "github.com/go-kit/kit/log"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
em "github.com/tendermint/tools/tm-monitor/eventmeter"
)
@ -16,6 +17,7 @@ type EventMeter struct {
func (e *EventMeter) Start() error { return nil }
func (e *EventMeter) Stop() {}
func (e *EventMeter) SetLogger(l gokitlog.Logger) {}
func (e *EventMeter) RegisterLatencyCallback(cb em.LatencyCallbackFunc) { e.latencyCallback = cb }
func (e *EventMeter) RegisterDisconnectCallback(cb em.DisconnectCallbackFunc) {
e.disconnectCallback = cb


+ 2
- 0
tm-monitor/monitor/node.go View File

@ -98,6 +98,7 @@ func (n *Node) NotifyAboutDisconnects(ch chan<- bool) {
// SetLogger lets you set your own logger
func (n *Node) SetLogger(l log.Logger) {
n.logger = l
n.em.SetLogger(l)
}
func (n *Node) Start() error {
@ -265,6 +266,7 @@ type eventMeter interface {
RegisterDisconnectCallback(em.DisconnectCallbackFunc)
Subscribe(string, em.EventCallbackFunc) error
Unsubscribe(string) error
SetLogger(l log.Logger)
}
// UnmarshalEvent unmarshals a json event


Loading…
Cancel
Save