generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 97
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
bitswap: peer prom tacker #413
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package peerpromtracker | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
bsmsg "github.com/ipfs/boxo/bitswap/message" | ||
"github.com/ipfs/boxo/bitswap/tracer" | ||
"github.com/ipfs/go-log/v2" | ||
peer "github.com/libp2p/go-libp2p/core/peer" | ||
prom "github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
var logger = log.Logger("bitswap/peer-prom-tracker") | ||
|
||
var _ tracer.Tracer = (*PeerTracer)(nil) | ||
|
||
type PeerTracer struct { | ||
bytesSent *prom.CounterVec | ||
messagesSent *prom.CounterVec | ||
bytesReceived *prom.CounterVec | ||
messagesReceived *prom.CounterVec | ||
|
||
reg *prom.Registry | ||
} | ||
|
||
func NewPeerTracer(reg *prom.Registry) (*PeerTracer, error) { | ||
pt := &PeerTracer{ | ||
reg: reg, | ||
} | ||
|
||
var good bool | ||
defer func() { | ||
if !good { | ||
pt.Close() // will unregister the sucessfully registered metrics | ||
} | ||
}() | ||
|
||
peerIdLabel := []string{"peer-id"} | ||
|
||
bytesSent := prom.NewCounterVec(prom.CounterOpts{ | ||
Namespace: "ipfs", | ||
Subsystem: "bitswap/messages", | ||
Name: "bytes-sent", | ||
Help: "This records the number of bitswap messages bytes sent per peer.", | ||
}, peerIdLabel) | ||
if err := reg.Register(bytesSent); err != nil { | ||
return nil, fmt.Errorf("registering bytes-sent: %w", err) | ||
} | ||
pt.bytesSent = bytesSent | ||
|
||
messagesSent := prom.NewCounterVec(prom.CounterOpts{ | ||
Namespace: "ipfs", | ||
Subsystem: "bitswap/messages", | ||
Name: "messages-sent", | ||
Help: "This records the number of bitswap messages sent per peer.", | ||
}, peerIdLabel) | ||
if err := reg.Register(messagesSent); err != nil { | ||
return nil, fmt.Errorf("registering messages-sent: %w", err) | ||
} | ||
pt.messagesSent = messagesSent | ||
|
||
bytesReceived := prom.NewCounterVec(prom.CounterOpts{ | ||
Namespace: "ipfs", | ||
Subsystem: "bitswap/messages", | ||
Name: "bytes-received", | ||
Help: "This records the number of bitswap messages bytes received from each peer.", | ||
}, peerIdLabel) | ||
if err := reg.Register(bytesReceived); err != nil { | ||
return nil, fmt.Errorf("registering bytes-received: %w", err) | ||
} | ||
pt.bytesReceived = bytesReceived | ||
|
||
messagesReceived := prom.NewCounterVec(prom.CounterOpts{ | ||
Namespace: "ipfs", | ||
Subsystem: "bitswap/messages", | ||
Name: "messages-received", | ||
Help: "This records the number of bitswap messages received from each peer.", | ||
}, peerIdLabel) | ||
if err := reg.Register(messagesReceived); err != nil { | ||
return nil, fmt.Errorf("registering messages-received: %w", err) | ||
} | ||
pt.messagesReceived = messagesReceived | ||
|
||
good = true | ||
return pt, nil | ||
} | ||
|
||
func (t *PeerTracer) MessageReceived(p peer.ID, msg bsmsg.BitSwapMessage) { | ||
strPeerid := p.Pretty() | ||
|
||
counter, err := t.messagesReceived.GetMetricWithLabelValues(strPeerid) | ||
if err == nil { | ||
logger.Debugf("failed to grab messages received label %s", err) | ||
} else { | ||
counter.Inc() | ||
} | ||
|
||
counter, err = t.bytesReceived.GetMetricWithLabelValues(strPeerid) | ||
if err == nil { | ||
logger.Debugf("failed to grab messages received label %s", err) | ||
} else { | ||
counter.Add(float64(msg.Size())) | ||
} | ||
} | ||
|
||
func (t *PeerTracer) MessageSent(p peer.ID, msg bsmsg.BitSwapMessage) { | ||
strPeerid := p.Pretty() | ||
|
||
counter, err := t.messagesSent.GetMetricWithLabelValues(strPeerid) | ||
if err == nil { | ||
logger.Debugf("failed to grab messages received label %s", err) | ||
} else { | ||
counter.Inc() | ||
} | ||
|
||
counter, err = t.bytesSent.GetMetricWithLabelValues(strPeerid) | ||
if err == nil { | ||
logger.Debugf("failed to grab messages received label %s", err) | ||
} else { | ||
counter.Add(float64(msg.Size())) | ||
} | ||
} | ||
|
||
func (t *PeerTracer) Close() error { | ||
if t.reg == nil { | ||
return errors.New("already closed") | ||
} | ||
|
||
// we have to check because this can be called by [NewPeerTracer] if an errors occurs. | ||
if t.bytesSent != nil { | ||
t.reg.Unregister(t.bytesSent) | ||
} | ||
if t.messagesSent != nil { | ||
t.reg.Unregister(t.messagesSent) | ||
} | ||
if t.bytesReceived != nil { | ||
t.reg.Unregister(t.bytesReceived) | ||
} | ||
if t.messagesReceived != nil { | ||
t.reg.Unregister(t.messagesReceived) | ||
} | ||
*t = PeerTracer{} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
High cardinality labels in prometheus are is considered antipattern. If there was 20K of peers, this will create 20K time series, and that may cause problems (performance, billing) when Grafana tries to visualize it.
To understand why high cardinality is a problem, see:
IMO this PR can't land in boxo in this form as it creates footgun for users of this library.
There needs to be either a hard-limit on the number of peers tracked, or an explicit opt-in via constructor option or ENV variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💭 I think if we wanted to have metrics similar to this, we could measure P95 globally without running into the cardinality problem.
To do so, one would define
Objectives
inSummaryOpts
to be P50, P75, P95 etc, and calculatemessages-received
,messages-sent
,bytes-received
etc across all peers, not specific per peer. This way we get useful P95 metric with known error margin, without exploding the time series.