Skip to content

Commit 185b9d4

Browse files
committed
feat(disk): Refactor disk usage calc
1 parent 3a81114 commit 185b9d4

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

example_config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ execution:
1313
- "txpool"
1414
diskUsage:
1515
enabled: false
16+
interval: 30m
1617
directories:
1718
- /data/ethereum

pkg/exporter/config.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package exporter
22

3+
import "github.com/ethpandaops/beacon/pkg/human"
4+
35
// Config holds the configuration for the ethereum sync status tool.
46
type Config struct {
57
// Execution is the execution node to use.
@@ -29,8 +31,9 @@ type ExecutionNode struct {
2931

3032
// DiskUsage configures the exporter to expose disk usage stats for these directories.
3133
type DiskUsage struct {
32-
Enabled bool `yaml:"enabled"`
33-
Directories []string `yaml:"directories"`
34+
Enabled bool `yaml:"enabled"`
35+
Directories []string `yaml:"directories"`
36+
Interval human.Duration `yaml:"interval"`
3437
}
3538

3639
// PairConfig holds the config for a Pair of Execution and Consensus Clients

pkg/exporter/disk/disk.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,33 @@ type diskUsage struct {
2121
log logrus.FieldLogger
2222
metrics Metrics
2323
directories []string
24+
interval time.Duration
2425
}
2526

2627
// NewUsage returns a new DiskUsage instance.
27-
func NewUsage(ctx context.Context, log logrus.FieldLogger, namespace string, directories []string) (UsageMetrics, error) {
28+
func NewUsage(ctx context.Context, log logrus.FieldLogger, namespace string, directories []string, interval time.Duration) (UsageMetrics, error) {
2829
return &diskUsage{
2930
log: log,
3031
metrics: NewMetrics(log, namespace),
3132
directories: directories,
33+
interval: interval,
3234
}, nil
3335
}
3436

3537
func (d *diskUsage) StartAsync(ctx context.Context) {
38+
d.log.WithField("directories", d.directories).Info("Starting disk usage metrics...")
39+
40+
_, err := d.GetUsage(ctx, d.directories)
41+
if err != nil {
42+
d.log.WithError(err).Error("Failed to get disk usage")
43+
}
44+
3645
go func() {
3746
for {
3847
select {
3948
case <-ctx.Done():
4049
return
41-
case <-time.After(time.Second * 60):
50+
case <-time.After(d.interval):
4251
if _, err := d.GetUsage(ctx, d.directories); err != nil {
4352
d.log.WithError(err).Error("Failed to get disk usage")
4453
}

pkg/exporter/exporter.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ func (e *exporter) Init(ctx context.Context) error {
5353
if e.config.Execution.Enabled {
5454
e.log.WithField("modules", strings.Join(e.config.Execution.Modules, ", ")).Info("Initializing execution...")
5555

56-
executionNode, err := execution.NewExecutionNode(ctx, e.log.WithField("exporter", "execution"), fmt.Sprintf("%s_exe", e.namespace), e.config.Execution.Name, e.config.Execution.URL, e.config.Execution.Modules)
56+
executionNode, err := execution.NewExecutionNode(
57+
ctx,
58+
e.log.WithField("exporter", "execution"),
59+
fmt.Sprintf("%s_exe", e.namespace),
60+
e.config.Execution.Name,
61+
e.config.Execution.URL,
62+
e.config.Execution.Modules,
63+
)
5764
if err != nil {
5865
return err
5966
}
@@ -68,7 +75,18 @@ func (e *exporter) Init(ctx context.Context) error {
6875
if e.config.DiskUsage.Enabled {
6976
e.log.Info("Initializing disk usage...")
7077

71-
diskUsage, err := disk.NewUsage(ctx, e.log.WithField("exporter", "disk"), fmt.Sprintf("%s_disk", e.namespace), e.config.DiskUsage.Directories)
78+
interval := e.config.DiskUsage.Interval.Duration
79+
if interval == 0 {
80+
interval = 30 * time.Minute
81+
}
82+
83+
diskUsage, err := disk.NewUsage(
84+
ctx,
85+
e.log.WithField("exporter", "disk"),
86+
fmt.Sprintf("%s_disk", e.namespace),
87+
e.config.DiskUsage.Directories,
88+
interval,
89+
)
7290
if err != nil {
7391
return err
7492
}
@@ -130,7 +148,7 @@ func (e *exporter) Serve(ctx context.Context, port int) error {
130148
return nil
131149
}
132150

133-
func (e *exporter) bootstrapConsensusClients(ctx context.Context) error {
151+
func (e *exporter) bootstrapConsensusClients(_ context.Context) error {
134152
opts := *beacon.DefaultOptions().
135153
EnableDefaultBeaconSubscription().
136154
EnablePrometheusMetrics()

0 commit comments

Comments
 (0)