[signal] add pprof and message size metrics (#3337)

This commit is contained in:
Pascal Fischer 2025-02-17 17:07:30 +01:00 committed by GitHub
parent 039a985f41
commit abe8da697c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 0 deletions

View File

@ -8,6 +8,8 @@ import (
"fmt"
"net"
"net/http"
// nolint:gosec
_ "net/http/pprof"
"strings"
"time"
@ -82,6 +84,8 @@ var (
RunE: func(cmd *cobra.Command, args []string) error {
flag.Parse()
startPprof()
opts, certManager, err := getTLSConfigurations()
if err != nil {
return err
@ -170,6 +174,15 @@ var (
}
)
func startPprof() {
go func() {
log.Debugf("Starting pprof server on 127.0.0.1:6060")
if err := http.ListenAndServe("127.0.0.1:6060", nil); err != nil {
log.Fatalf("pprof server failed: %v", err)
}
}()
}
func getTLSConfigurations() ([]grpc.ServerOption, *autocert.Manager, error) {
var (
err error

View File

@ -20,6 +20,8 @@ type AppMetrics struct {
MessagesForwarded metric.Int64Counter
MessageForwardFailures metric.Int64Counter
MessageForwardLatency metric.Float64Histogram
MessageSize metric.Int64Histogram
}
func NewAppMetrics(meter metric.Meter) (*AppMetrics, error) {
@ -97,6 +99,16 @@ func NewAppMetrics(meter metric.Meter) (*AppMetrics, error) {
return nil, err
}
messageSize, err := meter.Int64Histogram(
"message.size.bytes",
metric.WithUnit("bytes"),
metric.WithExplicitBucketBoundaries(getMessageSizeBucketBoundaries()...),
metric.WithDescription("Records the size of each message sent"),
)
if err != nil {
return nil, err
}
return &AppMetrics{
Meter: meter,
@ -112,9 +124,26 @@ func NewAppMetrics(meter metric.Meter) (*AppMetrics, error) {
MessagesForwarded: messagesForwarded,
MessageForwardFailures: messageForwardFailures,
MessageForwardLatency: messageForwardLatency,
MessageSize: messageSize,
}, nil
}
func getMessageSizeBucketBoundaries() []float64 {
return []float64{
100,
250,
500,
1000,
5000,
10000,
50000,
100000,
500000,
1000000,
}
}
func getStandardBucketBoundaries() []float64 {
return []float64{
0.1,

View File

@ -13,6 +13,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
gproto "google.golang.org/protobuf/proto"
"github.com/netbirdio/netbird/signal/metrics"
"github.com/netbirdio/netbird/signal/peer"
@ -175,4 +176,5 @@ func (s *Server) forwardMessageToPeer(ctx context.Context, msg *proto.EncryptedM
// in milliseconds
s.metrics.MessageForwardLatency.Record(ctx, float64(time.Since(start).Nanoseconds())/1e6, metric.WithAttributes(attribute.String(labelType, labelTypeStream)))
s.metrics.MessagesForwarded.Add(ctx, 1)
s.metrics.MessageSize.Record(ctx, int64(gproto.Size(msg)), metric.WithAttributes(attribute.String(labelType, labelTypeMessage)))
}