|
| 1 | +package bitswap |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + bstore "github.com/ipfs/boxo/blockstore" |
| 9 | + blocks "github.com/ipfs/go-block-format" |
| 10 | + "github.com/ipfs/go-cid" |
| 11 | + ipld "github.com/ipfs/go-ipld-format" |
| 12 | + "go.opentelemetry.io/otel" |
| 13 | + "go.opentelemetry.io/otel/attribute" |
| 14 | + "go.opentelemetry.io/otel/metric" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + notFoundKey = "not_found" |
| 19 | + failedKey = "failed" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + meter = otel.Meter("bitswap_blockstore") |
| 24 | + _ bstore.Blockstore = (*BlockstoreWithMetrics)(nil) |
| 25 | +) |
| 26 | + |
| 27 | +// BlockstoreWithMetrics is a blockstore that collects metrics on blockstore operations. |
| 28 | +type BlockstoreWithMetrics struct { |
| 29 | + bs bstore.Blockstore |
| 30 | + metrics *metrics |
| 31 | +} |
| 32 | + |
| 33 | +type metrics struct { |
| 34 | + delete metric.Int64Counter |
| 35 | + has metric.Int64Counter |
| 36 | + get metric.Int64Counter |
| 37 | + getSize metric.Int64Counter |
| 38 | + put metric.Int64Counter |
| 39 | + putMany metric.Int64Counter |
| 40 | + allKeysChan metric.Int64Counter |
| 41 | +} |
| 42 | + |
| 43 | +// NewBlockstoreWithMetrics creates a new BlockstoreWithMetrics. |
| 44 | +func NewBlockstoreWithMetrics(bs bstore.Blockstore) (*BlockstoreWithMetrics, error) { |
| 45 | + return &BlockstoreWithMetrics{ |
| 46 | + bs: bs, |
| 47 | + }, nil |
| 48 | +} |
| 49 | + |
| 50 | +// WithMetrics enables metrics collection on the blockstore. |
| 51 | +func (w *BlockstoreWithMetrics) WithMetrics() error { |
| 52 | + del, err := meter.Int64Counter( |
| 53 | + "blockstore_delete_block", |
| 54 | + metric.WithDescription("Blockstore delete block operation"), |
| 55 | + ) |
| 56 | + if err != nil { |
| 57 | + return fmt.Errorf("failed to create blockstore delete block counter: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + has, err := meter.Int64Counter( |
| 61 | + "blockstore_has", |
| 62 | + metric.WithDescription("Blockstore has operation"), |
| 63 | + ) |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("failed to create blockstore has counter: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + get, err := meter.Int64Counter( |
| 69 | + "blockstore_get", |
| 70 | + metric.WithDescription("Blockstore get operation"), |
| 71 | + ) |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("failed to create blockstore get counter: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + getSize, err := meter.Int64Counter( |
| 77 | + "blockstore_get_size", |
| 78 | + metric.WithDescription("Blockstore get size operation"), |
| 79 | + ) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("failed to create blockstore get size counter: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + put, err := meter.Int64Counter( |
| 85 | + "blockstore_put", |
| 86 | + metric.WithDescription("Blockstore put operation"), |
| 87 | + ) |
| 88 | + if err != nil { |
| 89 | + return fmt.Errorf("failed to create blockstore put counter: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + putMany, err := meter.Int64Counter( |
| 93 | + "blockstore_put_many", |
| 94 | + metric.WithDescription("Blockstore put many operation"), |
| 95 | + ) |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("failed to create blockstore put many counter: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + allKeysChan, err := meter.Int64Counter( |
| 101 | + "blockstore_all_keys_chan", |
| 102 | + metric.WithDescription("Blockstore all keys chan operation"), |
| 103 | + ) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("failed to create blockstore all keys chan counter: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + w.metrics = &metrics{ |
| 109 | + delete: del, |
| 110 | + has: has, |
| 111 | + get: get, |
| 112 | + getSize: getSize, |
| 113 | + put: put, |
| 114 | + putMany: putMany, |
| 115 | + allKeysChan: allKeysChan, |
| 116 | + } |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func (w *BlockstoreWithMetrics) DeleteBlock(ctx context.Context, cid cid.Cid) error { |
| 121 | + if w.metrics == nil { |
| 122 | + return w.bs.DeleteBlock(ctx, cid) |
| 123 | + } |
| 124 | + err := w.bs.DeleteBlock(ctx, cid) |
| 125 | + w.metrics.delete.Add(ctx, 1, metric.WithAttributes( |
| 126 | + attribute.Bool(failedKey, err != nil), |
| 127 | + )) |
| 128 | + return err |
| 129 | +} |
| 130 | + |
| 131 | +func (w *BlockstoreWithMetrics) Has(ctx context.Context, cid cid.Cid) (bool, error) { |
| 132 | + if w.metrics == nil { |
| 133 | + return w.bs.Has(ctx, cid) |
| 134 | + } |
| 135 | + has, err := w.bs.Has(ctx, cid) |
| 136 | + notFound := errors.Is(err, ipld.ErrNotFound{}) |
| 137 | + failed := err != nil && !notFound |
| 138 | + w.metrics.has.Add(ctx, 1, metric.WithAttributes( |
| 139 | + attribute.Bool(failedKey, failed), |
| 140 | + attribute.Bool(notFoundKey, notFound), |
| 141 | + )) |
| 142 | + return has, err |
| 143 | +} |
| 144 | + |
| 145 | +func (w *BlockstoreWithMetrics) Get(ctx context.Context, cid cid.Cid) (blocks.Block, error) { |
| 146 | + if w.metrics == nil { |
| 147 | + return w.bs.Get(ctx, cid) |
| 148 | + } |
| 149 | + get, err := w.bs.Get(ctx, cid) |
| 150 | + notFound := errors.Is(err, ipld.ErrNotFound{}) |
| 151 | + failed := err != nil && !notFound |
| 152 | + w.metrics.get.Add(ctx, 1, metric.WithAttributes( |
| 153 | + attribute.Bool(failedKey, failed), |
| 154 | + attribute.Bool(notFoundKey, notFound), |
| 155 | + )) |
| 156 | + return get, err |
| 157 | +} |
| 158 | + |
| 159 | +func (w *BlockstoreWithMetrics) GetSize(ctx context.Context, cid cid.Cid) (int, error) { |
| 160 | + if w.metrics == nil { |
| 161 | + return w.bs.GetSize(ctx, cid) |
| 162 | + } |
| 163 | + size, err := w.bs.GetSize(ctx, cid) |
| 164 | + notFound := errors.Is(err, ipld.ErrNotFound{}) |
| 165 | + failed := err != nil && !notFound |
| 166 | + w.metrics.getSize.Add(ctx, 1, metric.WithAttributes( |
| 167 | + attribute.Bool(failedKey, failed), |
| 168 | + attribute.Bool(notFoundKey, notFound), |
| 169 | + )) |
| 170 | + return size, err |
| 171 | +} |
| 172 | + |
| 173 | +func (w *BlockstoreWithMetrics) Put(ctx context.Context, block blocks.Block) error { |
| 174 | + if w.metrics == nil { |
| 175 | + return w.bs.Put(ctx, block) |
| 176 | + } |
| 177 | + err := w.bs.Put(ctx, block) |
| 178 | + w.metrics.put.Add(ctx, 1, metric.WithAttributes( |
| 179 | + attribute.Bool(failedKey, err != nil), |
| 180 | + )) |
| 181 | + return err |
| 182 | +} |
| 183 | + |
| 184 | +func (w *BlockstoreWithMetrics) PutMany(ctx context.Context, blocks []blocks.Block) error { |
| 185 | + if w.metrics == nil { |
| 186 | + return w.bs.PutMany(ctx, blocks) |
| 187 | + } |
| 188 | + err := w.bs.PutMany(ctx, blocks) |
| 189 | + w.metrics.putMany.Add(ctx, 1, metric.WithAttributes( |
| 190 | + attribute.Bool(failedKey, err != nil), |
| 191 | + )) |
| 192 | + return err |
| 193 | +} |
| 194 | + |
| 195 | +func (w *BlockstoreWithMetrics) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) { |
| 196 | + if w.metrics == nil { |
| 197 | + return w.bs.AllKeysChan(ctx) |
| 198 | + } |
| 199 | + ch, err := w.bs.AllKeysChan(ctx) |
| 200 | + w.metrics.allKeysChan.Add(ctx, 1, metric.WithAttributes( |
| 201 | + attribute.Bool(failedKey, err != nil), |
| 202 | + )) |
| 203 | + return ch, err |
| 204 | +} |
| 205 | + |
| 206 | +func (w *BlockstoreWithMetrics) HashOnRead(enabled bool) { |
| 207 | + w.bs.HashOnRead(enabled) |
| 208 | +} |
0 commit comments