1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00

Add option to specify propagators to use with grpctrace Extract and Inject

This commit is contained in:
Anthony J Mirabella
2020-06-01 13:53:56 -04:00
parent aea2257ee4
commit a5c21051c8
+29 -4
View File
@@ -26,6 +26,29 @@ import (
"go.opentelemetry.io/otel/api/trace"
)
// Option is a function that allows configuration of the grpctrace Extract()
// and Inject() functions
type Option func(*config)
type config struct {
propagators propagation.Propagators
}
func newConfig(opts []Option) *config {
c := &config{propagators: global.Propagators()}
for _, o := range opts {
o(c)
}
return c
}
// WithPropagators sets the propagators to use for Extraction and Injection
func WithPropagators(props propagation.Propagators) Option {
return func(c *config) {
c.propagators = props
}
}
type metadataSupplier struct {
metadata *metadata.MD
}
@@ -45,8 +68,9 @@ func (s *metadataSupplier) Set(key string, value string) {
// Inject injects correlation context and span context into the gRPC
// metadata object. This function is meant to be used on outgoing
// requests.
func Inject(ctx context.Context, metadata *metadata.MD) {
propagation.InjectHTTP(ctx, global.Propagators(), &metadataSupplier{
func Inject(ctx context.Context, metadata *metadata.MD, opts ...Option) {
c := newConfig(opts)
propagation.InjectHTTP(ctx, c.propagators, &metadataSupplier{
metadata: metadata,
})
}
@@ -54,8 +78,9 @@ func Inject(ctx context.Context, metadata *metadata.MD) {
// Extract returns the correlation context and span context that
// another service encoded in the gRPC metadata object with Inject.
// This function is meant to be used on incoming requests.
func Extract(ctx context.Context, metadata *metadata.MD) ([]kv.KeyValue, trace.SpanContext) {
ctx = propagation.ExtractHTTP(ctx, global.Propagators(), &metadataSupplier{
func Extract(ctx context.Context, metadata *metadata.MD, opts ...Option) ([]kv.KeyValue, trace.SpanContext) {
c := newConfig(opts)
ctx = propagation.ExtractHTTP(ctx, c.propagators, &metadataSupplier{
metadata: metadata,
})