Skip to content

Lower allocation overhead of metrics in peer.Send #2840

Description

@ValarDragon

Feature Request

Summary

In Memory profiles of full nodes, we see large amounts of heap allocation in p2p.send coming from Prometheus logging, that is ~entirely duplicate work.
image

This is coming from these lines:

cometbft/p2p/peer.go

Lines 284 to 292 in b846639

if res {
labels := []string{
"peer_id", string(p.ID()),
"chID", fmt.Sprintf("%#x", chID),
}
p.metrics.PeerSendBytesTotal.With(labels...).Add(float64(len(msgBytes)))
p.metrics.MessageSendBytesTotal.With("message_type", metricLabelValue).Add(float64(len(msgBytes)))
}
return res

We should be able to have the labels already cached (or at minimum the peer ID label cached) in the existing peer.Metrics instance.

Problem Definition

Lower heap allocation and time overhead of sending something to peers. I have no idea how much this is a dominant term in wall clock time, though I do see recv time and send time routinely in CPU profiles.

Proposal

Cache the peer ID label in p.metrics.PeerSendBytesTotal, so this is re-done on every packet send

Activity

  1. added
    enhancement
    needs-triage
    on Apr 18, 2024
  2. cason commented on Apr 18, 2024

    @cason

    There is a cache though:

    type metricsLabelCache struct {

  3. cason commented on Apr 18, 2024

    @cason

    But it doesn't apply to channel ID (which is immutable after startup) and to peer ID (this changes over time, so a cache would be good).

    But in general, have per peer ID metrics looks an overkill to me.

  4. ValarDragon commented on Apr 18, 2024

    @ValarDragon
    ContributorAuthor

    That makes sense! I'm down for deleting per peer id metrics as well

  5. hvanz commented on Apr 18, 2024

    @hvanz
    Collaborator

    I'm also in favor of deprecating this metric. I don't think it's useful for a node operator but we would need to check.

    There was already a warning about the peer ID in the original PR tendermint/tendermint#2425 (review)

    "Another red flag is the peer-id label, which we have to investigate will not lead to a cardinality explosion as this can be the cause for degraded performance of the monitoring pipeline to the point of it being downright unusable if the values in that label exceed the 10s/100s."

  6. cason commented on Apr 18, 2024

    @cason

    We should be able to have the labels already cached (or at minimum the peer ID label cached)

    I am wondering why we need to cache the peer ID. It is a string, so are you sure we are creating copies here?

  7. cason commented on Apr 18, 2024

    @cason

    If the problem is only the channel's labels, #2851 should solve it.

  8. ValarDragon commented on Apr 18, 2024

    @ValarDragon
    ContributorAuthor

    Lets measure after that gets in! The memory overhead is coming from the prometheus With and Add calls I believe, so the creation of the PeerId as a string isn't the issue

  9. cason commented on Apr 18, 2024

    @cason

    Updated the PR. It is covering both send and receive side metrics.

  10. 17 remaining items

  11. adizere commented on Jul 4, 2024

    @adizere
    Contributor

    Thanks Dev!

  12. removed this from the 2024-Q2 milestone on Jul 4, 2024
  13. moved this from Done to Ready for Review in CometBFTon Jul 4, 2024
  14. moved this from Ready for Review to Approved in CometBFTon Jul 22, 2024
  15. moved this from Approved to Done in CometBFTon Jul 26, 2024
  16. added a commit that references this issue on Jul 26, 2024
    581ad1f
  17. added a commit that references this issue on Jul 27, 2024
    672aa13
  18. added a commit that references this issue on Jul 28, 2024
    94d42a9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementmetricsp2p

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions