Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(disk): Refactor disk usage calc #111

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ execution:
- "txpool"
diskUsage:
enabled: false
interval: 60m
directories:
- /data/ethereum
14 changes: 12 additions & 2 deletions pkg/exporter/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package exporter

import (
"time"

"github.com/ethpandaops/beacon/pkg/human"
)

// Config holds the configuration for the ethereum sync status tool.
type Config struct {
// Execution is the execution node to use.
Expand Down Expand Up @@ -29,8 +35,9 @@ type ExecutionNode struct {

// DiskUsage configures the exporter to expose disk usage stats for these directories.
type DiskUsage struct {
Enabled bool `yaml:"enabled"`
Directories []string `yaml:"directories"`
Enabled bool `yaml:"enabled"`
Directories []string `yaml:"directories"`
Interval human.Duration `yaml:"interval"`
}

// PairConfig holds the config for a Pair of Execution and Consensus Clients
Expand All @@ -55,6 +62,9 @@ func DefaultConfig() *Config {
DiskUsage: DiskUsage{
Enabled: false,
Directories: []string{},
Interval: human.Duration{
Duration: 60 * time.Minute,
},
},
Pair: PairConfig{
Enabled: true,
Expand Down
56 changes: 27 additions & 29 deletions pkg/exporter/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package disk
import (
"context"
"os"
"path/filepath"
"time"

"github.com/sirupsen/logrus"
Expand All @@ -20,24 +21,33 @@ type diskUsage struct {
log logrus.FieldLogger
metrics Metrics
directories []string
interval time.Duration
}

// NewUsage returns a new DiskUsage instance.
func NewUsage(ctx context.Context, log logrus.FieldLogger, namespace string, directories []string) (UsageMetrics, error) {
func NewUsage(ctx context.Context, log logrus.FieldLogger, namespace string, directories []string, interval time.Duration) (UsageMetrics, error) {
return &diskUsage{
log: log,
metrics: NewMetrics(log, namespace),
directories: directories,
interval: interval,
}, nil
}

func (d *diskUsage) StartAsync(ctx context.Context) {
d.log.WithField("directories", d.directories).Info("Starting disk usage metrics...")

_, err := d.GetUsage(ctx, d.directories)
if err != nil {
d.log.WithError(err).Error("Failed to get disk usage")
}

go func() {
for {
select {
case <-ctx.Done():
return
case <-time.After(time.Second * 60):
case <-time.After(d.interval):
if _, err := d.GetUsage(ctx, d.directories); err != nil {
d.log.WithError(err).Error("Failed to get disk usage")
}
Expand All @@ -51,15 +61,17 @@ func (d *diskUsage) GetUsage(ctx context.Context, directories []string) ([]Usage
var diskUsed []Usage

for _, directory := range directories {
info, err := os.Lstat(directory)
_, err := os.Lstat(directory)
if err != nil {
d.log.WithField("directory", directory).Warn("Directory does not exist")

continue
}

used, err := getDiskUsed(directory, info)
used, err := getDiskUsed(directory)
if err != nil {
d.log.WithField("directory", directory).WithError(err).Error("Failed to get usage")

continue
}

Expand All @@ -76,33 +88,19 @@ func (d *diskUsage) GetUsage(ctx context.Context, directories []string) ([]Usage
return diskUsed, nil
}

func getDiskUsed(currentPath string, info os.FileInfo) (int64, error) {
size := info.Size()
if !info.IsDir() {
return size, nil
}

dir, err := os.Open(currentPath)

if err != nil {
return size, err
}
defer dir.Close()

files, err := dir.Readdir(-1)
if err != nil {
return size, err
}

for _, file := range files {
if file.Name() == "." || file.Name() == ".." {
continue
func getDiskUsed(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}

s, _ := getDiskUsed(currentPath+"/"+file.Name(), file)
if !info.IsDir() {
size += info.Size()
}

size += s
}
return err
})

return size, nil
return size, err
}
24 changes: 21 additions & 3 deletions pkg/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ func (e *exporter) Init(ctx context.Context) error {
if e.config.Execution.Enabled {
e.log.WithField("modules", strings.Join(e.config.Execution.Modules, ", ")).Info("Initializing execution...")

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)
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,
)
if err != nil {
return err
}
Expand All @@ -68,7 +75,18 @@ func (e *exporter) Init(ctx context.Context) error {
if e.config.DiskUsage.Enabled {
e.log.Info("Initializing disk usage...")

diskUsage, err := disk.NewUsage(ctx, e.log.WithField("exporter", "disk"), fmt.Sprintf("%s_disk", e.namespace), e.config.DiskUsage.Directories)
interval := e.config.DiskUsage.Interval.Duration
if interval == 0 {
interval = 60 * time.Minute
}

diskUsage, err := disk.NewUsage(
ctx,
e.log.WithField("exporter", "disk"),
fmt.Sprintf("%s_disk", e.namespace),
e.config.DiskUsage.Directories,
interval,
)
if err != nil {
return err
}
Expand Down Expand Up @@ -130,7 +148,7 @@ func (e *exporter) Serve(ctx context.Context, port int) error {
return nil
}

func (e *exporter) bootstrapConsensusClients(ctx context.Context) error {
func (e *exporter) bootstrapConsensusClients(_ context.Context) error {
opts := *beacon.DefaultOptions().
EnableDefaultBeaconSubscription().
EnablePrometheusMetrics()
Expand Down
Loading