Skip to content

Commit

Permalink
Expose network size estimation metric
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-tra committed Mar 31, 2023
1 parent 92af8dd commit 9cb79d1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/libp2p/go-libp2p-kad-dht/internal"
"github.com/libp2p/go-libp2p-kad-dht/metrics"
"github.com/libp2p/go-libp2p-kad-dht/qpeerset"
kb "github.com/libp2p/go-libp2p-kbucket"
"github.com/libp2p/go-libp2p/core/peer"
Expand Down Expand Up @@ -44,6 +45,10 @@ func (dht *IpfsDHT) GetClosestPeers(ctx context.Context, key string) ([]peer.ID,
logger.Warnf("network size estimator track peers: %s", err)
}

if ns, err := dht.nsEstimator.NetworkSize(); err == nil {
metrics.NetworkSize.M(int64(ns))
}

// refresh the cpl for this key as the query was successful
dht.routingTable.ResetCplRefreshedAtForID(kb.ConvertKey(key), time.Now())

Expand Down
5 changes: 5 additions & 0 deletions lookup_optim.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync/atomic"
"time"

"github.com/libp2p/go-libp2p-kad-dht/metrics"
"github.com/libp2p/go-libp2p-kad-dht/netsize"
"github.com/libp2p/go-libp2p-kad-dht/qpeerset"
kb "github.com/libp2p/go-libp2p-kbucket"
Expand Down Expand Up @@ -172,6 +173,10 @@ func (dht *IpfsDHT) optimisticProvide(outerCtx context.Context, keyMH multihash.
logger.Warnf("network size estimator track peers: %s", err)
}

if ns, err := dht.nsEstimator.NetworkSize(); err == nil {
metrics.NetworkSize.M(int64(ns))
}

// refresh the cpl for this key as the query was successful
dht.routingTable.ResetCplRefreshedAtForID(kb.ConvertKey(key), time.Now())

Expand Down
7 changes: 7 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
SentRequests = stats.Int64("libp2p.io/dht/kad/sent_requests", "Total number of requests sent per RPC", stats.UnitDimensionless)
SentRequestErrors = stats.Int64("libp2p.io/dht/kad/sent_request_errors", "Total number of errors for requests sent per RPC", stats.UnitDimensionless)
SentBytes = stats.Int64("libp2p.io/dht/kad/sent_bytes", "Total sent bytes per RPC", stats.UnitBytes)
NetworkSize = stats.Int64("libp2p.io/dht/kad/network_size", "Network size estimation", stats.UnitDimensionless)
)

// Views
Expand Down Expand Up @@ -93,6 +94,11 @@ var (
TagKeys: []tag.Key{KeyMessageType, KeyPeerID, KeyInstanceID},
Aggregation: defaultBytesDistribution,
}
NetworkSizeView = &view.View{
Measure: NetworkSize,
TagKeys: []tag.Key{KeyPeerID, KeyInstanceID},
Aggregation: view.Count(),
}
)

// DefaultViews with all views in it.
Expand All @@ -107,4 +113,5 @@ var DefaultViews = []*view.View{
SentRequestsView,
SentRequestErrorsView,
SentBytesView,
NetworkSizeView,
}

0 comments on commit 9cb79d1

Please sign in to comment.