Skip to content

Commit

Permalink
Merge pull request #30 from ethpandaops/feat/go-eth2-log-level
Browse files Browse the repository at this point in the history
fix: Pass through log level to go-eth2-api
  • Loading branch information
samcm authored Dec 2, 2024
2 parents 34c009c + 0eb863a commit 138511b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 7 deletions.
4 changes: 4 additions & 0 deletions pkg/beacon/beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/ethpandaops/beacon/pkg/beacon/state"
"github.com/ethpandaops/ethwallclock"
"github.com/go-co-op/gocron"
"github.com/rs/zerolog"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -145,6 +146,9 @@ type Node interface {
OnFinalityCheckpointUpdated(ctx context.Context, handler func(ctx context.Context, event *FinalityCheckpointUpdated) error)
// OnFirstTimeHealthy is called when the node is healthy for the first time.
OnFirstTimeHealthy(ctx context.Context, handler func(ctx context.Context, event *FirstTimeHealthyEvent) error)

// GetZeroLogLevel returns the zerolog level for the node.
GetZeroLogLevel() zerolog.Level
}

// Node represents an Ethereum beacon node. It computes values based on the spec.
Expand Down
5 changes: 3 additions & 2 deletions pkg/beacon/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
eth2client "github.com/attestantio/go-eth2-client"
ehttp "github.com/attestantio/go-eth2-client/http"
"github.com/ethpandaops/beacon/pkg/beacon/api"
"github.com/rs/zerolog"
)

// ensureClients ensures that the node has a client and an API client.
func (n *node) ensureClients(ctx context.Context) error {
failures := 0

zerologLevel := n.GetZeroLogLevel()

for {
if n.client != nil {
_, isProvider := n.client.(eth2client.NodeSyncingProvider)
Expand All @@ -31,7 +32,7 @@ func (n *node) ensureClients(ctx context.Context) error {

client, err := ehttp.New(ctx,
ehttp.WithAddress(n.config.Addr),
ehttp.WithLogLevel(zerolog.Disabled),
ehttp.WithLogLevel(zerologLevel),
ehttp.WithTimeout(timeout),
ehttp.WithExtraHeaders(n.config.Headers),
)
Expand Down
43 changes: 43 additions & 0 deletions pkg/beacon/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package beacon

import (
"github.com/rs/zerolog"
"github.com/sirupsen/logrus"
)

func (n *node) GetZeroLogLevel() zerolog.Level {
if n.log == nil {
return zerolog.NoLevel
}

var logLevel logrus.Level

// Handle both Logger and Entry types
switch v := n.log.(type) {
case *logrus.Logger:
logLevel = v.GetLevel()
case *logrus.Entry:
logLevel = v.Logger.GetLevel()
default:
return zerolog.NoLevel
}

zerologLevel := zerolog.NoLevel

switch logLevel {
case logrus.DebugLevel:
zerologLevel = zerolog.DebugLevel
case logrus.InfoLevel:
zerologLevel = zerolog.InfoLevel
case logrus.WarnLevel:
zerologLevel = zerolog.WarnLevel
case logrus.ErrorLevel:
zerologLevel = zerolog.ErrorLevel
case logrus.FatalLevel:
zerologLevel = zerolog.FatalLevel
case logrus.PanicLevel:
zerologLevel = zerolog.PanicLevel
}

return zerologLevel
}
61 changes: 61 additions & 0 deletions pkg/beacon/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package beacon_test

import (
"testing"

"github.com/ethpandaops/beacon/pkg/beacon"
"github.com/rs/zerolog"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func TestGetZeroLogLevel(t *testing.T) {
tests := []struct {
name string
logLevel logrus.Level
want zerolog.Level
}{
{
name: "debug level",
logLevel: logrus.DebugLevel,
want: zerolog.DebugLevel,
},
{
name: "info level",
logLevel: logrus.InfoLevel,
want: zerolog.InfoLevel,
},
{
name: "warn level",
logLevel: logrus.WarnLevel,
want: zerolog.WarnLevel,
},
{
name: "error level",
logLevel: logrus.ErrorLevel,
want: zerolog.ErrorLevel,
},
{
name: "fatal level",
logLevel: logrus.FatalLevel,
want: zerolog.FatalLevel,
},
{
name: "panic level",
logLevel: logrus.PanicLevel,
want: zerolog.PanicLevel,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logger := logrus.New()
logger.SetLevel(tt.logLevel)

node := beacon.NewNode(logger, &beacon.Config{}, "", beacon.Options{})
got := node.GetZeroLogLevel()

assert.Equal(t, tt.want, got)
})
}
}
9 changes: 4 additions & 5 deletions pkg/beacon/metrics_fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func (f *ForkMetrics) Start(ctx context.Context) error {
func (f *ForkMetrics) Stop() error {
return nil
}

func (f *ForkMetrics) calculateCurrent(ctx context.Context) error {
slot := f.beacon.Wallclock().Slots().Current()

Expand All @@ -112,12 +111,12 @@ func (f *ForkMetrics) calculateCurrent(ctx context.Context) error {
f.Epochs.Reset()

for _, fork := range spec.ForkEpochs {
f.Epochs.WithLabelValues(string(fork.Name)).Set(float64(fork.Epoch))
f.Epochs.WithLabelValues(fork.Name.String()).Set(float64(fork.Epoch))

if fork.Active(phase0.Epoch(phase0.Slot(slot.Number()) / slotsPerEpoch)) {
f.Activated.WithLabelValues(string(fork.Name)).Set(1)
f.Activated.WithLabelValues(fork.Name.String()).Set(1)
} else {
f.Activated.WithLabelValues(string(fork.Name)).Set(0)
f.Activated.WithLabelValues(fork.Name.String()).Set(0)
}
}

Expand All @@ -127,7 +126,7 @@ func (f *ForkMetrics) calculateCurrent(ctx context.Context) error {
} else {
f.Current.Reset()

f.Current.WithLabelValues(string(current.Name)).Set(1)
f.Current.WithLabelValues(current.Name.String()).Set(1)
}

return nil
Expand Down

0 comments on commit 138511b

Please sign in to comment.