1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-25 22:41:46 +02:00

propagation: extract of multiple header values (#5973)

* Add `ValuesGetter` interface, an optional `TextMapCarrier` feature
that adds `Values(string) []string`.
* `HeaderCarrier` implements `ValuesGetter`.
* Change `Baggage` to use the `Values()` method if it's implemented.

Notable comment:
https://github.com/open-telemetry/opentelemetry-go/pull/5973#discussion_r2074894706

Adds tests extracting requests with multiple 'baggage' headers set.
Does not introduce any breaking changes or alter any existing tests.

Spec issue:
https://github.com/open-telemetry/opentelemetry-specification/issues/433
Corresponding Java prototype:
https://github.com/open-telemetry/opentelemetry-java/pull/6852

---------

Co-authored-by: Robert Pająk <pellared@hotmail.com>
Co-authored-by: dmathieu <42@dmathieu.com>
This commit is contained in:
James Moessis
2025-05-15 17:14:35 +10:00
committed by GitHub
parent 0385f838f0
commit f410084b21
4 changed files with 132 additions and 7 deletions

View File

@@ -9,6 +9,7 @@ import (
)
// TextMapCarrier is the storage medium used by a TextMapPropagator.
// See ValuesGetter for how a TextMapCarrier can get multiple values for a key.
type TextMapCarrier interface {
// DO NOT CHANGE: any modification will not be backwards compatible and
// must never be done outside of a new major release.
@@ -29,6 +30,18 @@ type TextMapCarrier interface {
// must never be done outside of a new major release.
}
// ValuesGetter can return multiple values for a single key,
// with contrast to TextMapCarrier.Get which returns a single value.
type ValuesGetter interface {
// DO NOT CHANGE: any modification will not be backwards compatible and
// must never be done outside of a new major release.
// Values returns all values associated with the passed key.
Values(key string) []string
// DO NOT CHANGE: any modification will not be backwards compatible and
// must never be done outside of a new major release.
}
// MapCarrier is a TextMapCarrier that uses a map held in memory as a storage
// medium for propagated key-value pairs.
type MapCarrier map[string]string
@@ -55,14 +68,25 @@ func (c MapCarrier) Keys() []string {
return keys
}
// HeaderCarrier adapts http.Header to satisfy the TextMapCarrier interface.
// HeaderCarrier adapts http.Header to satisfy the TextMapCarrier and ValuesGetter interfaces.
type HeaderCarrier http.Header
// Get returns the value associated with the passed key.
// Compile time check that HeaderCarrier implements ValuesGetter.
var _ TextMapCarrier = HeaderCarrier{}
// Compile time check that HeaderCarrier implements TextMapCarrier.
var _ ValuesGetter = HeaderCarrier{}
// Get returns the first value associated with the passed key.
func (hc HeaderCarrier) Get(key string) string {
return http.Header(hc).Get(key)
}
// Values returns all values associated with the passed key.
func (hc HeaderCarrier) Values(key string) []string {
return http.Header(hc).Values(key)
}
// Set stores the key-value pair.
func (hc HeaderCarrier) Set(key string, value string) {
http.Header(hc).Set(key, value)
@@ -89,6 +113,8 @@ type TextMapPropagator interface {
// must never be done outside of a new major release.
// Extract reads cross-cutting concerns from the carrier into a Context.
// Implementations may check if the carrier implements ValuesGetter,
// to support extraction of multiple values per key.
Extract(ctx context.Context, carrier TextMapCarrier) context.Context
// DO NOT CHANGE: any modification will not be backwards compatible and
// must never be done outside of a new major release.