You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
3 years ago
|
package opentelemetry
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"go.opentelemetry.io/otel/baggage"
|
||
|
"go.opentelemetry.io/otel/propagation"
|
||
|
"go.opentelemetry.io/otel/trace"
|
||
|
"google.golang.org/grpc/metadata"
|
||
|
)
|
||
|
|
||
3 years ago
|
// assert that metadataSupplier implements the TextMapCarrier interface
|
||
|
var _ propagation.TextMapCarrier = new(metadataSupplier)
|
||
|
|
||
3 years ago
|
type metadataSupplier struct {
|
||
|
metadata *metadata.MD
|
||
|
}
|
||
|
|
||
|
func (s *metadataSupplier) Get(key string) string {
|
||
|
values := s.metadata.Get(key)
|
||
|
if len(values) == 0 {
|
||
|
return ""
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
return values[0]
|
||
|
}
|
||
|
|
||
3 years ago
|
func (s *metadataSupplier) Set(key, value string) {
|
||
3 years ago
|
s.metadata.Set(key, value)
|
||
|
}
|
||
|
|
||
|
func (s *metadataSupplier) Keys() []string {
|
||
|
out := make([]string, 0, len(*s.metadata))
|
||
|
for key := range *s.metadata {
|
||
|
out = append(out, key)
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
return out
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
// Inject injects the metadata into ctx.
|
||
3 years ago
|
func Inject(ctx context.Context, p propagation.TextMapPropagator, metadata *metadata.MD) {
|
||
|
p.Inject(ctx, &metadataSupplier{
|
||
|
metadata: metadata,
|
||
|
})
|
||
|
}
|
||
|
|
||
3 years ago
|
// Extract extracts the metadata from ctx.
|
||
3 years ago
|
func Extract(ctx context.Context, p propagation.TextMapPropagator, metadata *metadata.MD) (baggage.Baggage, trace.SpanContext) {
|
||
|
ctx = p.Extract(ctx, &metadataSupplier{
|
||
|
metadata: metadata,
|
||
|
})
|
||
|
|
||
|
return baggage.FromContext(ctx), trace.SpanContextFromContext(ctx)
|
||
|
}
|