Skip to content

Commit 15c3f9b

Browse files
committed
metrics
1 parent 8ee6db4 commit 15c3f9b

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

mod/node-core/pkg/components/block_store.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
blockservice "github.com/berachain/beacon-kit/mod/beacon/block_store"
2727
"github.com/berachain/beacon-kit/mod/config"
2828
"github.com/berachain/beacon-kit/mod/log"
29+
"github.com/berachain/beacon-kit/mod/node-core/pkg/components/metrics"
2930
"github.com/berachain/beacon-kit/mod/node-core/pkg/components/storage"
3031
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
3132
"github.com/berachain/beacon-kit/mod/storage/pkg/block"
@@ -42,9 +43,10 @@ type BlockStoreInput[
4243
] struct {
4344
depinject.In
4445

45-
AppOpts servertypes.AppOptions
46-
ChainSpec common.ChainSpec
47-
Logger LoggerT
46+
AppOpts servertypes.AppOptions
47+
ChainSpec common.ChainSpec
48+
Logger LoggerT
49+
TelemetrySink *metrics.TelemetrySink
4850
}
4951

5052
// ProvideBlockStore is a function that provides the module to the
@@ -64,6 +66,7 @@ func ProvideBlockStore[
6466
storage.NewKVStoreProvider(kvp),
6567
in.ChainSpec,
6668
in.Logger.With("service", manager.BlockStoreName),
69+
in.TelemetrySink,
6770
), nil
6871
}
6972

mod/storage/pkg/block/metrics.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: BUSL-1.1
2+
//
3+
// Copyright (C) 2024, Berachain Foundation. All rights reserved.
4+
// Use of this software is governed by the Business Source License included
5+
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
6+
//
7+
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
8+
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
9+
// VERSIONS OF THE LICENSED WORK.
10+
//
11+
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
12+
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
13+
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
14+
//
15+
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
16+
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
17+
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
18+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
19+
// TITLE.
20+
21+
package block
22+
23+
// storeMetrics is a struct that contains metrics for the block store.
24+
type storeMetrics struct {
25+
// sink is the sink for the metrics.
26+
sink TelemetrySink
27+
}
28+
29+
// newStoreMetrics creates a new storeMetrics.
30+
func newStoreMetrics(sink TelemetrySink) *storeMetrics {
31+
return &storeMetrics{
32+
sink: sink,
33+
}
34+
}
35+
36+
// markPruneBlockFailure marks a block prune failure.
37+
func (sm *storeMetrics) markPruneBlockFailure(err error) {
38+
var labels []string
39+
if err != nil {
40+
labels = append(labels, "reason", "error")
41+
} else {
42+
labels = append(labels, "reason", "panic")
43+
}
44+
45+
sm.sink.IncrementCounter(
46+
"beacon_kit.block-store.prune_block_failure", labels...,
47+
)
48+
}

mod/storage/pkg/block/store.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ type KVStore[BeaconBlockT BeaconBlock[BeaconBlockT]] struct {
4545
cs common.ChainSpec
4646
blockCodec *encoding.SSZInterfaceCodec[BeaconBlockT]
4747
logger log.Logger[any]
48+
metrics *storeMetrics
4849
}
4950

5051
// NewStore creates a new block store.
5152
func NewStore[BeaconBlockT BeaconBlock[BeaconBlockT]](
5253
kvsp store.KVStoreService,
5354
cs common.ChainSpec,
5455
logger log.Logger[any],
56+
ts TelemetrySink,
5557
) *KVStore[BeaconBlockT] {
5658
schemaBuilder := sdkcollections.NewSchemaBuilder(kvsp)
5759
blockCodec := &encoding.SSZInterfaceCodec[BeaconBlockT]{}
@@ -67,6 +69,7 @@ func NewStore[BeaconBlockT BeaconBlock[BeaconBlockT]](
6769
blockCodec: blockCodec,
6870
cs: cs,
6971
logger: logger,
72+
metrics: newStoreMetrics(ts),
7073
}
7174
}
7275

@@ -112,7 +115,7 @@ func (kv *KVStore[BeaconBlockT]) Prune(start, end uint64) error {
112115
func (kv *KVStore[BeaconBlockT]) prune(ctx context.Context, slot math.Slot) {
113116
defer func() {
114117
if r := recover(); r != nil {
115-
// TODO: add metrics here.
118+
kv.metrics.markPruneBlockFailure(nil)
116119
kv.logger.Warn(
117120
"‼️ panic occurred while pruning block",
118121
"slot", slot, "panic", r, "stack", debug.Stack(),
@@ -130,8 +133,7 @@ func (kv *KVStore[BeaconBlockT]) prune(ctx context.Context, slot math.Slot) {
130133
// case, we choose not to retry removal and instead
131134
// continue. This means this slot may never be pruned, but
132135
// ensures that we always get to pruning subsequent slots.
133-
//
134-
// TODO: add metrics here.
136+
kv.metrics.markPruneBlockFailure(err)
135137
kv.logger.Warn(
136138
"‼️ failed to prune block", "slot", slot, "error", err,
137139
)

mod/storage/pkg/block/types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
package block
2222

2323
import (
24+
"time"
25+
2426
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
2527
"github.com/berachain/beacon-kit/mod/primitives/pkg/constraints"
2628
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
@@ -36,3 +38,14 @@ type BeaconBlock[T any] interface {
3638
SetStateRoot(root common.Root)
3739
GetStateRoot() common.Root
3840
}
41+
42+
// TelemetrySink is an interface for sending metrics to a telemetry backend.
43+
type TelemetrySink interface {
44+
// IncrementCounter increments the counter identified by
45+
// the provided key.
46+
IncrementCounter(key string, args ...string)
47+
48+
// MeasureSince measures the time since the provided start time,
49+
// identified by the provided keys.
50+
MeasureSince(key string, start time.Time, args ...string)
51+
}

0 commit comments

Comments
 (0)