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

identify: add some basic metrics #2069

Merged
merged 1 commit into from
Feb 9, 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
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