Skip to content

Commit ab72e8e

Browse files
committed
fix: plumb through ctor contexts to preserve metrics scopes
1 parent 81393bc commit ab72e8e

File tree

6 files changed

+37
-28
lines changed

6 files changed

+37
-28
lines changed

bitswap.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import (
1010
"github.com/ipfs/go-bitswap/network"
1111
"github.com/ipfs/go-bitswap/server"
1212
"github.com/ipfs/go-bitswap/tracer"
13+
"github.com/ipfs/go-metrics-interface"
1314

14-
"github.com/ipfs/go-block-format"
15+
blocks "github.com/ipfs/go-block-format"
1516
"github.com/ipfs/go-cid"
16-
"github.com/ipfs/go-ipfs-blockstore"
17-
"github.com/ipfs/go-ipfs-exchange-interface"
17+
blockstore "github.com/ipfs/go-ipfs-blockstore"
18+
exchange "github.com/ipfs/go-ipfs-exchange-interface"
1819
logging "github.com/ipfs/go-log"
1920
"github.com/libp2p/go-libp2p-core/peer"
2021

@@ -86,6 +87,8 @@ func New(ctx context.Context, net network.BitSwapNetwork, bstore blockstore.Bloc
8687
serverOptions = append(serverOptions, server.HasBlockBufferSize(HasBlockBufferSize))
8788
}
8889

90+
ctx = metrics.CtxSubScope(ctx, "bitswap")
91+
8992
bs.Server = server.New(ctx, net, bstore, serverOptions...)
9093
bs.Client = client.New(ctx, net, bstore, append(clientOptions, client.WithBlockReceivedNotifier(bs.Server))...)
9194
net.Start(bs) // use the polyfill receiver to log received errors and trace messages only once

client/client.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ func New(parent context.Context, network bsnet.BitSwapNetwork, bstore blockstore
153153
sim: sim,
154154
notif: notif,
155155
counters: new(counters),
156-
dupMetric: bmetrics.DupHist(),
157-
allMetric: bmetrics.AllHist(),
156+
dupMetric: bmetrics.DupHist(ctx),
157+
allMetric: bmetrics.AllHist(ctx),
158158
provSearchDelay: defaults.ProvSearchDelay,
159159
rebroadcastDelay: delay.Fixed(time.Minute),
160160
simulateDontHavesOnTimeout: true,

metrics/metrics.go

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package metrics
22

33
import (
4+
"context"
5+
46
"github.com/ipfs/go-metrics-interface"
57
)
68

@@ -11,34 +13,34 @@ var (
1113
timeMetricsBuckets = []float64{1, 10, 30, 60, 90, 120, 600}
1214
)
1315

14-
func DupHist() metrics.Histogram {
15-
return metrics.New("recv_dup_blocks_bytes", "Summary of duplicate data blocks recived").Histogram(metricsBuckets)
16+
func DupHist(ctx context.Context) metrics.Histogram {
17+
return metrics.NewCtx(ctx, "recv_dup_blocks_bytes", "Summary of duplicate data blocks recived").Histogram(metricsBuckets)
1618
}
1719

18-
func AllHist() metrics.Histogram {
19-
return metrics.New("recv_all_blocks_bytes", "Summary of all data blocks recived").Histogram(metricsBuckets)
20+
func AllHist(ctx context.Context) metrics.Histogram {
21+
return metrics.NewCtx(ctx, "recv_all_blocks_bytes", "Summary of all data blocks recived").Histogram(metricsBuckets)
2022
}
2123

22-
func SentHist() metrics.Histogram {
23-
return metrics.New("sent_all_blocks_bytes", "Histogram of blocks sent by this bitswap").Histogram(metricsBuckets)
24+
func SentHist(ctx context.Context) metrics.Histogram {
25+
return metrics.NewCtx(ctx, "sent_all_blocks_bytes", "Histogram of blocks sent by this bitswap").Histogram(metricsBuckets)
2426
}
2527

26-
func SendTimeHist() metrics.Histogram {
27-
return metrics.New("send_times", "Histogram of how long it takes to send messages in this bitswap").Histogram(timeMetricsBuckets)
28+
func SendTimeHist(ctx context.Context) metrics.Histogram {
29+
return metrics.NewCtx(ctx, "send_times", "Histogram of how long it takes to send messages in this bitswap").Histogram(timeMetricsBuckets)
2830
}
2931

30-
func PendingEngineGauge() metrics.Gauge {
31-
return metrics.New("pending_tasks", "Total number of pending tasks").Gauge()
32+
func PendingEngineGauge(ctx context.Context) metrics.Gauge {
33+
return metrics.NewCtx(ctx, "pending_tasks", "Total number of pending tasks").Gauge()
3234
}
3335

34-
func ActiveEngineGauge() metrics.Gauge {
35-
return metrics.New("active_tasks", "Total number of active tasks").Gauge()
36+
func ActiveEngineGauge(ctx context.Context) metrics.Gauge {
37+
return metrics.NewCtx(ctx, "active_tasks", "Total number of active tasks").Gauge()
3638
}
3739

38-
func PendingBlocksGauge() metrics.Gauge {
39-
return metrics.New("pending_block_tasks", "Total number of pending blockstore tasks").Gauge()
40+
func PendingBlocksGauge(ctx context.Context) metrics.Gauge {
41+
return metrics.NewCtx(ctx, "pending_block_tasks", "Total number of pending blockstore tasks").Gauge()
4042
}
4143

42-
func ActiveBlocksGauge() metrics.Gauge {
43-
return metrics.New("active_block_tasks", "Total number of active blockstore tasks").Gauge()
44+
func ActiveBlocksGauge(ctx context.Context) metrics.Gauge {
45+
return metrics.NewCtx(ctx, "active_block_tasks", "Total number of active blockstore tasks").Gauge()
4446
}

server/internal/decision/engine.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,14 @@ func wrapTaskComparator(tc TaskComparator) peertask.QueueTaskComparator {
305305
// maxOutstandingBytesPerPeer hints to the peer task queue not to give a peer more tasks if it has some maximum
306306
// work already outstanding.
307307
func NewEngine(
308+
ctx context.Context,
308309
bs bstore.Blockstore,
309310
peerTagger PeerTagger,
310311
self peer.ID,
311312
opts ...Option,
312313
) *Engine {
313314
return newEngine(
315+
ctx,
314316
bs,
315317
peerTagger,
316318
self,
@@ -320,6 +322,7 @@ func NewEngine(
320322
}
321323

322324
func newEngine(
325+
ctx context.Context,
323326
bs bstore.Blockstore,
324327
peerTagger PeerTagger,
325328
self peer.ID,
@@ -340,8 +343,8 @@ func newEngine(
340343
sendDontHaves: true,
341344
self: self,
342345
peerLedger: newPeerLedger(),
343-
pendingGauge: bmetrics.PendingEngineGauge(),
344-
activeGauge: bmetrics.ActiveEngineGauge(),
346+
pendingGauge: bmetrics.PendingEngineGauge(ctx),
347+
activeGauge: bmetrics.ActiveEngineGauge(ctx),
345348
targetMessageSize: defaultTargetMessageSize,
346349
tagQueued: fmt.Sprintf(tagFormat, "queued", uuid.New().String()),
347350
tagUseful: fmt.Sprintf(tagFormat, "useful", uuid.New().String()),
@@ -351,7 +354,7 @@ func newEngine(
351354
opt(e)
352355
}
353356

354-
e.bsm = newBlockstoreManager(bs, e.bstoreWorkerCount, bmetrics.PendingBlocksGauge(), bmetrics.ActiveBlocksGauge())
357+
e.bsm = newBlockstoreManager(bs, e.bstoreWorkerCount, bmetrics.PendingBlocksGauge(ctx), bmetrics.ActiveBlocksGauge(ctx))
355358

356359
// default peer task queue options
357360
peerTaskQueueOpts := []peertaskqueue.Option{

server/internal/decision/engine_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func newEngineForTesting(
192192
opts ...Option,
193193
) *Engine {
194194
return newEngine(
195+
ctx,
195196
bs,
196197
peerTagger,
197198
self,

server/server.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/ipfs/go-bitswap/tracer"
1818
blocks "github.com/ipfs/go-block-format"
1919
"github.com/ipfs/go-cid"
20-
"github.com/ipfs/go-ipfs-blockstore"
20+
blockstore "github.com/ipfs/go-ipfs-blockstore"
2121
logging "github.com/ipfs/go-log"
2222
"github.com/ipfs/go-metrics-interface"
2323
process "github.com/jbenet/goprocess"
@@ -85,8 +85,8 @@ func New(ctx context.Context, network bsnet.BitSwapNetwork, bstore blockstore.Bl
8585
}()
8686

8787
s := &Server{
88-
sentHistogram: bmetrics.SentHist(),
89-
sendTimeHistogram: bmetrics.SendTimeHist(),
88+
sentHistogram: bmetrics.SentHist(ctx),
89+
sendTimeHistogram: bmetrics.SendTimeHist(ctx),
9090
taskWorkerCount: defaults.BitswapTaskWorkerCount,
9191
network: network,
9292
process: px,
@@ -100,8 +100,8 @@ func New(ctx context.Context, network bsnet.BitSwapNetwork, bstore blockstore.Bl
100100
o(s)
101101
}
102102

103-
// Set up decision engine
104103
s.engine = decision.NewEngine(
104+
ctx,
105105
bstore,
106106
network.ConnectionManager(),
107107
network.Self(),

0 commit comments

Comments
 (0)