From f46a1e252b9115545a83c57daad498e4672daca7 Mon Sep 17 00:00:00 2001 From: zstone12 <522089185@qq.com> Date: Thu, 24 Oct 2024 21:38:14 +0800 Subject: [PATCH 01/11] feat: init file --- logging/slog/logger.go | 1 + logging/slog/logger_test.go | 1 + logging/slog/option.go | 1 + logging/slog/utils.go | 1 + 4 files changed, 4 insertions(+) create mode 100644 logging/slog/logger.go create mode 100644 logging/slog/logger_test.go create mode 100644 logging/slog/option.go create mode 100644 logging/slog/utils.go diff --git a/logging/slog/logger.go b/logging/slog/logger.go new file mode 100644 index 0000000..c797bba --- /dev/null +++ b/logging/slog/logger.go @@ -0,0 +1 @@ +package slog diff --git a/logging/slog/logger_test.go b/logging/slog/logger_test.go new file mode 100644 index 0000000..c797bba --- /dev/null +++ b/logging/slog/logger_test.go @@ -0,0 +1 @@ +package slog diff --git a/logging/slog/option.go b/logging/slog/option.go new file mode 100644 index 0000000..c797bba --- /dev/null +++ b/logging/slog/option.go @@ -0,0 +1 @@ +package slog diff --git a/logging/slog/utils.go b/logging/slog/utils.go new file mode 100644 index 0000000..c797bba --- /dev/null +++ b/logging/slog/utils.go @@ -0,0 +1 @@ +package slog From b4fe74b5166d6a08388bcc570650dc53665a7e94 Mon Sep 17 00:00:00 2001 From: zstone12 <522089185@qq.com> Date: Sun, 27 Oct 2024 17:31:47 +0800 Subject: [PATCH 02/11] feat: slog init func --- logging/slog/handler.go | 76 +++++++++++++++++++++++++++++++ logging/slog/logger.go | 31 +++++++++++++ logging/slog/logger_test.go | 74 ++++++++++++++++++++++++++++++ logging/slog/option.go | 91 +++++++++++++++++++++++++++++++++++++ logging/slog/utils.go | 1 - 5 files changed, 272 insertions(+), 1 deletion(-) create mode 100644 logging/slog/handler.go delete mode 100644 logging/slog/utils.go diff --git a/logging/slog/handler.go b/logging/slog/handler.go new file mode 100644 index 0000000..a1b8754 --- /dev/null +++ b/logging/slog/handler.go @@ -0,0 +1,76 @@ +package slog + +import ( + "context" + "errors" + "io" + "log/slog" + + "go.opentelemetry.io/otel/codes" + "go.opentelemetry.io/otel/trace" +) + +const ( + traceIDKey = "trace_id" + spanIDKey = "span_id" + traceFlagsKey = "trace_flags" +) + +type traceConfig struct { + recordStackTraceInSpan bool + errorSpanLevel slog.Level +} + +type traceHandler struct { + slog.Handler + tcfg *traceConfig +} + +func NewTraceHandler(w io.Writer, opts *slog.HandlerOptions, traceConfig *traceConfig) *traceHandler { + if opts == nil { + opts = &slog.HandlerOptions{} + } + return &traceHandler{ + slog.NewJSONHandler(w, opts), + traceConfig, + } +} + +func (t *traceHandler) Enabled(ctx context.Context, level slog.Level) bool { + return t.Handler.Enabled(ctx, level) +} + +func (t *traceHandler) Handle(ctx context.Context, record slog.Record) error { + // trace span add + span := trace.SpanFromContext(ctx) + if span.SpanContext().TraceID().IsValid() { + record.Add(traceIDKey, span.SpanContext().TraceID()) + } + if span.SpanContext().SpanID().IsValid() { + record.Add(spanIDKey, span.SpanContext().SpanID()) + } + if span.SpanContext().TraceFlags().IsSampled() { + record.Add(traceFlagsKey, span.SpanContext().TraceFlags()) + } + + // non recording spans do not support modifying + if !span.IsRecording() { + return t.Handler.Handle(ctx, record) + } + + // set span status + if record.Level >= t.tcfg.errorSpanLevel { + span.SetStatus(codes.Error, "") + span.RecordError(errors.New(record.Message), trace.WithStackTrace(t.tcfg.recordStackTraceInSpan)) + } + + return t.Handler.Handle(ctx, record) +} + +func (t *traceHandler) WithAttrs(attrs []slog.Attr) slog.Handler { + return t.Handler.WithAttrs(attrs) +} + +func (t *traceHandler) WithGroup(name string) slog.Handler { + return t.Handler.WithGroup(name) +} diff --git a/logging/slog/logger.go b/logging/slog/logger.go index c797bba..d5d4f86 100644 --- a/logging/slog/logger.go +++ b/logging/slog/logger.go @@ -1 +1,32 @@ package slog + +import ( + "fmt" + "log/slog" +) + +type Writer struct { + log *slog.Logger + config *config +} + +func NewWriter(opts ...Option) *Writer { + cfg := defaultConfig() + + // apply options + for _, opt := range opts { + opt.apply(cfg) + } + + logger := slog.New(NewTraceHandler(cfg.coreConfig.writer, cfg.coreConfig.opt, cfg.traceConfig)) + + return &Writer{log: logger, config: cfg} +} + +func (w *Writer) Logger() *slog.Logger { + return w.log +} + +func (w *Writer) Printf(format string, v ...interface{}) { + w.log.Info(fmt.Sprintf(format, v...)) +} diff --git a/logging/slog/logger_test.go b/logging/slog/logger_test.go index c797bba..0d4c22e 100644 --- a/logging/slog/logger_test.go +++ b/logging/slog/logger_test.go @@ -1 +1,75 @@ package slog + +import ( + "context" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + "gorm.io/driver/sqlite" + "gorm.io/gorm" + "gorm.io/gorm/logger" + "testing" + "time" +) + +func stdoutProvider(ctx context.Context) func() { + provider := sdktrace.NewTracerProvider() + otel.SetTracerProvider(provider) + + exp, err := stdouttrace.New(stdouttrace.WithPrettyPrint()) + if err != nil { + panic(err) + } + + bsp := sdktrace.NewBatchSpanProcessor(exp) + provider.RegisterSpanProcessor(bsp) + + return func() { + if err := provider.Shutdown(ctx); err != nil { + panic(err) + } + } +} + +func TestLogger(t *testing.T) { + ctx := context.Background() + shutdown := stdoutProvider(ctx) + defer shutdown() + + logger := logger.New( + NewWriter(), + logger.Config{ + SlowThreshold: time.Millisecond, + LogLevel: logger.Warn, + Colorful: false, + }, + ) + db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{Logger: logger}) + if err != nil { + panic(err) + } + + db.Logger.Info(ctx, "log from origin logrus") + + tracer := otel.Tracer("test otel std logger") + + ctx, span := tracer.Start(ctx, "root") + + db.Logger.Info(ctx, "hello %s", "world") + + span.End() + + ctx, child := tracer.Start(ctx, "child") + + db.Logger.Warn(ctx, "foo %s", "bar") + + child.End() + + ctx, errSpan := tracer.Start(ctx, "error") + + db.Logger.Error(ctx, "error %s", "this is a error") + + db.Logger.Info(ctx, "no trace context") + + errSpan.End() +} diff --git a/logging/slog/option.go b/logging/slog/option.go index c797bba..f5da355 100644 --- a/logging/slog/option.go +++ b/logging/slog/option.go @@ -1 +1,92 @@ package slog + +import ( + "io" + "log/slog" + "os" +) + +type Option interface { + apply(cfg *config) +} + +type option func(cfg *config) + +func (fn option) apply(cfg *config) { + fn(cfg) +} + +type coreConfig struct { + opt *slog.HandlerOptions + writer io.Writer + level *slog.LevelVar + withLevel bool + withHandlerOptions bool +} + +type config struct { + coreConfig coreConfig + traceConfig *traceConfig +} + +func defaultConfig() *config { + coreConfig := defaultCoreConfig() + return &config{ + coreConfig: *coreConfig, + traceConfig: &traceConfig{ + recordStackTraceInSpan: true, + errorSpanLevel: slog.LevelError, + }, + } +} + +func defaultCoreConfig() *coreConfig { + level := new(slog.LevelVar) + level.Set(slog.LevelInfo) + return &coreConfig{ + opt: &slog.HandlerOptions{ + Level: level, + }, + writer: os.Stdout, + level: level, + withLevel: false, + withHandlerOptions: false, + } +} + +// WithHandlerOptions slog handler-options +func WithHandlerOptions(opt *slog.HandlerOptions) Option { + return option(func(cfg *config) { + cfg.coreConfig.opt = opt + cfg.coreConfig.withHandlerOptions = true + }) +} + +// WithOutput slog writer +func WithOutput(iow io.Writer) Option { + return option(func(cfg *config) { + cfg.coreConfig.writer = iow + }) +} + +// WithLevel slog level +func WithLevel(lvl *slog.LevelVar) Option { + return option(func(cfg *config) { + cfg.coreConfig.level = lvl + cfg.coreConfig.withLevel = true + }) +} + +// WithTraceErrorSpanLevel trace error span level option +func WithTraceErrorSpanLevel(level slog.Level) Option { + return option(func(cfg *config) { + cfg.traceConfig.errorSpanLevel = level + }) +} + +// WithRecordStackTraceInSpan record stack track option +func WithRecordStackTraceInSpan(recordStackTraceInSpan bool) Option { + return option(func(cfg *config) { + cfg.traceConfig.recordStackTraceInSpan = recordStackTraceInSpan + }) +} diff --git a/logging/slog/utils.go b/logging/slog/utils.go deleted file mode 100644 index c797bba..0000000 --- a/logging/slog/utils.go +++ /dev/null @@ -1 +0,0 @@ -package slog From 6349b759270fdd37ecf0b59e018c91f22bcc0aff Mon Sep 17 00:00:00 2001 From: zstone12 <522089185@qq.com> Date: Sun, 27 Oct 2024 18:07:23 +0800 Subject: [PATCH 03/11] feat: split logging go mod --- examples/demo/go.mod | 4 ++- examples/metric/metrics/go.mod | 4 ++- go.mod | 5 ++-- go.sum | 14 +++++++--- logging/logrus/go.mod | 25 +++++++++++++++++ logging/logrus/go.sum | 49 ++++++++++++++++++++++++++++++++++ logging/slog/go.mod | 24 +++++++++++++++++ logging/slog/go.sum | 41 ++++++++++++++++++++++++++++ 8 files changed, 157 insertions(+), 9 deletions(-) create mode 100644 logging/logrus/go.mod create mode 100644 logging/logrus/go.sum create mode 100644 logging/slog/go.mod create mode 100644 logging/slog/go.sum diff --git a/examples/demo/go.mod b/examples/demo/go.mod index 707229b..9f8fee6 100644 --- a/examples/demo/go.mod +++ b/examples/demo/go.mod @@ -1,6 +1,8 @@ module gorm.io/plugin/example -go 1.20 +go 1.21 + +toolchain go1.22.5 replace gorm.io/plugin/opentelemetry => ./../.. diff --git a/examples/metric/metrics/go.mod b/examples/metric/metrics/go.mod index 1467eb4..088cc63 100644 --- a/examples/metric/metrics/go.mod +++ b/examples/metric/metrics/go.mod @@ -1,6 +1,8 @@ module github.com/uptrace/opentelemetry-go-extra/example/prometheus/metrics -go 1.20 +go 1.21 + +toolchain go1.22.5 require ( github.com/prometheus/client_golang v1.17.0 diff --git a/go.mod b/go.mod index cefbee5..9b8c3bc 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,9 @@ module gorm.io/plugin/opentelemetry -go 1.20 +go 1.21 require ( - github.com/sirupsen/logrus v1.9.2 + github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.8.4 go.opentelemetry.io/contrib/instrumentation/runtime v0.44.0 go.opentelemetry.io/contrib/propagators/b3 v1.19.0 @@ -14,7 +14,6 @@ require ( go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.42.0 go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0 - go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0 go.opentelemetry.io/otel/metric v1.19.0 go.opentelemetry.io/otel/sdk v1.19.0 go.opentelemetry.io/otel/sdk/metric v1.19.0 diff --git a/go.sum b/go.sum index 2a244ba..859aa06 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,7 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/glog v1.1.2 h1:DVjP2PbBOzHyzA+dn3WhHIq4NdVu3Q+pvivFICf/7fo= +github.com/golang/glog v1.1.2/go.mod h1:zR+okUeTbrL6EL3xHUDxZuEtGv04p5shwip1+mL/rLQ= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -43,6 +44,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 h1:YBftPWNWd4WwGqtY2yeZL2ef8rHAxPBD8KFhJpmcqms= github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0/go.mod h1:YN5jB8ie0yfIUg6VvR9Kz84aCaG7AsGZnLjhHbUqwPg= @@ -51,15 +53,18 @@ github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkr github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= -github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y= -github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -93,8 +98,6 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 h1:Mne5On7VWdx7omSrSSZ go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0/go.mod h1:IPtUMKL4O3tH5y+iXVyAXqpAwMuzC1IrxVS81rummfE= go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0 h1:3d+S281UTjM+AbF31XSOYn1qXn3BgIdWl8HNEpx08Jk= go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0/go.mod h1:0+KuTDyKL4gjKCF75pHOX4wuzYDUZYfAQdSu43o+Z2I= -go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0 h1:Nw7Dv4lwvGrI68+wULbcq7su9K2cebeCUrDjVrUJHxM= -go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0/go.mod h1:1MsF6Y7gTqosgoZvHlzcaaM8DIMNZgJh87ykokoNH7Y= go.opentelemetry.io/otel/metric v1.19.0 h1:aTzpGtV0ar9wlV4Sna9sdJyII5jTVJEvKETPiOKwvpE= go.opentelemetry.io/otel/metric v1.19.0/go.mod h1:L5rUsV9kM1IxCj1MmSdS+JQAcVm319EUrDVLrt7jqt8= go.opentelemetry.io/otel/sdk v1.19.0 h1:6USY6zH+L8uMH8L3t1enZPR3WFEmSTADlqldyHtJi3o= @@ -106,6 +109,7 @@ go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmY go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I= go.opentelemetry.io/proto/otlp v1.0.0/go.mod h1:Sy6pihPLfYHkr3NkUbEhGHFhINUSI/v80hjKIs5JXpM= go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A= +go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -149,6 +153,7 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d h1:VBu5YqKPv6XiJ199exd8Br+Aetz+o08F+PLMnwJQHAY= +google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d/go.mod h1:yZTlhN0tQnXo3h00fuXNCxJdLdIdnVFVBaRJ5LWBbw4= google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d h1:DoPTO70H+bcDXcd39vOqb2viZxgqeBeSGtZ55yZU4/Q= google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk= google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4= @@ -175,6 +180,7 @@ google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/logging/logrus/go.mod b/logging/logrus/go.mod new file mode 100644 index 0000000..065be0e --- /dev/null +++ b/logging/logrus/go.mod @@ -0,0 +1,25 @@ +module gorm.io/plugin/opentelemetry/logging/logrus + +go 1.22.5 + +require ( + github.com/sirupsen/logrus v1.9.3 + go.opentelemetry.io/otel v1.31.0 + go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0 + go.opentelemetry.io/otel/sdk v1.31.0 + go.opentelemetry.io/otel/trace v1.31.0 + gorm.io/driver/sqlite v1.5.6 + gorm.io/gorm v1.25.12 +) + +require ( + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/mattn/go-sqlite3 v1.14.22 // indirect + go.opentelemetry.io/otel/metric v1.31.0 // indirect + golang.org/x/sys v0.26.0 // indirect + golang.org/x/text v0.14.0 // indirect +) diff --git a/logging/logrus/go.sum b/logging/logrus/go.sum new file mode 100644 index 0000000..0845ec8 --- /dev/null +++ b/logging/logrus/go.sum @@ -0,0 +1,49 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY= +go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0 h1:UGZ1QwZWY67Z6BmckTU+9Rxn04m2bD3gD6Mk0OIOCPk= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0/go.mod h1:fcwWuDuaObkkChiDlhEpSq9+X1C0omv+s5mBtToAQ64= +go.opentelemetry.io/otel/metric v1.31.0 h1:FSErL0ATQAmYHUIzSezZibnyVlft1ybhy4ozRPcF2fE= +go.opentelemetry.io/otel/metric v1.31.0/go.mod h1:C3dEloVbLuYoX41KpmAhOqNriGbA+qqH6PQ5E5mUfnY= +go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H5Gk= +go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0= +go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys= +go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= +golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE= +gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= +gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= +gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= diff --git a/logging/slog/go.mod b/logging/slog/go.mod new file mode 100644 index 0000000..c191c6f --- /dev/null +++ b/logging/slog/go.mod @@ -0,0 +1,24 @@ +module gorm.io/plugin/opentelemetry/logging/slog + +go 1.22.5 + +require ( + go.opentelemetry.io/otel v1.31.0 + go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0 + go.opentelemetry.io/otel/sdk v1.31.0 + go.opentelemetry.io/otel/trace v1.31.0 + gorm.io/driver/sqlite v1.5.6 + gorm.io/gorm v1.25.12 +) + +require ( + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/mattn/go-sqlite3 v1.14.22 // indirect + go.opentelemetry.io/otel/metric v1.31.0 // indirect + golang.org/x/sys v0.26.0 // indirect + golang.org/x/text v0.14.0 // indirect +) diff --git a/logging/slog/go.sum b/logging/slog/go.sum new file mode 100644 index 0000000..1785538 --- /dev/null +++ b/logging/slog/go.sum @@ -0,0 +1,41 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY= +go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0 h1:UGZ1QwZWY67Z6BmckTU+9Rxn04m2bD3gD6Mk0OIOCPk= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0/go.mod h1:fcwWuDuaObkkChiDlhEpSq9+X1C0omv+s5mBtToAQ64= +go.opentelemetry.io/otel/metric v1.31.0 h1:FSErL0ATQAmYHUIzSezZibnyVlft1ybhy4ozRPcF2fE= +go.opentelemetry.io/otel/metric v1.31.0/go.mod h1:C3dEloVbLuYoX41KpmAhOqNriGbA+qqH6PQ5E5mUfnY= +go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H5Gk= +go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0= +go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys= +go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A= +golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= +golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE= +gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= +gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= +gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= From 5665fb3d353f76a3c08043b2179bc1fab0ed01e8 Mon Sep 17 00:00:00 2001 From: zstone12 <522089185@qq.com> Date: Sun, 27 Oct 2024 18:22:37 +0800 Subject: [PATCH 04/11] feat: change go version to 1.21 --- examples/demo/go.mod | 4 +--- examples/demo/go.sum | 1 + examples/metric/metrics/go.mod | 4 +--- examples/metric/metrics/go.sum | 1 + logging/logrus/go.mod | 2 +- logging/slog/go.mod | 2 +- 6 files changed, 6 insertions(+), 8 deletions(-) diff --git a/examples/demo/go.mod b/examples/demo/go.mod index 9f8fee6..9fa31d0 100644 --- a/examples/demo/go.mod +++ b/examples/demo/go.mod @@ -2,8 +2,6 @@ module gorm.io/plugin/example go 1.21 -toolchain go1.22.5 - replace gorm.io/plugin/opentelemetry => ./../.. require ( @@ -22,7 +20,7 @@ require ( github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/mattn/go-sqlite3 v1.14.16 // indirect - github.com/sirupsen/logrus v1.9.2 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect go.opentelemetry.io/otel/metric v1.19.0 // indirect golang.org/x/sys v0.14.0 // indirect ) diff --git a/examples/demo/go.sum b/examples/demo/go.sum index b656db7..8bd678b 100644 --- a/examples/demo/go.sum +++ b/examples/demo/go.sum @@ -17,6 +17,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y= github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= diff --git a/examples/metric/metrics/go.mod b/examples/metric/metrics/go.mod index 088cc63..39d0135 100644 --- a/examples/metric/metrics/go.mod +++ b/examples/metric/metrics/go.mod @@ -2,8 +2,6 @@ module github.com/uptrace/opentelemetry-go-extra/example/prometheus/metrics go 1.21 -toolchain go1.22.5 - require ( github.com/prometheus/client_golang v1.17.0 go.opentelemetry.io/contrib/instrumentation/runtime v0.44.0 @@ -31,7 +29,7 @@ require ( github.com/prometheus/client_model v0.5.0 // indirect github.com/prometheus/common v0.44.0 // indirect github.com/prometheus/procfs v0.11.1 // indirect - github.com/sirupsen/logrus v1.9.2 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect go.opentelemetry.io/otel/metric v1.19.0 // indirect golang.org/x/sys v0.14.0 // indirect google.golang.org/protobuf v1.31.0 // indirect diff --git a/examples/metric/metrics/go.sum b/examples/metric/metrics/go.sum index 6311cef..b3e9683 100644 --- a/examples/metric/metrics/go.sum +++ b/examples/metric/metrics/go.sum @@ -36,6 +36,7 @@ github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwa github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY= github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y= github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= diff --git a/logging/logrus/go.mod b/logging/logrus/go.mod index 065be0e..5fb3d3e 100644 --- a/logging/logrus/go.mod +++ b/logging/logrus/go.mod @@ -1,6 +1,6 @@ module gorm.io/plugin/opentelemetry/logging/logrus -go 1.22.5 +go 1.21 require ( github.com/sirupsen/logrus v1.9.3 diff --git a/logging/slog/go.mod b/logging/slog/go.mod index c191c6f..615102d 100644 --- a/logging/slog/go.mod +++ b/logging/slog/go.mod @@ -1,6 +1,6 @@ module gorm.io/plugin/opentelemetry/logging/slog -go 1.22.5 +go 1.21 require ( go.opentelemetry.io/otel v1.31.0 From 6bbe689f5562482a9626731044ef5bb31d06938f Mon Sep 17 00:00:00 2001 From: zstone12 <522089185@qq.com> Date: Sun, 27 Oct 2024 18:24:07 +0800 Subject: [PATCH 05/11] feat: go mod tidy feat: remove local call chore: link fix style: gofumpt chore: fix link fix --- .github/workflows/pr-check.yaml | 2 +- .github/workflows/tests.yml | 2 +- examples/demo/go.mod | 2 +- examples/metric/metrics/go.mod | 2 +- go.mod | 2 +- logging/logrus/go.mod | 2 +- logging/slog/go.mod | 2 +- logging/slog/logger_test.go | 5 +++-- 8 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.github/workflows/pr-check.yaml b/.github/workflows/pr-check.yaml index db74c9c..f215a13 100644 --- a/.github/workflows/pr-check.yaml +++ b/.github/workflows/pr-check.yaml @@ -11,7 +11,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v3 with: - go-version: '1.20' + go-version: '1.22' - uses: actions/cache@v3 with: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index bc2cf8f..a59ab22 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,7 +11,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: '1.20' + go-version: '1.21' - uses: actions/cache@v3 with: diff --git a/examples/demo/go.mod b/examples/demo/go.mod index 9fa31d0..1ae7827 100644 --- a/examples/demo/go.mod +++ b/examples/demo/go.mod @@ -1,6 +1,6 @@ module gorm.io/plugin/example -go 1.21 +go 1.22 replace gorm.io/plugin/opentelemetry => ./../.. diff --git a/examples/metric/metrics/go.mod b/examples/metric/metrics/go.mod index 39d0135..ac94310 100644 --- a/examples/metric/metrics/go.mod +++ b/examples/metric/metrics/go.mod @@ -1,6 +1,6 @@ module github.com/uptrace/opentelemetry-go-extra/example/prometheus/metrics -go 1.21 +go 1.22 require ( github.com/prometheus/client_golang v1.17.0 diff --git a/go.mod b/go.mod index 9b8c3bc..a3f446f 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module gorm.io/plugin/opentelemetry -go 1.21 +go 1.22 require ( github.com/sirupsen/logrus v1.9.3 diff --git a/logging/logrus/go.mod b/logging/logrus/go.mod index 5fb3d3e..5316b8c 100644 --- a/logging/logrus/go.mod +++ b/logging/logrus/go.mod @@ -1,6 +1,6 @@ module gorm.io/plugin/opentelemetry/logging/logrus -go 1.21 +go 1.22 require ( github.com/sirupsen/logrus v1.9.3 diff --git a/logging/slog/go.mod b/logging/slog/go.mod index 615102d..ff3fdfc 100644 --- a/logging/slog/go.mod +++ b/logging/slog/go.mod @@ -1,6 +1,6 @@ module gorm.io/plugin/opentelemetry/logging/slog -go 1.21 +go 1.22 require ( go.opentelemetry.io/otel v1.31.0 diff --git a/logging/slog/logger_test.go b/logging/slog/logger_test.go index 0d4c22e..d87a407 100644 --- a/logging/slog/logger_test.go +++ b/logging/slog/logger_test.go @@ -2,14 +2,15 @@ package slog import ( "context" + "testing" + "time" + "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" sdktrace "go.opentelemetry.io/otel/sdk/trace" "gorm.io/driver/sqlite" "gorm.io/gorm" "gorm.io/gorm/logger" - "testing" - "time" ) func stdoutProvider(ctx context.Context) func() { From e8e02f03ba4476ac13206b4406159a4984d7ac24 Mon Sep 17 00:00:00 2001 From: zstone12 <522089185@qq.com> Date: Mon, 28 Oct 2024 12:29:46 +0800 Subject: [PATCH 06/11] chore: change ci go version --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a59ab22..2c68d9b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,7 +11,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: '1.21' + go-version: '1.22' - uses: actions/cache@v3 with: From 99150d77d4052b51ffacc8e8e72c106a089acaea Mon Sep 17 00:00:00 2001 From: zstone12 <522089185@qq.com> Date: Mon, 28 Oct 2024 18:12:26 +0800 Subject: [PATCH 07/11] feat: handle config --- logging/slog/logger.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/logging/slog/logger.go b/logging/slog/logger.go index d5d4f86..8f19bf8 100644 --- a/logging/slog/logger.go +++ b/logging/slog/logger.go @@ -17,7 +17,48 @@ func NewWriter(opts ...Option) *Writer { for _, opt := range opts { opt.apply(cfg) } + // When user set the handlerOptions level but not set with coreconfig level + if !cfg.coreConfig.withLevel && cfg.coreConfig.withHandlerOptions && cfg.coreConfig.opt.Level != nil { + lvl := &slog.LevelVar{} + lvl.Set(cfg.coreConfig.opt.Level.Level()) + cfg.coreConfig.level = lvl + } + cfg.coreConfig.opt.Level = cfg.coreConfig.level + + var replaceAttrDefined bool + if cfg.coreConfig.opt.ReplaceAttr == nil { + replaceAttrDefined = false + } else { + replaceAttrDefined = true + } + replaceFunc := cfg.coreConfig.opt.ReplaceAttr + + replaceAttr := func(groups []string, a slog.Attr) slog.Attr { + // default replaceAttr level + if a.Key == slog.LevelKey { + level := a.Value.Any().(slog.Level) + switch level { + case slog.LevelDebug: + a.Value = slog.StringValue("Debug") + case slog.LevelInfo: + a.Value = slog.StringValue("Info") + case slog.LevelWarn: + a.Value = slog.StringValue("Warn") + case slog.LevelError: + a.Value = slog.StringValue("Error") + default: + a.Value = slog.StringValue("Warn") + } + } + // append replaceAttr by user + if replaceAttrDefined { + return replaceFunc(groups, a) + } else { + return a + } + } + cfg.coreConfig.opt.ReplaceAttr = replaceAttr logger := slog.New(NewTraceHandler(cfg.coreConfig.writer, cfg.coreConfig.opt, cfg.traceConfig)) return &Writer{log: logger, config: cfg} From 3e3106028b4b1e7c24621ace46fe4960b334e5d1 Mon Sep 17 00:00:00 2001 From: zstone12 <522089185@qq.com> Date: Mon, 28 Oct 2024 18:32:37 +0800 Subject: [PATCH 08/11] [wip]feat: add unit test --- logging/slog/go.mod | 4 ++ logging/slog/go.sum | 2 + logging/slog/logger_test.go | 95 +++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) diff --git a/logging/slog/go.mod b/logging/slog/go.mod index ff3fdfc..a27da64 100644 --- a/logging/slog/go.mod +++ b/logging/slog/go.mod @@ -3,6 +3,7 @@ module gorm.io/plugin/opentelemetry/logging/slog go 1.22 require ( + github.com/stretchr/testify v1.9.0 go.opentelemetry.io/otel v1.31.0 go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0 go.opentelemetry.io/otel/sdk v1.31.0 @@ -12,13 +13,16 @@ require ( ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/google/uuid v1.6.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/mattn/go-sqlite3 v1.14.22 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect go.opentelemetry.io/otel/metric v1.31.0 // indirect golang.org/x/sys v0.26.0 // indirect golang.org/x/text v0.14.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/logging/slog/go.sum b/logging/slog/go.sum index 1785538..b576999 100644 --- a/logging/slog/go.sum +++ b/logging/slog/go.sum @@ -33,6 +33,8 @@ golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE= diff --git a/logging/slog/logger_test.go b/logging/slog/logger_test.go index d87a407..53b3ec4 100644 --- a/logging/slog/logger_test.go +++ b/logging/slog/logger_test.go @@ -1,7 +1,12 @@ package slog import ( + "bytes" "context" + "github.com/stretchr/testify/assert" + "log/slog" + "os" + "strings" "testing" "time" @@ -74,3 +79,93 @@ func TestLogger(t *testing.T) { errSpan.End() } + +func TestLogger1(t *testing.T) { + ctx := context.Background() + + buf := new(bytes.Buffer) + + shutdown := stdoutProvider(ctx) + defer shutdown() + + logger := logger.New( + NewWriter( + WithTraceErrorSpanLevel(slog.LevelWarn), + WithRecordStackTraceInSpan(true), + ), logger.Config{ + SlowThreshold: time.Millisecond, + LogLevel: logger.Warn, + Colorful: false, + }) + + logger.Info(ctx, "log from origin slog") + assert.True(t, strings.Contains(buf.String(), "log from origin slog")) + buf.Reset() + + tracer := otel.Tracer("test otel std logger") + + ctx, span := tracer.Start(ctx, "root") + + assert.True(t, strings.Contains(buf.String(), "trace_id")) + assert.True(t, strings.Contains(buf.String(), "span_id")) + assert.True(t, strings.Contains(buf.String(), "trace_flags")) + buf.Reset() + + span.End() + + ctx, child := tracer.Start(ctx, "child") + + child.End() + + _, errSpan := tracer.Start(ctx, "error") + + errSpan.End() +} + +func TestLogLevel(t *testing.T) { + buf := new(bytes.Buffer) + + logger := NewWriter( + WithTraceErrorSpanLevel(slog.LevelWarn), + WithRecordStackTraceInSpan(true), + ) + + /* // output to buffer + logger.SetOutput(buf) + + logger.Debug("this is a debug log") + assert.False(t, strings.Contains(buf.String(), "this is a debug log")) + + logger.SetLevel(klog.LevelDebug) + */ + logger.log.Debug("this is a debug log msg") + assert.True(t, strings.Contains(buf.String(), "this is a debug log")) +} + +func TestLogOption(t *testing.T) { + buf := new(bytes.Buffer) + + lvl := new(slog.LevelVar) + lvl.Set(slog.LevelDebug) + logger := NewWriter( + WithLevel(lvl), + WithOutput(buf), + WithHandlerOptions(&slog.HandlerOptions{ + AddSource: true, + ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { + if a.Key == slog.MessageKey { + msg := a.Value.Any().(string) + msg = strings.ReplaceAll(msg, "log", "new log") + a.Value = slog.StringValue(msg) + } + return a + }, + }), + ) + + logger.log.Debug("this is a debug log") + assert.True(t, strings.Contains(buf.String(), "this is a debug new log")) + + dir, _ := os.Getwd() + assert.True(t, strings.Contains(buf.String(), dir)) +} From 9cf34cbc144054575dbeed5c9f4d26a9f110c8fe Mon Sep 17 00:00:00 2001 From: zstone12 <522089185@qq.com> Date: Mon, 28 Oct 2024 18:38:31 +0800 Subject: [PATCH 09/11] style: lint format --- logging/slog/logger_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/logging/slog/logger_test.go b/logging/slog/logger_test.go index 53b3ec4..24b8e70 100644 --- a/logging/slog/logger_test.go +++ b/logging/slog/logger_test.go @@ -3,13 +3,14 @@ package slog import ( "bytes" "context" - "github.com/stretchr/testify/assert" "log/slog" "os" "strings" "testing" "time" + "github.com/stretchr/testify/assert" + "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" sdktrace "go.opentelemetry.io/otel/sdk/trace" From 2554467e0118d16cd9549a36d6755c5d987f69e9 Mon Sep 17 00:00:00 2001 From: zstone12 <522089185@qq.com> Date: Wed, 30 Oct 2024 22:37:45 +0800 Subject: [PATCH 10/11] feat: add set level and test --- logging/slog/logger.go | 12 ++++++++++++ logging/slog/logger_test.go | 36 ++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/logging/slog/logger.go b/logging/slog/logger.go index 8f19bf8..500a2b4 100644 --- a/logging/slog/logger.go +++ b/logging/slog/logger.go @@ -2,6 +2,7 @@ package slog import ( "fmt" + "io" "log/slog" ) @@ -71,3 +72,14 @@ func (w *Writer) Logger() *slog.Logger { func (w *Writer) Printf(format string, v ...interface{}) { w.log.Info(fmt.Sprintf(format, v...)) } + +func (w *Writer) SetLvel(level slog.Level) { + w.config.coreConfig.level.Set(level) + w.config.coreConfig.withLevel = true +} + +func (w *Writer) SetOutput(writer io.Writer) { + log := slog.New(NewTraceHandler(writer, w.config.coreConfig.opt, w.config.traceConfig)) + w.config.coreConfig.writer = writer + w.log = log +} diff --git a/logging/slog/logger_test.go b/logging/slog/logger_test.go index 24b8e70..c8a9015 100644 --- a/logging/slog/logger_test.go +++ b/logging/slog/logger_test.go @@ -81,7 +81,7 @@ func TestLogger(t *testing.T) { errSpan.End() } -func TestLogger1(t *testing.T) { +func TestLoggerBase(t *testing.T) { ctx := context.Background() buf := new(bytes.Buffer) @@ -89,17 +89,13 @@ func TestLogger1(t *testing.T) { shutdown := stdoutProvider(ctx) defer shutdown() - logger := logger.New( - NewWriter( - WithTraceErrorSpanLevel(slog.LevelWarn), - WithRecordStackTraceInSpan(true), - ), logger.Config{ - SlowThreshold: time.Millisecond, - LogLevel: logger.Warn, - Colorful: false, - }) + logger := NewWriter( + WithTraceErrorSpanLevel(slog.LevelWarn), + WithOutput(buf), + WithRecordStackTraceInSpan(true), + ) - logger.Info(ctx, "log from origin slog") + logger.log.InfoContext(ctx, "log from origin slog") assert.True(t, strings.Contains(buf.String(), "log from origin slog")) buf.Reset() @@ -107,9 +103,11 @@ func TestLogger1(t *testing.T) { ctx, span := tracer.Start(ctx, "root") - assert.True(t, strings.Contains(buf.String(), "trace_id")) - assert.True(t, strings.Contains(buf.String(), "span_id")) - assert.True(t, strings.Contains(buf.String(), "trace_flags")) + // TODO fix it + logger.log.WarnContext(ctx, "hello world") + assert.True(t, strings.Contains(buf.String(), "TraceID")) + assert.True(t, strings.Contains(buf.String(), "SpanID")) + assert.True(t, strings.Contains(buf.String(), "TraceFlags")) buf.Reset() span.End() @@ -131,14 +129,12 @@ func TestLogLevel(t *testing.T) { WithRecordStackTraceInSpan(true), ) - /* // output to buffer - logger.SetOutput(buf) + logger.SetOutput(buf) + logger.log.Debug("this is a debug log") + assert.False(t, strings.Contains(buf.String(), "this is a debug log")) - logger.Debug("this is a debug log") - assert.False(t, strings.Contains(buf.String(), "this is a debug log")) + logger.SetLvel(slog.LevelDebug) - logger.SetLevel(klog.LevelDebug) - */ logger.log.Debug("this is a debug log msg") assert.True(t, strings.Contains(buf.String(), "this is a debug log")) } From 5a024b17d6cbc8c7208a7bc203d40a27ade92a8b Mon Sep 17 00:00:00 2001 From: zstone12 <522089185@qq.com> Date: Thu, 21 Nov 2024 08:10:28 +0800 Subject: [PATCH 11/11] feat: improve test --- logging/slog/logger_test.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/logging/slog/logger_test.go b/logging/slog/logger_test.go index c8a9015..43c4442 100644 --- a/logging/slog/logger_test.go +++ b/logging/slog/logger_test.go @@ -89,13 +89,13 @@ func TestLoggerBase(t *testing.T) { shutdown := stdoutProvider(ctx) defer shutdown() - logger := NewWriter( + writer := NewWriter( WithTraceErrorSpanLevel(slog.LevelWarn), WithOutput(buf), WithRecordStackTraceInSpan(true), ) - logger.log.InfoContext(ctx, "log from origin slog") + writer.log.InfoContext(ctx, "log from origin slog") assert.True(t, strings.Contains(buf.String(), "log from origin slog")) buf.Reset() @@ -103,17 +103,26 @@ func TestLoggerBase(t *testing.T) { ctx, span := tracer.Start(ctx, "root") - // TODO fix it - logger.log.WarnContext(ctx, "hello world") - assert.True(t, strings.Contains(buf.String(), "TraceID")) - assert.True(t, strings.Contains(buf.String(), "SpanID")) - assert.True(t, strings.Contains(buf.String(), "TraceFlags")) + writer.log.WarnContext(ctx, "hello world") + assert.True(t, strings.Contains(buf.String(), "trace_id")) + assert.True(t, strings.Contains(buf.String(), "span_id")) + assert.True(t, strings.Contains(buf.String(), "trace_flags")) buf.Reset() span.End() ctx, child := tracer.Start(ctx, "child") - + logger := logger.New( + NewWriter(), + logger.Config{ + SlowThreshold: time.Millisecond, + LogLevel: logger.Info, + Colorful: false, + }, + ) + logger.Info(ctx, "Info %s", "this is a info log") + logger.Warn(ctx, "warn %s", "this is a warn log") + logger.Error(ctx, "error %s", "this is a error log") child.End() _, errSpan := tracer.Start(ctx, "error")