Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit 431329a

Browse files
committed
refactor: remove metrics object
1 parent 1ac4824 commit 431329a

File tree

7 files changed

+60
-159
lines changed

7 files changed

+60
-159
lines changed

polyfill.go renamed to bitswap.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/ipfs/go-bitswap/client"
88
"github.com/ipfs/go-bitswap/message"
9-
"github.com/ipfs/go-bitswap/metrics"
109
"github.com/ipfs/go-bitswap/network"
1110
"github.com/ipfs/go-bitswap/server"
1211
"github.com/ipfs/go-bitswap/tracer"
@@ -24,7 +23,7 @@ import (
2423
var log = logging.Logger("bitswap")
2524

2625
// old interface we are targeting
27-
type old interface {
26+
type bitswap interface {
2827
Close() error
2928
GetBlock(ctx context.Context, k cid.Cid) (blocks.Block, error)
3029
GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks.Block, error)
@@ -44,7 +43,7 @@ type old interface {
4443
}
4544

4645
var _ exchange.SessionExchange = (*Bitswap)(nil)
47-
var _ old = (*Bitswap)(nil)
46+
var _ bitswap = (*Bitswap)(nil)
4847

4948
type Bitswap struct {
5049
*client.Client
@@ -81,9 +80,8 @@ func New(ctx context.Context, net network.BitSwapNetwork, bstore blockstore.Bloc
8180
serverOptions = append(serverOptions, server.WithTracer(tracer))
8281
}
8382

84-
stats := metrics.New(ctx)
85-
bs.Server = server.New(ctx, net, bstore, stats, serverOptions...)
86-
bs.Client = client.New(ctx, net, bstore, stats, append(clientOptions, client.WithBlockReceivedNotifier(bs.Server))...)
83+
bs.Server = server.New(ctx, net, bstore, serverOptions...)
84+
bs.Client = client.New(ctx, net, bstore, append(clientOptions, client.WithBlockReceivedNotifier(bs.Server))...)
8785
net.Start(bs) // use the polyfill receiver to log received errors and trace messages only once
8886

8987
return bs

client/client.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,14 @@ func WithBlockReceivedNotifier(brn BlockReceivedNotifier) Option {
8282
}
8383

8484
type BlockReceivedNotifier interface {
85-
// ReceivedBlocks notify the decision engine that a peer is well behaving
86-
// and gave us usefull data, potentially increasing it's score and making us
85+
// ReceivedBlocks notifies the decision engine that a peer is well-behaving
86+
// and gave us useful data, potentially increasing its score and making us
8787
// send them more data in exchange.
8888
ReceivedBlocks(peer.ID, []blocks.Block)
8989
}
9090

91-
// New initializes a BitSwap instance that communicates over the provided
92-
// BitSwapNetwork. This function registers the returned instance as the network
93-
// delegate. Runs until context is cancelled or bitswap.Close is called.
94-
func New(parent context.Context, network bsnet.BitSwapNetwork, bstore blockstore.Blockstore, m *bmetrics.Metrics, options ...Option) *Client {
91+
// New initializes a Bitswap client that runs until client.Close is called.
92+
func New(parent context.Context, network bsnet.BitSwapNetwork, bstore blockstore.Blockstore, options ...Option) *Client {
9593
// important to use provided parent context (since it may include important
9694
// loggable data). It's probably not a good idea to allow bitswap to be
9795
// coupled to the concerns of the ipfs daemon in this way.
@@ -155,8 +153,8 @@ func New(parent context.Context, network bsnet.BitSwapNetwork, bstore blockstore
155153
sim: sim,
156154
notif: notif,
157155
counters: new(counters),
158-
dupMetric: m.DupHist(),
159-
allMetric: m.AllHist(),
156+
dupMetric: bmetrics.DupHist(),
157+
allMetric: bmetrics.AllHist(),
160158
provSearchDelay: defaults.ProvSearchDelay,
161159
rebroadcastDelay: delay.Fixed(time.Minute),
162160
simulateDontHavesOnTimeout: true,

metrics/gen.go

Lines changed: 0 additions & 132 deletions
This file was deleted.

metrics/metrics.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package metrics
2+
3+
import (
4+
"github.com/ipfs/go-metrics-interface"
5+
)
6+
7+
var (
8+
// the 1<<18+15 is to observe old file chunks that are 1<<18 + 14 in size
9+
metricsBuckets = []float64{1 << 6, 1 << 10, 1 << 14, 1 << 18, 1<<18 + 15, 1 << 22}
10+
11+
timeMetricsBuckets = []float64{1, 10, 30, 60, 90, 120, 600}
12+
)
13+
14+
func DupHist() metrics.Histogram {
15+
return metrics.New("recv_dup_blocks_bytes", "Summary of duplicate data blocks recived").Histogram(metricsBuckets)
16+
}
17+
18+
func AllHist() metrics.Histogram {
19+
return metrics.New("recv_all_blocks_bytes", "Summary of all data blocks recived").Histogram(metricsBuckets)
20+
}
21+
22+
func SentHist() metrics.Histogram {
23+
return metrics.New("sent_all_blocks_bytes", "Histogram of blocks sent by this bitswap").Histogram(metricsBuckets)
24+
}
25+
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+
}
29+
30+
func PendingEngineGauge() metrics.Gauge {
31+
return metrics.New("pending_tasks", "Total number of pending tasks").Gauge()
32+
}
33+
34+
func ActiveEngineGauge() metrics.Gauge {
35+
return metrics.New("active_tasks", "Total number of active tasks").Gauge()
36+
}
37+
38+
func PendingBlocksGauge() metrics.Gauge {
39+
return metrics.New("pending_block_tasks", "Total number of pending blockstore tasks").Gauge()
40+
}
41+
42+
func ActiveBlocksGauge() metrics.Gauge {
43+
return metrics.New("active_block_tasks", "Total number of active blockstore tasks").Gauge()
44+
}

server/internal/decision/engine.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,13 @@ func NewEngine(
308308
bs bstore.Blockstore,
309309
peerTagger PeerTagger,
310310
self peer.ID,
311-
metrics *bmetrics.Metrics,
312311
opts ...Option,
313312
) *Engine {
314313
return newEngine(
315314
bs,
316315
peerTagger,
317316
self,
318317
maxBlockSizeReplaceHasWithBlock,
319-
metrics,
320318
opts...,
321319
)
322320
}
@@ -326,10 +324,8 @@ func newEngine(
326324
peerTagger PeerTagger,
327325
self peer.ID,
328326
maxReplaceSize int,
329-
metrics *bmetrics.Metrics,
330327
opts ...Option,
331328
) *Engine {
332-
333329
e := &Engine{
334330
ledgerMap: make(map[peer.ID]*ledger),
335331
scoreLedger: NewDefaultScoreLedger(),
@@ -344,8 +340,8 @@ func newEngine(
344340
sendDontHaves: true,
345341
self: self,
346342
peerLedger: newPeerLedger(),
347-
pendingGauge: metrics.PendingEngineGauge(),
348-
activeGauge: metrics.ActiveEngineGauge(),
343+
pendingGauge: bmetrics.PendingEngineGauge(),
344+
activeGauge: bmetrics.ActiveEngineGauge(),
349345
targetMessageSize: defaultTargetMessageSize,
350346
tagQueued: fmt.Sprintf(tagFormat, "queued", uuid.New().String()),
351347
tagUseful: fmt.Sprintf(tagFormat, "useful", uuid.New().String()),
@@ -355,7 +351,7 @@ func newEngine(
355351
opt(e)
356352
}
357353

358-
e.bsm = newBlockstoreManager(bs, e.bstoreWorkerCount, metrics.PendingBlocksGauge(), metrics.ActiveBlocksGauge())
354+
e.bsm = newBlockstoreManager(bs, e.bstoreWorkerCount, bmetrics.PendingBlocksGauge(), bmetrics.ActiveBlocksGauge())
359355

360356
// default peer task queue options
361357
peerTaskQueueOpts := []peertaskqueue.Option{

server/internal/decision/engine_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/ipfs/go-bitswap/internal/testutil"
1515
message "github.com/ipfs/go-bitswap/message"
1616
pb "github.com/ipfs/go-bitswap/message/pb"
17-
"github.com/ipfs/go-bitswap/metrics"
1817

1918
blocks "github.com/ipfs/go-block-format"
2019
"github.com/ipfs/go-cid"
@@ -197,7 +196,6 @@ func newEngineForTesting(
197196
peerTagger,
198197
self,
199198
maxReplaceSize,
200-
metrics.New(ctx),
201199
opts...,
202200
)
203201
}

server/server.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type Server struct {
7878
provideEnabled bool
7979
}
8080

81-
func New(ctx context.Context, network bsnet.BitSwapNetwork, bstore blockstore.Blockstore, m *bmetrics.Metrics, options ...Option) *Server {
81+
func New(ctx context.Context, network bsnet.BitSwapNetwork, bstore blockstore.Blockstore, options ...Option) *Server {
8282
ctx, cancel := context.WithCancel(ctx)
8383

8484
px := process.WithTeardown(func() error {
@@ -90,8 +90,8 @@ func New(ctx context.Context, network bsnet.BitSwapNetwork, bstore blockstore.Bl
9090
}()
9191

9292
s := &Server{
93-
sentHistogram: m.SentHist(),
94-
sendTimeHistogram: m.SendTimeHist(),
93+
sentHistogram: bmetrics.SentHist(),
94+
sendTimeHistogram: bmetrics.SendTimeHist(),
9595
taskWorkerCount: defaults.BitswapTaskWorkerCount,
9696
network: network,
9797
process: px,
@@ -109,7 +109,6 @@ func New(ctx context.Context, network bsnet.BitSwapNetwork, bstore blockstore.Bl
109109
bstore,
110110
network.ConnectionManager(),
111111
network.Self(),
112-
m,
113112
s.engineOptions...,
114113
)
115114
s.engineOptions = nil

0 commit comments

Comments
 (0)