From 79fb2d0c7c8d57c08c7ad45cbaae59bf9afc1d62 Mon Sep 17 00:00:00 2001 From: Pavel <177363085+pkcll@users.noreply.github.com> Date: Mon, 12 Aug 2024 11:22:07 -0400 Subject: [PATCH] Beholder: remove beholder logger based on opentelemetry-go-extra/otelzap --- go.mod | 2 - go.sum | 4 -- pkg/beholder/client.go | 16 ++++---- pkg/beholder/example_test.go | 56 ++++++++++++++++++-------- pkg/beholder/global/global.go | 21 +--------- pkg/beholder/logger/logger.go | 63 ------------------------------ pkg/beholder/logger/logger_test.go | 29 -------------- 7 files changed, 50 insertions(+), 141 deletions(-) delete mode 100644 pkg/beholder/logger/logger.go delete mode 100644 pkg/beholder/logger/logger_test.go diff --git a/go.mod b/go.mod index 6fedb81d0..9c210662f 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,6 @@ require ( github.com/shopspring/decimal v1.4.0 github.com/smartcontractkit/libocr v0.0.0-20240419185742-fd3cab206b2c github.com/stretchr/testify v1.9.0 - github.com/uptrace/opentelemetry-go-extra/otelzap v0.3.1 go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.52.0 go.opentelemetry.io/otel v1.28.0 go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.0.0-20240801233905-f7977e064c9c @@ -87,7 +86,6 @@ require ( github.com/prometheus/procfs v0.11.1 // indirect github.com/santhosh-tekuri/jsonschema/v5 v5.2.0 github.com/stretchr/objx v0.5.2 // indirect - github.com/uptrace/opentelemetry-go-extra/otelutil v0.3.1 // indirect github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect github.com/x448/float16 v0.8.4 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 // indirect diff --git a/go.sum b/go.sum index 25607cf04..5f25f0974 100644 --- a/go.sum +++ b/go.sum @@ -230,10 +230,6 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/testcontainers/testcontainers-go v0.14.0 h1:h0D5GaYG9mhOWr2qHdEKDXpkce/VlvaYOCzTRi6UBi8= github.com/testcontainers/testcontainers-go v0.14.0/go.mod h1:hSRGJ1G8Q5Bw2gXgPulJOLlEBaYJHeBSOkQM5JLG+JQ= -github.com/uptrace/opentelemetry-go-extra/otelutil v0.3.1 h1:Suvl9fe12MM0oi8/rcGxlGd7XawNQawU369aHzZFFec= -github.com/uptrace/opentelemetry-go-extra/otelutil v0.3.1/go.mod h1:aiX/F5+EYbY2ed2OQEYRXzMcNGvI9pip5gW2ZtBDers= -github.com/uptrace/opentelemetry-go-extra/otelzap v0.3.1 h1:0iCp8hx3PFhGihubKHxyOCdIlIPxzUr0VsK+rvlMGdk= -github.com/uptrace/opentelemetry-go-extra/otelzap v0.3.1/go.mod h1:FXrjpUJDqwqofvXWG3YNxQwhg2876tUpZASj8VvOMAM= github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc= github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw= github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= diff --git a/pkg/beholder/client.go b/pkg/beholder/client.go index 203bea8d7..cdebed97a 100644 --- a/pkg/beholder/client.go +++ b/pkg/beholder/client.go @@ -37,14 +37,14 @@ type Client interface { Close() error } -var _ Client = (*beholderClient)(nil) +var _ Client = (*otelClient)(nil) type messageEmitter struct { exporter sdklog.Exporter messageLogger otellog.Logger } -type beholderClient struct { +type otelClient struct { config Config // Logger logger otellog.Logger @@ -66,7 +66,7 @@ func NewClient( emitter Emitter, onClose func() error, ) Client { - return &beholderClient{ + return &otelClient{ config: config, logger: logger, tracer: tracer, @@ -249,22 +249,22 @@ func (e messageEmitter) EmitMessage(ctx context.Context, message Message) error return nil } -func (b *beholderClient) Logger() otellog.Logger { +func (b *otelClient) Logger() otellog.Logger { return b.logger } -func (b *beholderClient) Tracer() oteltrace.Tracer { +func (b *otelClient) Tracer() oteltrace.Tracer { return b.tracer } -func (b *beholderClient) Meter() otelmetric.Meter { +func (b *otelClient) Meter() otelmetric.Meter { return b.meter } -func (b *beholderClient) Emitter() Emitter { +func (b *otelClient) Emitter() Emitter { return b.emitter } -func (b *beholderClient) Close() error { +func (b *otelClient) Close() error { if b.closeFunc != nil { return b.closeFunc() } diff --git a/pkg/beholder/example_test.go b/pkg/beholder/example_test.go index 3c3f022fb..26043c93c 100644 --- a/pkg/beholder/example_test.go +++ b/pkg/beholder/example_test.go @@ -20,22 +20,6 @@ import ( func ExampleClient() { ctx := context.Background() - newMessageBytes := func(i int) []byte { - // Create protobuf message - customMessagePb := &pb.CustomMessage{} - customMessagePb.BoolVal = true - customMessagePb.IntVal = int64(i) - customMessagePb.FloatVal = float32(i) - customMessagePb.StringVal = fmt.Sprintf("string-value-%d", i) - customMessagePb.BytesVal = []byte{byte(i)} - // Encode protobuf message - customMessageBytes, err := proto.Marshal(customMessagePb) - if err != nil { - log.Fatalf("Error encoding message: %v", err) - } - return customMessageBytes - } - client, err := beholder.NewOtelClient(beholder.DefaultConfig(), errorHandler) if err != nil { log.Fatalf("Error creating beholder client: %v", err) @@ -80,6 +64,22 @@ func ExampleClient() { // Emitting message 2 } +func newMessageBytes(i int) []byte { + // Create protobuf message + customMessagePb := &pb.CustomMessage{} + customMessagePb.BoolVal = true + customMessagePb.IntVal = int64(i) + customMessagePb.FloatVal = float32(i) + customMessagePb.StringVal = fmt.Sprintf("string-value-%d", i) + customMessagePb.BytesVal = []byte{byte(i)} + // Encode protobuf message + customMessageBytes, err := proto.Marshal(customMessagePb) + if err != nil { + log.Fatalf("Error encoding message: %v", err) + } + return customMessageBytes +} + func errorHandler(e error) { if e != nil { log.Fatalf("otel error: %v", e) @@ -131,3 +131,27 @@ func ExampleEmitter() { // Create metric counter // Emit custom message } + +func ExampleBootstrap() { + beholderConfig := beholder.DefaultConfig() + + // Bootstrap Beholder Client + err := global.Bootstrap(beholderConfig, errorHandler) + if err != nil { + log.Fatalf("Error bootstrapping Beholder: %v", err) + } + + payloadBytes := newMessageBytes(0) + + // Emit custom message + for range 3 { + err := global.Emit(context.Background(), payloadBytes, beholder.Attributes{ + "beholder_data_type": "custom_message", + "foo": "bar", + }) + if err != nil { + log.Printf("Error emitting message: %v", err) + } + } + // Output: +} diff --git a/pkg/beholder/global/global.go b/pkg/beholder/global/global.go index 155f106d6..350d3353e 100644 --- a/pkg/beholder/global/global.go +++ b/pkg/beholder/global/global.go @@ -4,17 +4,13 @@ import ( "context" "sync/atomic" - "github.com/uptrace/opentelemetry-go-extra/otelzap" otellog "go.opentelemetry.io/otel/log" otelmetric "go.opentelemetry.io/otel/metric" oteltrace "go.opentelemetry.io/otel/trace" "github.com/smartcontractkit/chainlink-common/pkg/beholder" - "github.com/smartcontractkit/chainlink-common/pkg/beholder/logger" ) -var log = logger.New() - // Pointer to the global Beholder Client var globalBeholderClient = defaultBeholderClient() @@ -65,11 +61,9 @@ func Emit(ctx context.Context, body []byte, attrs beholder.Attributes) error { return Emitter().Emit(ctx, body, attrs) } -func Bootstrap(cfg beholder.Config) error { +func Bootstrap(cfg beholder.Config, errorHandler func(error)) error { // Initialize beholder client - c, err := beholder.NewOtelClient(cfg, func(err error) { - log.Infof("OTel error %s", err) - }) + c, err := beholder.NewOtelClient(cfg, errorHandler) if err != nil { return err } @@ -82,14 +76,3 @@ func Bootstrap(cfg beholder.Config) error { func NewConfig() beholder.Config { return beholder.DefaultConfig() } - -// Creates logger based on zap logger which writes to stdout -func NewSimpleLogger() logger.Logger { - return logger.New() -} - -// Returns a new logger based on otelzap logger -// The logger is able to write to stdout and send logs to otel collector -func NewLogger() *otelzap.Logger { - return logger.NewOtelzapLogger() -} diff --git a/pkg/beholder/logger/logger.go b/pkg/beholder/logger/logger.go deleted file mode 100644 index 34f1463d3..000000000 --- a/pkg/beholder/logger/logger.go +++ /dev/null @@ -1,63 +0,0 @@ -package logger - -import ( - "log" - "os" - - "github.com/uptrace/opentelemetry-go-extra/otelzap" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" -) - -type Logger interface { - Info(args ...interface{}) - Infow(msg string, keysAndValues ...interface{}) - Infof(format string, values ...interface{}) - - Debug(args ...interface{}) - Debugw(msg string, keysAndValues ...interface{}) - Debugf(format string, values ...interface{}) - - Warn(args ...interface{}) - Warnw(msg string, keysAndValues ...interface{}) - Warnf(format string, values ...interface{}) - - Error(args ...interface{}) - Errorw(msg string, keysAndValues ...interface{}) - Errorf(format string, values ...interface{}) - - Panic(args ...interface{}) - Panicf(format string, values ...interface{}) - - Fatal(args ...interface{}) - Fatalf(format string, values ...interface{}) - - Sync() error -} - -func New() Logger { - return newZapLogger().Sugar() -} - -// NewOtelzapLogger records Zap log messages as messages on the existing span that must be passed in a context.Context as a first argument. -// It does not record anything if the context does not contain a span. -// See more details [here](https://github.com/uptrace/opentelemetry-go-extra/tree/main/otelzap) -func NewOtelzapLogger() *otelzap.Logger { - zl := newZapLogger() - return otelzap.New(newZapLogger(), otelzap.WithMinLevel(zl.Level())) -} - -func newZapLogger() *zap.Logger { - var level zapcore.Level - err := level.UnmarshalText([]byte(os.Getenv("LOG_LEVEL"))) - if err != nil { - log.Fatal(err) - } - config := zap.NewProductionConfig() - config.Level = zap.NewAtomicLevelAt(level) - zl, err := config.Build(zap.AddCallerSkip(0)) - if err != nil { - log.Fatal(err) - } - return zl -} diff --git a/pkg/beholder/logger/logger_test.go b/pkg/beholder/logger/logger_test.go deleted file mode 100644 index fcb015934..000000000 --- a/pkg/beholder/logger/logger_test.go +++ /dev/null @@ -1,29 +0,0 @@ -package logger_test - -import ( - "errors" - "io/fs" - "testing" - - "github.com/smartcontractkit/chainlink-common/pkg/beholder/logger" -) - -func TestLoggerExample(t *testing.T) { - log := logger.New() - log.Info("This is an info log") - log.Error("This is an error log") - log.Debug("This is a debug log") - log.Warn("This is a warning log") - log.Infof("This is a formatted info log with %s", "arguments") - log.Warnf("This is a formatted warning log with %s", "arguments") - log.Errorf("This is a formatted error log with %s", "arguments") - log.Debugf("This is a formatted debug log with %s", "arguments") - log.Infow("This is a structured info log", "key", "value") - log.Warnw("This is a structured warning log", "key", "value") - log.Errorw("This is a structured error log", "key", "value") - log.Debugw("This is a structured debug log", "key", "value") - var pathErr *fs.PathError - if err := log.Sync(); err != nil && !errors.As(err, &pathErr) { - t.Fatal(err) - } -}