Skip to content

Commit

Permalink
Merge pull request #2 from samcm/feat/disk-usage-node-info
Browse files Browse the repository at this point in the history
Feat(exporter): disk usage & node info
  • Loading branch information
samcm authored May 6, 2022
2 parents 43b6ee6 + 6c42968 commit 31f4c9e
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 23 deletions.
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
initCommon()

err := ethClient.Serve(ctx, metricsPort)
err := export.Serve(ctx, metricsPort)
if err != nil {
logr.Fatal(err)
}
Expand All @@ -28,7 +28,7 @@ var rootCmd = &cobra.Command{
var (
cfgFile string
config *exporter.Config
ethClient exporter.Ethereum
export exporter.Exporter
ctx context.Context
logr logrus.FieldLogger
executionUrl string
Expand Down Expand Up @@ -101,8 +101,8 @@ func initCommon() {
config.Consensus.URL = consensusUrl
}

ethClient = exporter.NewEthereum(log, config)
if err := ethClient.Init(ctx); err != nil {
export = exporter.NewExporter(log, config)
if err := export.Init(ctx); err != nil {
logrus.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var serveCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
initCommon()

err := ethClient.Serve(ctx, metricsPort)
err := export.Serve(ctx, metricsPort)
if err != nil {
logr.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var statusCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
initCommon()

status, err := ethClient.GetSyncStatus(ctx)
status, err := export.GetSyncStatus(ctx)
if err != nil {
logrus.Fatal(err)
}
Expand Down
12 changes: 8 additions & 4 deletions example_config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
pollingFrequencySeconds: 5
pollingFrequencySeconds: 10

consensus:
enabled: true
url: "http://192.168.0.128:5053"
url: "http://localhost:5053"
name: "consensus-client"
execution:
enabled: true
url: "http://192.168.0.128:8545"
name: "execution-client"
url: "http://localhost:8545"
name: "execution-client"
diskUsage:
enabled: false
directories:
- /data/ethereum
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ github.com/prysmaticlabs/go-bitfield v0.0.0-20210809151128-385d8c5e3fb7/go.mod h
github.com/r3labs/sse/v2 v2.7.4 h1:pvCMswPDlXd/ZUFx1dry0LbXJNHXwWPulLcUGYwClc0=
github.com/r3labs/sse/v2 v2.7.4/go.mod h1:hUrYMKfu9WquG9MyI0r6TKiNH+6Sw/QPKm2YbNbU5g8=
github.com/retailnext/hllpp v1.0.1-0.20180308014038-101a6d2f8b52/go.mod h1:RDpi1RftBQPUCDRw6SmxeaREsAaRKnOclghuzp/WRzc=
github.com/ricochet2200/go-disk-usage/du v0.0.0-20210707232629-ac9918953285 h1:d54EL9l+XteliUfUCGsEwwuk65dmmxX85VXF+9T6+50=
github.com/ricochet2200/go-disk-usage/du v0.0.0-20210707232629-ac9918953285/go.mod h1:fxIDly1xtudczrZeOOlfaUvd2OPb2qZAPuWdU2BsBTk=
github.com/rjeczalik/notify v0.9.1/go.mod h1:rKwnCoCGeuQnwBtTSPL9Dad03Vh2n40ePRrjvIXnJho=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
Expand Down
12 changes: 12 additions & 0 deletions pkg/exporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type Config struct {
Consensus ConsensusNode `yaml:"consensus"`
// PollingFrequencySeconds determines how frequently to poll the targets if running in Serve mode.
PollingFrequencySeconds int `yaml:"pollingFrequencySeconds"`
// DiskUsage determines if the disk usage metrics should be exported.
DiskUsage DiskUsage `yaml:"diskUsage"`
}

// ConsensusNode represents a single ethereum consensus client.
Expand All @@ -24,6 +26,12 @@ type ExecutionNode struct {
URL string `yaml:"url"`
}

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

// DefaultConfig represents a sane-default configuration.
func DefaultConfig() *Config {
return &Config{
Expand All @@ -38,5 +46,9 @@ func DefaultConfig() *Config {
URL: "http://localhost:5052",
},
PollingFrequencySeconds: 5,
DiskUsage: DiskUsage{
Enabled: false,
Directories: []string{},
},
}
}
18 changes: 18 additions & 0 deletions pkg/exporter/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Node interface {
Bootstrapped() bool
Bootstrap(ctx context.Context) error
SyncStatus(ctx context.Context) (*SyncStatus, error)
NodeVersion(ctx context.Context) (string, error)
}

type node struct {
Expand Down Expand Up @@ -106,3 +107,20 @@ func (c *node) SyncStatus(ctx context.Context) (*SyncStatus, error) {

return syncStatus, nil
}

func (c *node) NodeVersion(ctx context.Context) (string, error) {
provider, isProvider := c.client.(eth2client.NodeVersionProvider)
if !isProvider {
c.refreshClient(ctx)
return "", errors.New("client does not implement eth2client.NodeVersionProvider")
}

version, err := provider.NodeVersion(ctx)
if err != nil {
return "", err
}

c.metrics.ObserveNodeVersion(version)

return version, nil
}
18 changes: 18 additions & 0 deletions pkg/exporter/consensus/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Metrics interface {
ObserveSyncHeadSlot(slot uint64)
ObserveSyncDistance(slots uint64)
ObserveSyncIsSyncing(syncing bool)
ObserveNodeVersion(version string)
}

type metrics struct {
Expand All @@ -16,6 +17,7 @@ type metrics struct {
syncHeadSlot prometheus.Gauge
syncDistance prometheus.Gauge
syncIsSyncing prometheus.Gauge
nodeVersion *prometheus.GaugeVec
}

func NewMetrics(nodeName, namespace string) Metrics {
Expand Down Expand Up @@ -64,13 +66,25 @@ func NewMetrics(nodeName, namespace string) Metrics {
ConstLabels: constLabels,
},
),
nodeVersion: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "eth_node_version",
Help: "The version of the running beacon node.",
ConstLabels: constLabels,
},
[]string{
"version",
},
),
}

prometheus.MustRegister(m.syncPercentage)
prometheus.MustRegister(m.syncEstimatedHighestSlot)
prometheus.MustRegister(m.syncHeadSlot)
prometheus.MustRegister(m.syncDistance)
prometheus.MustRegister(m.syncIsSyncing)
prometheus.MustRegister(m.nodeVersion)
return m
}

Expand Down Expand Up @@ -98,3 +112,7 @@ func (m *metrics) ObserveSyncIsSyncing(syncing bool) {

m.syncIsSyncing.Set(0)
}

func (m *metrics) ObserveNodeVersion(version string) {
m.nodeVersion.WithLabelValues(version).Set(float64(1))
}
81 changes: 81 additions & 0 deletions pkg/exporter/disk/disk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package disk

import (
"context"
"os"

"github.com/sirupsen/logrus"
)

type DiskUsage interface {
GetUsage(ctx context.Context, directories []string) ([]DiskUsed, error)
}

type diskUsage struct {
log logrus.FieldLogger
metrics Metrics
}

func NewDiskUsage(ctx context.Context, log logrus.FieldLogger, metrics Metrics) (DiskUsage, error) {
return &diskUsage{
log: log,
metrics: metrics,
}, nil
}

func (d *diskUsage) GetUsage(ctx context.Context, directories []string) ([]DiskUsed, error) {
var diskUsed []DiskUsed
for _, directory := range directories {
info, err := os.Lstat(directory)
if err != nil {
d.log.WithField("directory", directory).Warn("Directory does not exist")
continue
}

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

diskUsed = append(diskUsed, DiskUsed{
Directory: directory,
UsageBytes: used,
})
}

for _, disk := range diskUsed {
d.metrics.ObserveDiskUsage(disk)
}

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
}
s, _ := getDiskUsed(currentPath+"/"+file.Name(), file)
size += s
}

return size, nil
}
36 changes: 36 additions & 0 deletions pkg/exporter/disk/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package disk

import "github.com/prometheus/client_golang/prometheus"

type Metrics interface {
ObserveDiskUsage(disk DiskUsed)
}

type metrics struct {
diskUsage *prometheus.GaugeVec
}

func NewMetrics(namespace string) Metrics {
constLabels := make(prometheus.Labels)

m := &metrics{
diskUsage: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "disk_usage",
Help: "How large the directory is (in bytes).",
ConstLabels: constLabels,
},
[]string{
"directory",
},
),
}

prometheus.MustRegister(m.diskUsage)
return m
}

func (m *metrics) ObserveDiskUsage(disk DiskUsed) {
m.diskUsage.WithLabelValues(disk.Directory).Set(float64(disk.UsageBytes))
}
6 changes: 6 additions & 0 deletions pkg/exporter/disk/usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package disk

type DiskUsed struct {
Directory string
UsageBytes int64
}
Loading

0 comments on commit 31f4c9e

Please sign in to comment.