Skip to content

Commit

Permalink
identify: add some basic metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Feb 7, 2023
1 parent 3cee9fa commit a83940a
Show file tree
Hide file tree
Showing 8 changed files with 498 additions and 17 deletions.
2 changes: 2 additions & 0 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,14 @@ func NewHost(n network.Network, opts *HostOpts) (*BasicHost, error) {
identify.UserAgent(opts.UserAgent),
identify.ProtocolVersion(opts.ProtocolVersion),
identify.DisableSignedPeerRecord(),
identify.WithMetricsTracer(identify.NewMetricsTracer()),
)
} else {
h.ids, err = identify.NewIDService(
h,
identify.UserAgent(opts.UserAgent),
identify.ProtocolVersion(opts.ProtocolVersion),
identify.WithMetricsTracer(identify.NewMetricsTracer()),
)
}
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion p2p/host/resource-manager/obs/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

const metricNamespace = "libp2p_rcmgr"

var (
metricNamespace = "libp2p_rcmgr"

// Conns
conns = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Expand Down
14 changes: 14 additions & 0 deletions p2p/metricshelper/dir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package metricshelper

import "github.com/libp2p/go-libp2p/core/network"

func GetDirection(dir network.Direction) string {
switch dir {
case network.DirOutbound:
return "outbound"
case network.DirInbound:
return "inbound"
default:
return "unknown"
}
}
19 changes: 4 additions & 15 deletions p2p/net/swarm/swarm_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,6 @@ func NewMetricsTracer() *metricsTracer {
return &metricsTracer{}
}

func getDirection(dir network.Direction) string {
switch dir {
case network.DirOutbound:
return "outbound"
case network.DirInbound:
return "inbound"
default:
return "unknown"
}
}

func appendConnectionState(tags []string, cs network.ConnectionState) []string {
if cs.Transport == "" {
// This shouldn't happen, unless the transport doesn't properly set the Transport field in the ConnectionState.
Expand All @@ -123,12 +112,12 @@ func (m *metricsTracer) OpenedConnection(dir network.Direction, p crypto.PubKey,
tags := metricshelper.GetStringSlice()
defer metricshelper.PutStringSlice(tags)

*tags = append(*tags, getDirection(dir))
*tags = append(*tags, metricshelper.GetDirection(dir))
*tags = appendConnectionState(*tags, cs)
connsOpened.WithLabelValues(*tags...).Inc()

*tags = (*tags)[:0]
*tags = append(*tags, getDirection(dir))
*tags = append(*tags, metricshelper.GetDirection(dir))
*tags = append(*tags, p.Type().String())
keyTypes.WithLabelValues(*tags...).Inc()
}
Expand All @@ -137,12 +126,12 @@ func (m *metricsTracer) ClosedConnection(dir network.Direction, duration time.Du
tags := metricshelper.GetStringSlice()
defer metricshelper.PutStringSlice(tags)

*tags = append(*tags, getDirection(dir))
*tags = append(*tags, metricshelper.GetDirection(dir))
*tags = appendConnectionState(*tags, cs)
connsClosed.WithLabelValues(*tags...).Inc()

*tags = (*tags)[:0]
*tags = append(*tags, getDirection(dir))
*tags = append(*tags, metricshelper.GetDirection(dir))
*tags = appendConnectionState(*tags, cs)
connDuration.WithLabelValues(*tags...).Observe(duration.Seconds())
}
Expand Down
Loading

0 comments on commit a83940a

Please sign in to comment.