From 347ce1683350a187122a007d21a8d085b01449a4 Mon Sep 17 00:00:00 2001 From: Nicholas Wiersma Date: Thu, 31 Aug 2023 13:08:14 +0200 Subject: [PATCH] feat: support trace id context (#59) --- ctx/ctx.go | 11 +++++++++++ go.mod | 2 ++ go.sum | 10 ++++++++++ logger_test.go | 32 ++++++++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/ctx/ctx.go b/ctx/ctx.go index 4a73504..ad45492 100644 --- a/ctx/ctx.go +++ b/ctx/ctx.go @@ -9,6 +9,7 @@ import ( "github.com/go-stack/stack" "github.com/hamba/logger/v2" + "go.opentelemetry.io/otel/trace" ) // Str returns a string context field. @@ -186,3 +187,13 @@ func Interface(k string, v interface{}) logger.Field { e.AppendInterface(k, v) } } + +// TraceID returns a open telemetry trace ID context field. +func TraceID(k string, span trace.Span) logger.Field { + if !span.IsRecording() { + return func(*logger.Event) {} + } + return func(e *logger.Event) { + e.AppendString(k, span.SpanContext().TraceID().String()) + } +} diff --git a/go.mod b/go.mod index 1ffdc43..c79c126 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,6 @@ go 1.16 require ( github.com/go-stack/stack v1.8.1 github.com/stretchr/testify v1.8.4 + go.opentelemetry.io/otel v1.17.0 + go.opentelemetry.io/otel/trace v1.17.0 ) diff --git a/go.sum b/go.sum index b01f45c..b8ed9f5 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,13 @@ 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.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw= github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4= +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/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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -12,6 +17,11 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +go.opentelemetry.io/otel v1.17.0 h1:MW+phZ6WZ5/uk2nd93ANk/6yJ+dVrvNWUjGhnnFU5jM= +go.opentelemetry.io/otel v1.17.0/go.mod h1:I2vmBGtFaODIVMBSTPVDlJSzBDNf93k60E6Ft0nyjo0= +go.opentelemetry.io/otel/metric v1.17.0/go.mod h1:h4skoxdZI17AxwITdmdZjjYJQH5nzijUUjm+wtPph5o= +go.opentelemetry.io/otel/trace v1.17.0 h1:/SWhSRHmDPOImIAetP1QAeMnZYiQXrTy4fMMYOdSKWQ= +go.opentelemetry.io/otel/trace v1.17.0/go.mod h1:I/4vKTgFclIsXRVucpH25X0mpFSczM7aHeaz0ZBLWjY= 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.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/logger_test.go b/logger_test.go index 565ea48..28dadb2 100644 --- a/logger_test.go +++ b/logger_test.go @@ -13,6 +13,9 @@ import ( "github.com/hamba/logger/v2/ctx" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/codes" + "go.opentelemetry.io/otel/trace" ) func TestLevelFromString(t *testing.T) { @@ -186,6 +189,8 @@ func TestLogger_Context(t *testing.T) { var buf bytes.Buffer log := logger.New(&buf, logger.LogfmtFormat(), logger.Info).With(ctx.Str("_n", "bench"), ctx.Int("_p", 1)) + span := &fakeSpan{ID: byte(2), Recording: true} + _, file, line, _ := runtime.Caller(0) caller := file + ":" + strconv.Itoa(line+3) @@ -213,9 +218,10 @@ func TestLogger_Context(t *testing.T) { ctx.Duration("dur", time.Second), ctx.Interface("obj", obj), ctx.Caller("caller"), + ctx.TraceID("tid", span), ) - want := `lvl=info msg="some message" _n=bench _p=1 str=string strs=string1,string2 bytes=98,121,116,101,115 bool=true int=1 ints=1,2,3 int8=2 int16=3 int32=4 int64=5 uint=1 uint8=2 uint16=3 uint32=4 uint64=5 float32=1.230 float64=4.560 err="test error" error="test error" time=1541573670 dur=1s obj={Name:test} caller=` + caller + "\n" + want := `lvl=info msg="some message" _n=bench _p=1 str=string strs=string1,string2 bytes=98,121,116,101,115 bool=true int=1 ints=1,2,3 int8=2 int16=3 int32=4 int64=5 uint=1 uint8=2 uint16=3 uint32=4 uint64=5 float32=1.230 float64=4.560 err="test error" error="test error" time=1541573670 dur=1s obj={Name:test} caller=` + caller + " tid=01000000000000000000000000000000\n" assert.Equal(t, want, buf.String()) } @@ -225,7 +231,7 @@ func TestLogger_Stack(t *testing.T) { log.Info("some message", ctx.Stack("stack")) - want := `lvl=info msg="some message" stack=[github.com/hamba/logger/logger/logger_test.go:226]` + "\n" + want := `lvl=info msg="some message" stack=[github.com/hamba/logger/logger/logger_test.go:232]` + "\n" assert.Equal(t, want, buf.String()) } @@ -253,3 +259,25 @@ func TestLogger_Writer(t *testing.T) { assert.Equal(t, 13, n) assert.Equal(t, want, buf.String()) } + +type fakeSpan struct { + Recording bool + ID byte +} + +func (s *fakeSpan) End(...trace.SpanEndOption) {} +func (s *fakeSpan) AddEvent(string, ...trace.EventOption) {} +func (s *fakeSpan) IsRecording() bool { return s.Recording } +func (s *fakeSpan) RecordError(error, ...trace.EventOption) {} +func (s *fakeSpan) SetStatus(codes.Code, string) {} +func (s *fakeSpan) SetName(string) {} +func (s *fakeSpan) SetAttributes(...attribute.KeyValue) {} +func (s *fakeSpan) TracerProvider() trace.TracerProvider { return nil } + +func (s *fakeSpan) SpanContext() trace.SpanContext { + return trace.NewSpanContext(trace.SpanContextConfig{ + TraceID: trace.TraceID([16]byte{1}), + SpanID: trace.SpanID([8]byte{s.ID}), + Remote: false, + }) +}