Skip to content

Commit

Permalink
feat: add handshake protocol messages metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
mrekucci committed Sep 14, 2021
1 parent dad0c45 commit f6a22ab
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pkg/p2p/libp2p/internal/handshake/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type Service struct {
receivedHandshakesMu sync.Mutex
logger logging.Logger
libp2pID libp2ppeer.ID
metrics metrics
network.Notifiee // handshake service can be the receiver for network.Notify
}

Expand Down Expand Up @@ -111,6 +112,7 @@ func New(signer crypto.Signer, advertisableAddresser AdvertisableAddressResolver
receivedHandshakes: make(map[libp2ppeer.ID]struct{}),
libp2pID: ownPeerID,
logger: logger,
metrics: newMetrics(),
Notifiee: new(network.NoopNotifiee),
}
svc.welcomeMessage.Store(welcomeMessage)
Expand Down Expand Up @@ -244,8 +246,10 @@ func (s *Service) Handle(ctx context.Context, stream p2p.Stream, remoteMultiaddr

var syn pb.Syn
if err := r.ReadMsgWithContext(ctx, &syn); err != nil {
s.metrics.SynRxFailed.Inc()
return nil, fmt.Errorf("read syn message: %w", err)
}
s.metrics.SynRx.Inc()

observedUnderlay, err := ma.NewMultiaddrBytes(syn.ObservedUnderlay)
if err != nil {
Expand Down Expand Up @@ -285,13 +289,17 @@ func (s *Service) Handle(ctx context.Context, stream p2p.Stream, remoteMultiaddr
WelcomeMessage: welcomeMessage,
},
}); err != nil {
s.metrics.SynAckTxFailed.Inc()
return nil, fmt.Errorf("write synack message: %w", err)
}
s.metrics.SynAckTx.Inc()

var ack pb.Ack
if err := r.ReadMsgWithContext(ctx, &ack); err != nil {
s.metrics.AckRxFailed.Inc()
return nil, fmt.Errorf("read ack message: %w", err)
}
s.metrics.AckRx.Inc()

if ack.NetworkID != s.networkID {
return nil, ErrNetworkIDIncompatible
Expand Down
69 changes: 69 additions & 0 deletions pkg/p2p/libp2p/internal/handshake/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2021 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package handshake

import (
m "github.com/ethersphere/bee/pkg/metrics"
"github.com/prometheus/client_golang/prometheus"
)

// metrics groups handshake related prometheus counters.
type metrics struct {
SynRx prometheus.Counter
SynRxFailed prometheus.Counter
SynAckTx prometheus.Counter
SynAckTxFailed prometheus.Counter
AckRx prometheus.Counter
AckRxFailed prometheus.Counter
}

// newMetrics is a convenient constructor for creating new metrics.
func newMetrics() metrics {
const subsystem = "handshake"

return metrics{
SynRx: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "syn_rx",
Help: "The number of syn messages that were successfully read.",
}),
SynRxFailed: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "syn_rx_failed",
Help: "The number of syn messages that were unsuccessfully read.",
}),
SynAckTx: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "syn_ack_tx",
Help: "The number of syn-ack messages that were successfully written.",
}),
SynAckTxFailed: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "syn_ack_tx_failed",
Help: "The number of syn-ack messages that were unsuccessfully written.",
}),
AckRx: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "ack_rx",
Help: "The number of ack messages that were successfully read.",
}),
AckRxFailed: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "ack_rx_failed",
Help: "The number of ack messages that were unsuccessfully read.",
}),
}
}

// Metrics returns set of prometheus collectors.
func (s *Service) Metrics() []prometheus.Collector {
return m.PrometheusCollectorsFromFields(s.metrics)
}
3 changes: 2 additions & 1 deletion pkg/p2p/libp2p/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@ func newMetrics() metrics {
}

func (s *Service) Metrics() []prometheus.Collector {
return m.PrometheusCollectorsFromFields(s.metrics)
return append(m.PrometheusCollectorsFromFields(s.metrics),
m.PrometheusCollectorsFromFields(s.handshakeService.Metrics())...)
}

0 comments on commit f6a22ab

Please sign in to comment.