File tree Expand file tree Collapse file tree 4 files changed +31
-2
lines changed Expand file tree Collapse file tree 4 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -17,8 +17,9 @@ require (
17
17
github.com/felixge/httpsnoop v1.0.3 // indirect
18
18
github.com/go-logr/logr v1.2.3 // indirect
19
19
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
21
21
github.com/pmezard/go-difflib v1.0.0 // indirect
22
+ github.com/rogpeppe/go-internal v1.9.0 // indirect
22
23
github.com/uptrace/opentelemetry-go-extra/otelutil v0.1.20 // indirect
23
24
go.opentelemetry.io/otel/metric v0.35.0 // indirect
24
25
golang.org/x/sys v0.4.0 // indirect
Original file line number Diff line number Diff line change @@ -10,14 +10,15 @@ github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbV
10
10
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag =
11
11
github.com/go-logr/stdr v1.2.2 /go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE =
12
12
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38 =
13
- github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI =
14
13
github.com/kr/pretty v0.2.1 /go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI =
14
+ github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0 =
15
15
github.com/kr/pty v1.1.1 /go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ =
16
16
github.com/kr/text v0.1.0 /go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI =
17
17
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY =
18
18
github.com/kr/text v0.2.0 /go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE =
19
19
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM =
20
20
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 =
21
22
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0 =
22
23
github.com/sirupsen/logrus v1.9.0 /go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ =
23
24
github.com/stretchr/objx v0.1.0 /go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME =
Original file line number Diff line number Diff line change @@ -17,6 +17,12 @@ func TraceAndSpan(ctx context.Context, fields logrus.Fields) {
17
17
}
18
18
}
19
19
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
+
20
26
type Tracer = trace.Tracer
21
27
22
28
// GetTracer returns the default otel tracer. You probably want to use NewTracerProvider instead.
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments