2021-09-10 10:36:38 +00:00
|
|
|
package tracer
|
2021-08-30 12:35:15 +00:00
|
|
|
|
|
|
|
import (
|
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-16 09:51:59 +00:00
|
|
|
func NewLotusTracer(tt []TracerTransport, pid peer.ID, sourceAuth string) LotusTracer {
|
2021-08-30 12:35:15 +00:00
|
|
|
return &lotusTracer{
|
2021-09-15 11:41:56 +00:00
|
|
|
tt: tt,
|
|
|
|
pid: pid,
|
2021-09-16 09:51:59 +00:00
|
|
|
sa: sourceAuth,
|
2021-08-30 12:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type lotusTracer struct {
|
2021-09-15 12:50:27 +00:00
|
|
|
tt []TracerTransport
|
2021-09-15 11:41:56 +00:00
|
|
|
pid peer.ID
|
2021-09-16 09:51:59 +00:00
|
|
|
sa string
|
2021-09-15 11:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-09-29 11:12:42 +00:00
|
|
|
TraceEventPeerScores pubsub_pb.TraceEvent_Type = 100
|
2021-09-15 11:41:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type LotusTraceEvent struct {
|
|
|
|
Type pubsub_pb.TraceEvent_Type `json:"type,omitempty"`
|
2021-09-21 10:54:07 +00:00
|
|
|
PeerID string `json:"peerID,omitempty"`
|
2021-09-15 11:41:56 +00:00
|
|
|
Timestamp *int64 `json:"timestamp,omitempty"`
|
2021-09-29 11:12:42 +00:00
|
|
|
PeerScore TraceEventPeerScore `json:"peerScore,omitempty"`
|
2021-09-16 09:51:59 +00:00
|
|
|
SourceAuth string `json:"sourceAuth,omitempty"`
|
2021-09-15 11:41:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 11:12:42 +00:00
|
|
|
type TraceEventPeerScore struct {
|
2021-09-17 11:46:26 +00:00
|
|
|
PeerID string `json:"peerID"`
|
|
|
|
Score float32 `json:"score"`
|
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) {
|
2021-09-21 10:30:50 +00:00
|
|
|
now := time.Now().UnixNano()
|
2021-09-17 11:46:26 +00:00
|
|
|
for pid, score := range scores {
|
2021-09-21 10:30:50 +00:00
|
|
|
evt := &LotusTraceEvent{
|
2021-09-29 11:12:42 +00:00
|
|
|
Type: *TraceEventPeerScores.Enum(),
|
2021-09-21 10:54:07 +00:00
|
|
|
PeerID: lt.pid.Pretty(),
|
|
|
|
Timestamp: &now,
|
|
|
|
SourceAuth: lt.sa,
|
2021-09-29 11:12:42 +00:00
|
|
|
PeerScore: TraceEventPeerScore{PeerID: pid.Pretty(), Score: float32(score.Score)},
|
2021-09-21 10:30:50 +00:00
|
|
|
}
|
2021-09-17 11:46:26 +00:00
|
|
|
|
2021-09-21 10:30:50 +00:00
|
|
|
lt.TraceLotusEvent(evt)
|
2021-09-15 11:41:56 +00:00
|
|
|
}
|
2021-08-30 12:35:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 11:41:56 +00:00
|
|
|
func (lt *lotusTracer) TraceLotusEvent(evt *LotusTraceEvent) {
|
2021-09-15 12:50:27 +00:00
|
|
|
for _, t := range lt.tt {
|
2021-09-15 14:03:51 +00:00
|
|
|
err := t.Transport(TracerTransportEvent{
|
|
|
|
lotusTraceEvent: evt,
|
|
|
|
pubsubTraceEvent: nil,
|
|
|
|
})
|
2021-09-15 12:50:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while transporting peer scores: %s", err)
|
|
|
|
}
|
2021-09-10 13:37:38 +00:00
|
|
|
}
|
2021-09-15 12:50:27 +00:00
|
|
|
|
2021-09-10 13:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lt *lotusTracer) Trace(evt *pubsub_pb.TraceEvent) {
|
2021-09-15 12:50:27 +00:00
|
|
|
for _, t := range lt.tt {
|
2021-09-15 14:03:51 +00:00
|
|
|
err := t.Transport(TracerTransportEvent{
|
|
|
|
lotusTraceEvent: nil,
|
|
|
|
pubsubTraceEvent: evt,
|
|
|
|
})
|
2021-09-15 12:50:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while transporting trace event: %s", err)
|
|
|
|
}
|
2021-09-10 13:37:38 +00:00
|
|
|
}
|
|
|
|
}
|