Skip to content

Commit

Permalink
Add metrics for the routing table
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Mar 26, 2019
1 parent 26035d4 commit ee043eb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
41 changes: 31 additions & 10 deletions dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"sync"
"time"

"go.opencensus.io/metric/metricdata"

"go.opencensus.io/stats"

"github.com/libp2p/go-libp2p-kad-dht/metrics"
"go.opencensus.io/tag"

Expand Down Expand Up @@ -133,14 +137,6 @@ func NewDHTClient(ctx context.Context, h host.Host, dstore ds.Batching) *IpfsDHT
func makeDHT(ctx context.Context, h host.Host, dstore ds.Batching, protocols []protocol.ID) *IpfsDHT {
rt := kb.NewRoutingTable(KValue, kb.ConvertPeerID(h.ID()), time.Minute, h.Peerstore())

cmgr := h.ConnManager()
rt.PeerAdded = func(p peer.ID) {
cmgr.TagPeer(p, "kbucket", 5)
}
rt.PeerRemoved = func(p peer.ID) {
cmgr.UntagPeer(p, "kbucket")
}

dht := &IpfsDHT{
datastore: dstore,
self: h.ID(),
Expand All @@ -153,13 +149,38 @@ func makeDHT(ctx context.Context, h host.Host, dstore ds.Batching, protocols []p
protocols: protocols,
}
dht.ctx = dht.withLocalTags(ctx)

cmgr := h.ConnManager()
rt.PeerAdded = func(p peer.ID) {
cmgr.TagPeer(p, "kbucket", 5)
stats.Record(dht.ctx, metrics.RoutingTablePeersAdded.M(1))
}
rt.PeerRemoved = func(p peer.ID) {
cmgr.UntagPeer(p, "kbucket")
stats.Record(dht.ctx, metrics.RoutingTablePeersRemoved.M(1))
}
metrics.RoutingTableNumEntries.UpsertEntry(
func() int64 {
return int64(rt.Size())
},
metricdata.LabelValue{dht.localPeerIdTagValue(), true},
metricdata.LabelValue{dht.instanceIdTagValue(), true},
)
return dht
}

func (dht *IpfsDHT) localPeerIdTagValue() string {
return dht.self.String()
}

func (dht *IpfsDHT) instanceIdTagValue() string {
return fmt.Sprintf("%p", dht)
}

func (dht *IpfsDHT) tagMutators() []tag.Mutator {
return []tag.Mutator{
tag.Upsert(metrics.KeyLocalPeerID, dht.self.String()),
tag.Upsert(metrics.KeyInstanceID, fmt.Sprintf("%p", dht)),
tag.Upsert(metrics.KeyLocalPeerID, dht.localPeerIdTagValue()),
tag.Upsert(metrics.KeyInstanceID, dht.instanceIdTagValue()),
}
}

Expand Down
18 changes: 18 additions & 0 deletions metrics/opencensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package metrics

import (
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
"go.opencensus.io/metric"
"go.opencensus.io/metric/metricdata"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
Expand Down Expand Up @@ -40,6 +42,14 @@ var (
"Time between wanting to send a message, and writing it",
stats.UnitMilliseconds,
)

RoutingTablePeersAdded = stats.Int64(namePrefix+"routing_table_peers_added", "", stats.UnitDimensionless)
RoutingTablePeersRemoved = stats.Int64(namePrefix+"routing_table_peers_removed", "", stats.UnitDimensionless)

GaugeRegistry = metric.NewRegistry()
RoutingTableNumEntries, _ = GaugeRegistry.AddInt64DerivedGauge(
"routing_table_num_entries", "", metricdata.UnitDimensionless,
KeyLocalPeerID.Name(), KeyInstanceID.Name())
)

var Views = []*view.View{{
Expand Down Expand Up @@ -70,4 +80,12 @@ var Views = []*view.View{{
Measure: MessageWriteLatencyMs,
TagKeys: []tag.Key{KeyMessageType, KeyLocalPeerID, KeyInstanceID},
Aggregation: view.Distribution(0, 1, 10, 100),
}, {
Measure: RoutingTablePeersAdded,
TagKeys: []tag.Key{KeyLocalPeerID, KeyInstanceID},
Aggregation: view.Count(),
}, {
Measure: RoutingTablePeersRemoved,
TagKeys: []tag.Key{KeyLocalPeerID, KeyInstanceID},
Aggregation: view.Count(),
}}

0 comments on commit ee043eb

Please sign in to comment.