Skip to content

Commit

Permalink
feat: support trace id context (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma committed Aug 31, 2023
1 parent dad3f31 commit 347ce16
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
11 changes: 11 additions & 0 deletions ctx/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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())
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
Expand Down
32 changes: 30 additions & 2 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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())
}

Expand All @@ -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())
}

Expand Down Expand Up @@ -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,
})
}

0 comments on commit 347ce16

Please sign in to comment.