Skip to content

Commit

Permalink
fix(nodebuilder/p2p/metrics): enforce init order for bitswap metrics (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
walldiss authored Sep 23, 2024
1 parent f875256 commit 35cd3dd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
github.com/ipfs/go-ipld-cbor v0.1.0
github.com/ipfs/go-ipld-format v0.6.0
github.com/ipfs/go-log/v2 v2.5.1
github.com/ipfs/go-metrics-interface v0.0.1
github.com/ipfs/go-metrics-prometheus v0.0.2
github.com/ipld/go-car v0.6.2
github.com/libp2p/go-libp2p v0.36.2
Expand Down Expand Up @@ -213,7 +214,6 @@ require (
github.com/ipfs/go-ipld-legacy v0.2.1 // indirect
github.com/ipfs/go-log v1.0.5 // indirect
github.com/ipfs/go-merkledag v0.11.0 // indirect
github.com/ipfs/go-metrics-interface v0.0.1 // indirect
github.com/ipfs/go-peertaskqueue v0.8.1 // indirect
github.com/ipfs/go-verifcid v0.0.3 // indirect
github.com/ipld/go-car/v2 v2.13.1 // indirect
Expand Down
56 changes: 50 additions & 6 deletions nodebuilder/p2p/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"github.com/ipfs/boxo/blockstore"
"github.com/ipfs/boxo/exchange"
"github.com/ipfs/go-datastore"
metrics "github.com/ipfs/go-metrics-prometheus"
ipfsmetrics "github.com/ipfs/go-metrics-interface"
ipfsprom "github.com/ipfs/go-metrics-prometheus"
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
hst "github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/fx"

"github.com/celestiaorg/celestia-node/share/eds"
Expand Down Expand Up @@ -43,7 +45,14 @@ func dataExchange(params bitSwapParams) exchange.Interface {
bitswap.SetSimulateDontHavesOnTimeout(false),
bitswap.WithoutDuplicatedBlockStats(),
}
bs := bitswap.New(params.Ctx, net, params.Bs, opts...)

ctx := params.Ctx
if params.Metrics != nil {
// metrics scope is required for prometheus metrics and will be used as metrics name
// prefix
ctx = ipfsmetrics.CtxScope(ctx, "bitswap")
}
bs := bitswap.New(ctx, net, params.Bs, opts...)

params.Lifecycle.Append(fx.Hook{
OnStop: func(_ context.Context) (err error) {
Expand All @@ -53,7 +62,16 @@ func dataExchange(params bitSwapParams) exchange.Interface {
return bs
}

func blockstoreFromDatastore(ctx context.Context, ds datastore.Batching) (blockstore.Blockstore, error) {
func blockstoreFromDatastore(
ctx context.Context,
ds datastore.Batching,
b blockstoreParams,
) (blockstore.Blockstore, error) {
if b.Metrics != nil {
// metrics scope is required for prometheus metrics and will be used as metrics name
// prefix
ctx = ipfsmetrics.CtxScope(ctx, "blockstore")
}
return blockstore.CachedBlockstore(
ctx,
blockstore.NewBlockstore(ds),
Expand All @@ -65,7 +83,16 @@ func blockstoreFromDatastore(ctx context.Context, ds datastore.Batching) (blocks
)
}

func blockstoreFromEDSStore(ctx context.Context, store *eds.Store) (blockstore.Blockstore, error) {
func blockstoreFromEDSStore(
ctx context.Context,
store *eds.Store,
b blockstoreParams,
) (blockstore.Blockstore, error) {
if b.Metrics != nil {
// metrics scope is required for prometheus metrics and will be used as metrics name
// prefix
ctx = ipfsmetrics.CtxScope(ctx, "blockstore")
}
return blockstore.CachedBlockstore(
ctx,
store.Blockstore(),
Expand All @@ -75,6 +102,13 @@ func blockstoreFromEDSStore(ctx context.Context, store *eds.Store) (blockstore.B
)
}

type blockstoreParams struct {
fx.In
// Metrics is unused, it is in dependency graph to ensure that prometheus metrics are enabled before bitswap
// is started.
Metrics *bitswapMetrics `optional:"true"`
}

type bitSwapParams struct {
fx.In

Expand All @@ -83,12 +117,22 @@ type bitSwapParams struct {
Net Network
Host hst.Host
Bs blockstore.Blockstore
// Metrics is unused, it is in dependency graph to ensure that prometheus metrics are enabled before bitswap
// is started.
Metrics *bitswapMetrics `optional:"true"`
}

func protocolID(network Network) protocol.ID {
return protocol.ID(fmt.Sprintf("/celestia/%s", network))
}

func enableBitswapMetrics() {
_ = metrics.Inject()
type bitswapMetrics struct{}

func enableBitswapMetrics(_ prometheus.Registerer) *bitswapMetrics {
err := ipfsprom.Inject()
if err != nil {
log.Errorf("failed to inject bitswap metrics: %s", err)
return nil
}
return &bitswapMetrics{}
}
6 changes: 5 additions & 1 deletion nodebuilder/p2p/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func WithMetrics() fx.Option {
return fx.Options(
fx.Provide(resourceManagerOpt(traceReporter)),
fx.Provide(prometheusMetrics),
fx.Invoke(enableBitswapMetrics),
fx.Provide(enableBitswapMetrics),
)
}

Expand All @@ -45,6 +45,10 @@ func prometheusMetrics(lifecycle fx.Lifecycle,
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

0 comments on commit 35cd3dd

Please sign in to comment.