| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package queuebatch |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
|
| 9 |
"go.opentelemetry.io/otel/trace" |
| 10 |
) |
| 11 |
|
| 12 |
type traceContextKeyType int |
| 13 |
|
| 14 |
const batchSpanLinksKey traceContextKeyType = iota |
| 15 |
|
| 16 |
|
| 17 |
func LinksFromContext(ctx context.Context) []trace.Link { |
| 18 |
if ctx == nil { |
| 19 |
return []trace.Link{} |
| 20 |
} |
| 21 |
if links, ok := ctx.Value(batchSpanLinksKey).([]trace.Link); ok { |
| 22 |
return links |
| 23 |
} |
| 24 |
return []trace.Link{} |
| 25 |
} |
| 26 |
|
| 27 |
func parentsFromContext(ctx context.Context) []trace.Link { |
| 28 |
if spanCtx := trace.SpanContextFromContext(ctx); spanCtx.IsValid() { |
| 29 |
return []trace.Link{{SpanContext: spanCtx}} |
| 30 |
} |
| 31 |
return LinksFromContext(ctx) |
| 32 |
} |
| 33 |
|
| 34 |
func contextWithMergedLinks(mergedCtx, ctx1, ctx2 context.Context) context.Context { |
| 35 |
return context.WithValue( |
| 36 |
mergedCtx, |
| 37 |
batchSpanLinksKey, |
| 38 |
append(parentsFromContext(ctx1), parentsFromContext(ctx2)...)) |
| 39 |
} |