2021-09-10 10:36:38 +00:00
|
|
|
package tracer
|
2021-08-30 12:35:15 +00:00
|
|
|
|
|
|
|
import (
|
2021-09-10 13:37:38 +00:00
|
|
|
"encoding/json"
|
2021-09-15 11:41:56 +00:00
|
|
|
"time"
|
2021-09-10 13:37:38 +00:00
|
|
|
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2021-09-15 11:41:56 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2021-08-30 12:35:15 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb"
|
|
|
|
)
|
|
|
|
|
2021-09-10 13:37:38 +00:00
|
|
|
var log = logging.Logger("lotus-tracer")
|
|
|
|
|
2021-09-15 11:41:56 +00:00
|
|
|
func NewLotusTracer(tt TracerTransport, pid peer.ID) LotusTracer {
|
2021-08-30 12:35:15 +00:00
|
|
|
return &lotusTracer{
|
2021-09-15 11:41:56 +00:00
|
|
|
tt: tt,
|
|
|
|
pid: pid,
|
2021-08-30 12:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type lotusTracer struct {
|
2021-09-15 11:41:56 +00:00
|
|
|
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"`
|
2021-08-30 12:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LotusTracer interface {
|
|
|
|
Trace(evt *pubsub_pb.TraceEvent)
|
2021-09-15 11:41:56 +00:00
|
|
|
TraceLotusEvent(evt *LotusTraceEvent)
|
|
|
|
|
|
|
|
PeerScores(scores map[peer.ID]*pubsub.PeerScoreSnapshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-08-30 12:35:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 11:41:56 +00:00
|
|
|
func (lt *lotusTracer) TraceLotusEvent(evt *LotusTraceEvent) {
|
|
|
|
jsonEvent, err := json.Marshal(evt)
|
2021-09-10 13:37:38 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while marshaling peer score: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = lt.tt.Transport(jsonEvent)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while transporting peer scores: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lt *lotusTracer) Trace(evt *pubsub_pb.TraceEvent) {
|
|
|
|
jsonEvent, err := json.Marshal(evt)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while marshaling tracer event: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2021-08-30 12:35:15 +00:00
|
|
|
|
2021-09-10 13:37:38 +00:00
|
|
|
err = lt.tt.Transport(jsonEvent)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while transporting trace event: %s", err)
|
|
|
|
}
|
|
|
|
}
|