2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-11-27 02:14:09 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package grpctrace
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
|
2020-02-14 09:16:05 +02:00
|
|
|
"go.opentelemetry.io/otel/api/correlation"
|
2020-03-05 20:12:10 +02:00
|
|
|
"go.opentelemetry.io/otel/api/global"
|
2020-05-14 01:06:03 +02:00
|
|
|
"go.opentelemetry.io/otel/api/kv"
|
2020-02-14 09:16:05 +02:00
|
|
|
"go.opentelemetry.io/otel/api/propagation"
|
2020-01-28 20:13:46 +02:00
|
|
|
"go.opentelemetry.io/otel/api/trace"
|
2019-11-27 02:14:09 +02:00
|
|
|
)
|
|
|
|
|
2020-06-01 19:53:56 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 02:14:09 +02:00
|
|
|
type metadataSupplier struct {
|
|
|
|
metadata *metadata.MD
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *metadataSupplier) Get(key string) string {
|
|
|
|
values := s.metadata.Get(key)
|
2020-02-01 00:03:04 +02:00
|
|
|
if len(values) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return values[0]
|
2019-11-27 02:14:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *metadataSupplier) Set(key string, value string) {
|
2020-02-01 00:03:04 +02:00
|
|
|
s.metadata.Set(key, value)
|
2019-11-27 02:14:09 +02:00
|
|
|
}
|
|
|
|
|
2020-01-27 20:39:21 +02:00
|
|
|
// Inject injects correlation context and span context into the gRPC
|
|
|
|
// metadata object. This function is meant to be used on outgoing
|
|
|
|
// requests.
|
2020-06-01 19:53:56 +02:00
|
|
|
func Inject(ctx context.Context, metadata *metadata.MD, opts ...Option) {
|
|
|
|
c := newConfig(opts)
|
|
|
|
propagation.InjectHTTP(ctx, c.propagators, &metadataSupplier{
|
2019-11-27 02:14:09 +02:00
|
|
|
metadata: metadata,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-27 20:39:21 +02:00
|
|
|
// 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.
|
2020-06-01 19:53:56 +02:00
|
|
|
func Extract(ctx context.Context, metadata *metadata.MD, opts ...Option) ([]kv.KeyValue, trace.SpanContext) {
|
|
|
|
c := newConfig(opts)
|
|
|
|
ctx = propagation.ExtractHTTP(ctx, c.propagators, &metadataSupplier{
|
2019-11-27 02:14:09 +02:00
|
|
|
metadata: metadata,
|
|
|
|
})
|
|
|
|
|
2020-02-14 09:16:05 +02:00
|
|
|
spanContext := trace.RemoteSpanContextFromContext(ctx)
|
2020-05-14 01:06:03 +02:00
|
|
|
var correlationCtxKVs []kv.KeyValue
|
|
|
|
correlation.MapFromContext(ctx).Foreach(func(kv kv.KeyValue) bool {
|
2019-11-27 02:14:09 +02:00
|
|
|
correlationCtxKVs = append(correlationCtxKVs, kv)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return correlationCtxKVs, spanContext
|
|
|
|
}
|