2019-11-27 02:14:09 +02:00
|
|
|
// Copyright 2019, OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/api/core"
|
2020-02-14 09:16:05 +02:00
|
|
|
"go.opentelemetry.io/otel/api/correlation"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-02-20 20:31:21 +02:00
|
|
|
tcPropagator = trace.DefaultHTTPPropagator()
|
|
|
|
ccPropagator = correlation.DefaultHTTPPropagator()
|
|
|
|
propagators = propagation.New(
|
|
|
|
propagation.WithInjectors(tcPropagator, ccPropagator),
|
|
|
|
propagation.WithExtractors(tcPropagator, ccPropagator),
|
|
|
|
)
|
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.
|
2019-11-27 02:14:09 +02:00
|
|
|
func Inject(ctx context.Context, metadata *metadata.MD) {
|
2020-02-14 09:16:05 +02:00
|
|
|
propagation.InjectHTTP(ctx, 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.
|
2019-11-27 02:14:09 +02:00
|
|
|
func Extract(ctx context.Context, metadata *metadata.MD) ([]core.KeyValue, core.SpanContext) {
|
2020-02-14 09:16:05 +02:00
|
|
|
ctx = propagation.ExtractHTTP(ctx, propagators, &metadataSupplier{
|
2019-11-27 02:14:09 +02:00
|
|
|
metadata: metadata,
|
|
|
|
})
|
|
|
|
|
2020-02-14 09:16:05 +02:00
|
|
|
spanContext := trace.RemoteSpanContextFromContext(ctx)
|
2019-11-27 02:14:09 +02:00
|
|
|
var correlationCtxKVs []core.KeyValue
|
2020-02-27 19:10:52 +02:00
|
|
|
correlation.MapFromContext(ctx).Foreach(func(kv core.KeyValue) bool {
|
2019-11-27 02:14:09 +02:00
|
|
|
correlationCtxKVs = append(correlationCtxKVs, kv)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return correlationCtxKVs, spanContext
|
|
|
|
}
|