Skip to content

Commit dfd2f82

Browse files
Merge pull request #63 from snabble/more-tracing
Pull in tracing functionality from tchibo-integration
2 parents a0c4cd9 + ac07986 commit dfd2f82

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ require (
1717
github.com/felixge/httpsnoop v1.0.3 // indirect
1818
github.com/go-logr/logr v1.2.3 // indirect
1919
github.com/go-logr/stdr v1.2.2 // indirect
20-
github.com/kr/text v0.2.0 // indirect
20+
github.com/kr/pretty v0.3.0 // indirect
2121
github.com/pmezard/go-difflib v1.0.0 // indirect
22+
github.com/rogpeppe/go-internal v1.9.0 // indirect
2223
github.com/uptrace/opentelemetry-go-extra/otelutil v0.1.20 // indirect
2324
go.opentelemetry.io/otel/metric v0.35.0 // indirect
2425
golang.org/x/sys v0.4.0 // indirect

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbV
1010
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
1111
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
1212
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
13-
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
1413
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
14+
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
1515
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1616
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
1717
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1818
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
1919
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2020
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
21+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
2122
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
2223
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
2324
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

tracex/trace.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ func TraceAndSpan(ctx context.Context, fields logrus.Fields) {
1717
}
1818
}
1919

20+
// BackgroundContextWithSpan creates a new context with the same span as ctx.
21+
// This is useful for tracing functions that should not be interrupted if the caller cancels or times out.
22+
func BackgroundContextWithSpan(ctx context.Context) context.Context {
23+
return trace.ContextWithSpan(context.Background(), trace.SpanFromContext(ctx))
24+
}
25+
2026
type Tracer = trace.Tracer
2127

2228
// GetTracer returns the default otel tracer. You probably want to use NewTracerProvider instead.

tracex/trace_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package tracex
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"go.opentelemetry.io/otel/trace"
9+
)
10+
11+
func Test_BackgroundContextWithSpan(t *testing.T) {
12+
originalContextWithCancel, cancel := context.WithCancel(context.Background())
13+
originalCtx, originalSpan := trace.NewNoopTracerProvider().Tracer("").Start(originalContextWithCancel, "spanName")
14+
newCtxWithSameSpan := BackgroundContextWithSpan(originalCtx)
15+
trace.SpanContextFromContext(newCtxWithSameSpan).Equal(originalSpan.SpanContext())
16+
17+
cancel()
18+
assert.NotNil(t, originalCtx.Err())
19+
20+
assert.Nil(t, newCtxWithSameSpan.Err())
21+
}

0 commit comments

Comments
 (0)