mirror of
https://github.com/hanzoai/telemetry.git
synced 2026-10-30 20:01:49 +00:00
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description This PR provides a solution to propagate context values across batching. This is particularly useful in scenarios where context carries important metadata—such as authentication tokens. **New API** A new API has been added to support this functionality: ``` mergeCtx func(ctx1 context.Context, ctx2 context.Context) context.Context ``` By supplying a custom mergeCtx function, users can control how context values are preserved or combined—for example, by selectively copying specific keys, merging metadata, or keeping the larger value. Note: `context.Context` returned from the above function should not override `Value()` function. **Default Behavior** If mergeCtx is not provided (i.e., left as nil), the batching system defaults to a conservative behavior: no context values are retained from the original incoming contexts. Users who need context propagation must explicitly opt in by defining and supplying a mergeCtx function. <!-- Issue number if applicable --> #### Link to tracking issue https://github.com/open-telemetry/opentelemetry-collector/issues/13320 <!--Describe what testing was performed and which tests were added.--> #### Testing * `batch_context_test.go` checks the `mergeCtx` does not interfere with span link * `partition_batcher_test.go` checks `mergeCtx=nil` works properly <!--Describe the documentation added.--> #### Documentation <!--Please delete paragraphs that you did not use before submitting.-->
40 lines
1.0 KiB
Go
40 lines
1.0 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package queuebatch // import "go.opentelemetry.io/collector/exporter/exporterhelper/internal/queuebatch"
|
|
|
|
import (
|
|
"context"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
type traceContextKeyType int
|
|
|
|
const batchSpanLinksKey traceContextKeyType = iota
|
|
|
|
// LinksFromContext returns a list of trace links registered in the context.
|
|
func LinksFromContext(ctx context.Context) []trace.Link {
|
|
if ctx == nil {
|
|
return []trace.Link{}
|
|
}
|
|
if links, ok := ctx.Value(batchSpanLinksKey).([]trace.Link); ok {
|
|
return links
|
|
}
|
|
return []trace.Link{}
|
|
}
|
|
|
|
func parentsFromContext(ctx context.Context) []trace.Link {
|
|
if spanCtx := trace.SpanContextFromContext(ctx); spanCtx.IsValid() {
|
|
return []trace.Link{{SpanContext: spanCtx}}
|
|
}
|
|
return LinksFromContext(ctx)
|
|
}
|
|
|
|
func contextWithMergedLinks(mergedCtx, ctx1, ctx2 context.Context) context.Context {
|
|
return context.WithValue(
|
|
mergedCtx,
|
|
batchSpanLinksKey,
|
|
append(parentsFromContext(ctx1), parentsFromContext(ctx2)...))
|
|
}
|