Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autonat: add metrics #2086

Merged
merged 7 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ func (cfg *Config) NewNode() (host.Host, error) {
autonat.UsingAddresses(func() []ma.Multiaddr {
return addrF(h.AllAddrs())
}),
autonat.WithMetricsTracer(autonat.NewMetricsTracer()),
}
if cfg.AutoNATConfig.ThrottleInterval != 0 {
autonatOpts = append(autonatOpts,
Expand Down
12 changes: 10 additions & 2 deletions p2p/host/autonat/autonat.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ func (as *AmbientAutoNAT) Status() network.Reachability {
func (as *AmbientAutoNAT) emitStatus() {
status := as.status.Load()
as.emitReachabilityChanged.Emit(event.EvtLocalReachabilityChanged{Reachability: status.Reachability})
if as.metricsTracer != nil {
as.metricsTracer.ReachabilityStatus(status.Reachability)
}
}

// PublicAddr returns the publicly connectable Multiaddr of this node if one is known.
Expand Down Expand Up @@ -279,7 +282,9 @@ func (as *AmbientAutoNAT) scheduleProbe() time.Duration {
nextProbe = as.lastProbe.Add(untilNext)
}
}

if as.metricsTracer != nil {
as.metricsTracer.NextProbeTime(nextProbe)
}
return nextProbe.Sub(fixedNow)
}

Expand Down Expand Up @@ -345,6 +350,9 @@ func (as *AmbientAutoNAT) recordObservation(observation autoNATResult) {
as.emitStatus()
}
}
if as.metricsTracer != nil {
as.metricsTracer.ReachabilityStatusConfidence(as.confidence)
}
}

func (as *AmbientAutoNAT) tryProbe(p peer.ID) bool {
Expand Down Expand Up @@ -372,7 +380,7 @@ func (as *AmbientAutoNAT) tryProbe(p peer.ID) bool {
}

func (as *AmbientAutoNAT) probe(pi *peer.AddrInfo) {
cli := NewAutoNATClient(as.host, as.config.addressFunc)
cli := NewAutoNATClient(as.host, as.config.addressFunc, as.metricsTracer)
ctx, cancel := context.WithTimeout(as.ctx, as.config.requestTimeout)
defer cancel()

Expand Down
8 changes: 6 additions & 2 deletions p2p/host/autonat/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ import (

// NewAutoNATClient creates a fresh instance of an AutoNATClient
// If addrFunc is nil, h.Addrs will be used
func NewAutoNATClient(h host.Host, addrFunc AddrFunc) Client {
func NewAutoNATClient(h host.Host, addrFunc AddrFunc, mt MetricsTracer) Client {
if addrFunc == nil {
addrFunc = h.Addrs
}
return &client{h: h, addrFunc: addrFunc}
return &client{h: h, addrFunc: addrFunc, mt: mt}
}

type client struct {
h host.Host
addrFunc AddrFunc
mt MetricsTracer
}

// DialBack asks peer p to dial us back on all addresses returned by the addrFunc.
Expand Down Expand Up @@ -79,6 +80,9 @@ func (c *client) DialBack(ctx context.Context, p peer.ID) (ma.Multiaddr, error)
}

status := res.GetDialResponse().GetStatus()
if c.mt != nil {
c.mt.ReceivedDialResponse(status)
}
switch status {
case pb.Message_OK:
addr := res.GetDialResponse().GetAddr()
Expand Down
Loading