Skip to content

Commit

Permalink
feat: includes peer_id as a label in the MessageSendBytesTotal and Me…
Browse files Browse the repository at this point in the history
…ssageReceiveBytesTotal Prometheus metrics (#1086)

Inline with #1077

Our past experimentation showed that none of the current Prometheus
traffic related metrics encompass all the information regarding the
message type, peer ID and channel ID. This deficiency can be addressed
by incorporating peer IDs for `message_receive_bytes_total` and
`message_send_bytes_total`. This PR provides this feature.

I tested it by executing a local validator node and examining the
Prometheus metrics endpoint. Here's an example of the output:

```
cometbft_p2p_message_send_bytes_total{chID="0x40", chain_id="mocha-4", message_type="blockchain_StatusResponse", peer_id="cb7adca6fbaabc5336c5eebbc1312390a2bb9d2d", version="1.0.0-rc0-278-g4c452c5f4"} 8
```
  • Loading branch information
staheri14 authored Sep 22, 2023
1 parent 367caa3 commit ad660fe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions p2p/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
Subsystem: MetricsSubsystem,
Name: "message_receive_bytes_total",
Help: "Number of bytes of each message type received.",
}, append(labels, "message_type", "chID")).With(labelsAndValues...),
}, append(labels, "message_type", "chID", "peer_id")).With(labelsAndValues...),
MessageSendBytesTotal: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
Namespace: namespace,
Subsystem: MetricsSubsystem,
Name: "message_send_bytes_total",
Help: "Number of bytes of each message type sent.",
}, append(labels, "message_type", "chID")).With(labelsAndValues...),
}, append(labels, "message_type", "chID", "peer_id")).With(labelsAndValues...),
}
}

Expand Down
30 changes: 20 additions & 10 deletions p2p/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,12 @@ func (p *peer) SendEnvelope(e Envelope) bool {
}
res := p.Send(e.ChannelID, msgBytes)
if res {
p.metrics.MessageSendBytesTotal.With("message_type",
metricLabelValue, "chID", fmt.Sprintf("%#x", e.ChannelID)).Add(float64(len(msgBytes)))
labels := []string{
"message_type", metricLabelValue,
"chID", fmt.Sprintf("%#x", e.ChannelID),
"peer_id", string(p.ID()),
}
p.metrics.MessageSendBytesTotal.With(labels...).Add(float64(len(msgBytes)))
}
return res
}
Expand Down Expand Up @@ -381,8 +385,12 @@ func (p *peer) TrySendEnvelope(e Envelope) bool {
}
res := p.TrySend(e.ChannelID, msgBytes)
if res {
p.metrics.MessageSendBytesTotal.With("message_type",
metricLabelValue, "chID", fmt.Sprintf("%#x", e.ChannelID)).Add(float64(len(msgBytes)))
labels := []string{
"message_type", metricLabelValue,
"chID", fmt.Sprintf("%#x", e.ChannelID),
"peer_id", string(p.ID()),
}
p.metrics.MessageSendBytesTotal.With(labels...).Add(float64(len(msgBytes)))
}
return res
}
Expand Down Expand Up @@ -523,19 +531,21 @@ func createMConnection(
if err != nil {
panic(fmt.Errorf("unmarshaling message: %s into type: %s", err, reflect.TypeOf(mt)))
}
labels := []string{
"peer_id", string(p.ID()),
"chID", fmt.Sprintf("%#x", chID),
}

if w, ok := msg.(Unwrapper); ok {
msg, err = w.Unwrap()
if err != nil {
panic(fmt.Errorf("unwrapping message: %s", err))
}
}

labels := []string{
"peer_id", string(p.ID()),
"chID", fmt.Sprintf("%#x", chID),
}

p.metrics.PeerReceiveBytesTotal.With(labels...).Add(float64(len(msgBytes)))
p.metrics.MessageReceiveBytesTotal.With("message_type",
p.mlc.ValueToMetricLabel(msg), "chID", fmt.Sprintf("%#x", chID)).Add(float64(len(msgBytes)))
p.metrics.MessageReceiveBytesTotal.With(append(labels, "message_type", p.mlc.ValueToMetricLabel(msg))...).Add(float64(len(msgBytes)))
if nr, ok := reactor.(EnvelopeReceiver); ok {
nr.ReceiveEnvelope(Envelope{
ChannelID: chID,
Expand Down

0 comments on commit ad660fe

Please sign in to comment.