Refactor lotus tracer to behave like lp2p tracer

This commit is contained in:
Matija Petrunic 2021-09-15 13:41:56 +02:00
parent ab86c5419d
commit 8a657b90a7

View File

@ -2,34 +2,66 @@ package tracer
import (
"encoding/json"
"time"
logging "github.com/ipfs/go-log/v2"
peer "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb"
)
var log = logging.Logger("lotus-tracer")
func newLotusTracer(et pubsub.EventTracer, tt TracerTransport) LotusTracer {
func NewLotusTracer(tt TracerTransport, pid peer.ID) LotusTracer {
return &lotusTracer{
et: et,
tt: tt,
pid: pid,
}
}
type lotusTracer struct {
et pubsub.EventTracer
tt TracerTransport
pid peer.ID
}
const (
TraceEvent_PEER_SCORES pubsub_pb.TraceEvent_Type = 100
)
type LotusTraceEvent struct {
Type pubsub_pb.TraceEvent_Type `json:"type,omitempty"`
PeerID []byte `json:"peerID,omitempty"`
Timestamp *int64 `json:"timestamp,omitempty"`
PeerScores *TraceEvent_PeerScores `json:"peerScores,omitempty"`
}
type TraceEvent_PeerScores struct {
Scores map[peer.ID]*pubsub.PeerScoreSnapshot `json:"scores,omitempty"`
}
type LotusTracer interface {
TracePeerScore(scores map[peer.ID]*pubsub.PeerScoreSnapshot)
Trace(evt *pubsub_pb.TraceEvent)
TraceLotusEvent(evt *LotusTraceEvent)
PeerScores(scores map[peer.ID]*pubsub.PeerScoreSnapshot)
}
func (lt *lotusTracer) TracePeerScore(scores map[peer.ID]*pubsub.PeerScoreSnapshot) {
jsonEvent, err := json.Marshal(scores)
func (lt *lotusTracer) PeerScores(scores map[peer.ID]*pubsub.PeerScoreSnapshot) {
now := time.Now().UnixNano()
evt := &LotusTraceEvent{
Type: *TraceEvent_PEER_SCORES.Enum(),
PeerID: []byte(lt.pid),
Timestamp: &now,
PeerScores: &TraceEvent_PeerScores{
Scores: scores,
},
}
lt.TraceLotusEvent(evt)
}
func (lt *lotusTracer) TraceLotusEvent(evt *LotusTraceEvent) {
jsonEvent, err := json.Marshal(evt)
if err != nil {
log.Errorf("error while marshaling peer score: %s", err)
return