Skip to content

Commit

Permalink
ensure libp2p metrics are collected to prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
walldiss committed Sep 19, 2024
1 parent 2ce4bc8 commit bc4e6c5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
26 changes: 13 additions & 13 deletions cmd/flags_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ func ParseMiscFlags(ctx context.Context, cmd *cobra.Command) (context.Context, e
}
}

ok, err := cmd.Flags().GetBool(pprofFlag)
enablePprof, err := cmd.Flags().GetBool(pprofFlag)
if err != nil {
panic(err)
}

if ok {
if enablePprof {
// TODO(@Wondertan): Eventually, this should be registered on http server in RPC
// by passing the http.Server with preregistered pprof handlers to the node.
// Node should not register pprof itself.
Expand All @@ -174,12 +174,12 @@ func ParseMiscFlags(ctx context.Context, cmd *cobra.Command) (context.Context, e
}()
}

ok, err = cmd.Flags().GetBool(pyroscopeFlag)
enablePyro, err := cmd.Flags().GetBool(pyroscopeFlag)
if err != nil {
panic(err)
}

if ok {
if enablePyro {
ctx = WithNodeOptions(ctx,
nodebuilder.WithPyroscope(
cmd.Flag(pyroscopeEndpoint).Value.String(),
Expand All @@ -188,12 +188,12 @@ func ParseMiscFlags(ctx context.Context, cmd *cobra.Command) (context.Context, e
)
}

ok, err = cmd.Flags().GetBool(tracingFlag)
enableTracing, err := cmd.Flags().GetBool(tracingFlag)
if err != nil {
panic(err)
}

if ok {
if enableTracing {
opts := []otlptracehttp.Option{
otlptracehttp.WithCompression(otlptracehttp.GzipCompression),
otlptracehttp.WithEndpoint(cmd.Flag(tracingEndpointFlag).Value.String()),
Expand All @@ -205,11 +205,11 @@ func ParseMiscFlags(ctx context.Context, cmd *cobra.Command) (context.Context, e
}

pyroOpts := make([]otelpyroscope.Option, 0)
ok, err = cmd.Flags().GetBool(pyroscopeTracing)
enablePyroTracing, err := cmd.Flags().GetBool(pyroscopeTracing)
if err != nil {
panic(err)
}
if ok {
if enablePyroTracing {
pyroOpts = append(pyroOpts,
otelpyroscope.WithAppName("celestia.da-node"),
otelpyroscope.WithPyroscopeURL(cmd.Flag(pyroscopeEndpoint).Value.String()),
Expand All @@ -222,12 +222,12 @@ func ParseMiscFlags(ctx context.Context, cmd *cobra.Command) (context.Context, e
ctx = WithNodeOptions(ctx, nodebuilder.WithTraces(opts, pyroOpts))
}

ok, err = cmd.Flags().GetBool(metricsFlag)
enableMetrics, err := cmd.Flags().GetBool(metricsFlag)
if err != nil {
panic(err)
}

if ok {
if enableMetrics {
opts := []otlpmetrichttp.Option{
otlpmetrichttp.WithCompression(otlpmetrichttp.GzipCompression),
otlpmetrichttp.WithEndpoint(cmd.Flag(metricsEndpointFlag).Value.String()),
Expand All @@ -241,13 +241,13 @@ func ParseMiscFlags(ctx context.Context, cmd *cobra.Command) (context.Context, e
ctx = WithNodeOptions(ctx, nodebuilder.WithMetrics(opts, NodeType(ctx)))
}

ok, err = cmd.Flags().GetBool(p2pMetrics)
enablep2pMetrics, err := cmd.Flags().GetBool(p2pMetrics)
if err != nil {
panic(err)
}

if ok {
if metricsEnabled, _ := cmd.Flags().GetBool(metricsFlag); !metricsEnabled {
if enablep2pMetrics {
if !enableMetrics {
log.Error("--p2p.metrics used without --metrics being enabled")
} else {
ctx = WithNodeOptions(ctx, modp2p.WithMetrics())
Expand Down
3 changes: 3 additions & 0 deletions nodebuilder/p2p/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type Config struct {

// Allowlist for IPColocation PubSub parameter, a list of string CIDRs
IPColocationWhitelist []string

// enableMetrics enables prometheus metrics collection in libp2p
enableMetrics bool
}

// DefaultConfig returns default configuration for P2P subsystem.
Expand Down
5 changes: 4 additions & 1 deletion nodebuilder/p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ func host(params hostParams) (HostBase, error) {
libp2p.DefaultMuxers,
}

if params.Registry != nil {
if params.Cfg.enableMetrics {
if params.Registry == nil {
panic("metrics enabled but no prometheus registry provided")
}
opts = append(opts, libp2p.PrometheusRegisterer(params.Registry))
} else {
opts = append(opts, libp2p.DisableMetrics())
Expand Down
15 changes: 12 additions & 3 deletions nodebuilder/p2p/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
func WithMetrics() fx.Option {
return fx.Options(
fx.Provide(resourceManagerOpt(traceReporter)),
fx.Invoke(prometheusMetrics),
fx.Provide(prometheusMetrics),
fx.Invoke(enableBitswapMetrics),
fx.Invoke(enableMetrics),
)
}

Expand All @@ -32,19 +33,27 @@ const (
peerIDLabel = "peer_id"
)

func enableMetrics(cfg *Config) {
cfg.enableMetrics = true
}

// prometheusMetrics option sets up native libp2p metrics up
func prometheusMetrics(lifecycle fx.Lifecycle,
peerID peer.ID,
nodeType node.Type,
network Network,
) error {
) (prometheus.Registerer, error) {
reg := prometheus.NewRegistry()
labels := prometheus.Labels{
networkLabel: network.String(),
nodeTypeLabel: nodeType.String(),
peerIDLabel: peerID.String(),
}
wrapped := prometheus.WrapRegistererWith(labels, reg)
// Set the default global registerer to the wrapped one with labels. This way all the metrics
// registered with the default registerer will be labeled with the provided labels. It is important
// because unlike libp2p metrics, bitswap metrics are registered with the default global registerer.
prometheus.DefaultRegisterer = wrapped

mux := http.NewServeMux()
handler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: wrapped})
Expand Down Expand Up @@ -72,5 +81,5 @@ func prometheusMetrics(lifecycle fx.Lifecycle,
return promHTTPServer.Shutdown(ctx)
},
})
return nil
return wrapped, nil
}

0 comments on commit bc4e6c5

Please sign in to comment.