From 3ecd9052f4729b18def3db45a798636e5126a60a Mon Sep 17 00:00:00 2001 From: Sam Calder-Mason Date: Mon, 2 Dec 2024 13:24:42 +1000 Subject: [PATCH 1/2] fix: Pass through log level to go-eth2-api --- pkg/beacon/beacon.go | 4 ++++ pkg/beacon/bootstrap.go | 5 +++-- pkg/beacon/metrics_fork.go | 9 ++++----- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pkg/beacon/beacon.go b/pkg/beacon/beacon.go index 1ccab71..1329f19 100644 --- a/pkg/beacon/beacon.go +++ b/pkg/beacon/beacon.go @@ -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" ) @@ -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. diff --git a/pkg/beacon/bootstrap.go b/pkg/beacon/bootstrap.go index da431ba..d77ab7c 100644 --- a/pkg/beacon/bootstrap.go +++ b/pkg/beacon/bootstrap.go @@ -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) @@ -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), ) diff --git a/pkg/beacon/metrics_fork.go b/pkg/beacon/metrics_fork.go index 5c72331..da19a7f 100644 --- a/pkg/beacon/metrics_fork.go +++ b/pkg/beacon/metrics_fork.go @@ -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() @@ -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) } } @@ -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 From 0eb863a470b0e11310f96d212cdb0d8ff021ae55 Mon Sep 17 00:00:00 2001 From: Sam Calder-Mason Date: Mon, 2 Dec 2024 13:25:11 +1000 Subject: [PATCH 2/2] fix: Pass through log level to go-eth2-api --- pkg/beacon/log.go | 43 +++++++++++++++++++++++++++++ pkg/beacon/log_test.go | 61 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 pkg/beacon/log.go create mode 100644 pkg/beacon/log_test.go diff --git a/pkg/beacon/log.go b/pkg/beacon/log.go new file mode 100644 index 0000000..ba49ffe --- /dev/null +++ b/pkg/beacon/log.go @@ -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 +} diff --git a/pkg/beacon/log_test.go b/pkg/beacon/log_test.go new file mode 100644 index 0000000..aff551b --- /dev/null +++ b/pkg/beacon/log_test.go @@ -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) + }) + } +}