@@ -9,10 +9,12 @@ import (
9
9
"github.com/ipfs/boxo/blockstore"
10
10
"github.com/ipfs/boxo/exchange"
11
11
"github.com/ipfs/go-datastore"
12
- metrics "github.com/ipfs/go-metrics-prometheus"
12
+ ipfsmetrics "github.com/ipfs/go-metrics-interface"
13
+ ipfsprom "github.com/ipfs/go-metrics-prometheus"
13
14
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
14
15
hst "github.com/libp2p/go-libp2p/core/host"
15
16
"github.com/libp2p/go-libp2p/core/protocol"
17
+ "github.com/prometheus/client_golang/prometheus"
16
18
"go.uber.org/fx"
17
19
18
20
"github.com/celestiaorg/celestia-node/share/eds"
@@ -43,7 +45,14 @@ func dataExchange(params bitSwapParams) exchange.Interface {
43
45
bitswap .SetSimulateDontHavesOnTimeout (false ),
44
46
bitswap .WithoutDuplicatedBlockStats (),
45
47
}
46
- bs := bitswap .New (params .Ctx , net , params .Bs , opts ... )
48
+
49
+ ctx := params .Ctx
50
+ if params .Metrics != nil {
51
+ // metrics scope is required for prometheus metrics and will be used as metrics name
52
+ // prefix
53
+ ctx = ipfsmetrics .CtxScope (ctx , "bitswap" )
54
+ }
55
+ bs := bitswap .New (ctx , net , params .Bs , opts ... )
47
56
48
57
params .Lifecycle .Append (fx.Hook {
49
58
OnStop : func (_ context.Context ) (err error ) {
@@ -53,7 +62,16 @@ func dataExchange(params bitSwapParams) exchange.Interface {
53
62
return bs
54
63
}
55
64
56
- func blockstoreFromDatastore (ctx context.Context , ds datastore.Batching ) (blockstore.Blockstore , error ) {
65
+ func blockstoreFromDatastore (
66
+ ctx context.Context ,
67
+ ds datastore.Batching ,
68
+ b blockstoreParams ,
69
+ ) (blockstore.Blockstore , error ) {
70
+ if b .Metrics != nil {
71
+ // metrics scope is required for prometheus metrics and will be used as metrics name
72
+ // prefix
73
+ ctx = ipfsmetrics .CtxScope (ctx , "blockstore" )
74
+ }
57
75
return blockstore .CachedBlockstore (
58
76
ctx ,
59
77
blockstore .NewBlockstore (ds ),
@@ -65,7 +83,16 @@ func blockstoreFromDatastore(ctx context.Context, ds datastore.Batching) (blocks
65
83
)
66
84
}
67
85
68
- func blockstoreFromEDSStore (ctx context.Context , store * eds.Store ) (blockstore.Blockstore , error ) {
86
+ func blockstoreFromEDSStore (
87
+ ctx context.Context ,
88
+ store * eds.Store ,
89
+ b blockstoreParams ,
90
+ ) (blockstore.Blockstore , error ) {
91
+ if b .Metrics != nil {
92
+ // metrics scope is required for prometheus metrics and will be used as metrics name
93
+ // prefix
94
+ ctx = ipfsmetrics .CtxScope (ctx , "blockstore" )
95
+ }
69
96
return blockstore .CachedBlockstore (
70
97
ctx ,
71
98
store .Blockstore (),
@@ -75,6 +102,13 @@ func blockstoreFromEDSStore(ctx context.Context, store *eds.Store) (blockstore.B
75
102
)
76
103
}
77
104
105
+ type blockstoreParams struct {
106
+ fx.In
107
+ // Metrics is unused, it is in dependency graph to ensure that prometheus metrics are enabled before bitswap
108
+ // is started.
109
+ Metrics * bitswapMetrics `optional:"true"`
110
+ }
111
+
78
112
type bitSwapParams struct {
79
113
fx.In
80
114
@@ -83,12 +117,22 @@ type bitSwapParams struct {
83
117
Net Network
84
118
Host hst.Host
85
119
Bs blockstore.Blockstore
120
+ // Metrics is unused, it is in dependency graph to ensure that prometheus metrics are enabled before bitswap
121
+ // is started.
122
+ Metrics * bitswapMetrics `optional:"true"`
86
123
}
87
124
88
125
func protocolID (network Network ) protocol.ID {
89
126
return protocol .ID (fmt .Sprintf ("/celestia/%s" , network ))
90
127
}
91
128
92
- func enableBitswapMetrics () {
93
- _ = metrics .Inject ()
129
+ type bitswapMetrics struct {}
130
+
131
+ func enableBitswapMetrics (_ prometheus.Registerer ) * bitswapMetrics {
132
+ err := ipfsprom .Inject ()
133
+ if err != nil {
134
+ log .Errorf ("failed to inject bitswap metrics: %s" , err )
135
+ return nil
136
+ }
137
+ return & bitswapMetrics {}
94
138
}
0 commit comments