1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-20 19:52:56 +02:00
opentelemetry-go/example/grpc/middleware/tracing/tracing.go
Krzesimir Nowak 942713a02d
Replace span relationship with a potentially remote parent context (#451)
This PR removes the non-compliant ChildOf and FollowsFrom interfaces
and the Relation type, which were inherited from OpenTracing via the
initial prototype. Instead allow adding a span context to the go
context as a remote span context and use a simple algorithm for
figuring out an actual parent of the new span, which was proposed for
the OpenTelemetry specification.

Also add a way to ignore current span and remote span context in go
context, so we can force the tracer to create a new root span - a span
with a new trace ID.

That required some moderate changes in the opentracing bridge - first
reference with ChildOfRef reference type becomes a local parent, the
rest become links. This also fixes links handling in the meantime. The
downside of the approach proposed here is that we can only set the
remote parent when creating a span through the opentracing API.

Co-authored-by: Joshua MacDonald <jmacd@users.noreply.github.com>
2020-02-04 08:55:03 -08:00

89 lines
2.9 KiB
Go

// 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 tracing
// gRPC tracing middleware
// https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-rpc.md
import (
"context"
"go.opentelemetry.io/otel/plugin/grpctrace"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/api/correlation"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/key"
"go.opentelemetry.io/otel/api/trace"
)
// UnaryServerInterceptor intercepts and extracts incoming trace data
func UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
requestMetadata, _ := metadata.FromIncomingContext(ctx)
metadataCopy := requestMetadata.Copy()
entries, spanCtx := grpctrace.Extract(ctx, &metadataCopy)
ctx = correlation.WithMap(ctx, correlation.NewMap(correlation.MapUpdate{
MultiKV: entries,
}))
grpcServerKey := key.New("grpc.server")
serverSpanAttrs := []core.KeyValue{
grpcServerKey.String("hello-world-server"),
}
tr := global.TraceProvider().Tracer("example/grpc")
ctx, span := tr.Start(
trace.ContextWithRemoteSpanContext(ctx, spanCtx),
"hello-api-op",
trace.WithAttributes(serverSpanAttrs...),
trace.WithSpanKind(trace.SpanKindServer),
)
defer span.End()
return handler(ctx, req)
}
// UnaryClientInterceptor intercepts and injects outgoing trace
func UnaryClientInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
requestMetadata, _ := metadata.FromOutgoingContext(ctx)
metadataCopy := requestMetadata.Copy()
tr := global.TraceProvider().Tracer("example/grpc")
err := tr.WithSpan(ctx, "hello-api-op",
func(ctx context.Context) error {
grpctrace.Inject(ctx, &metadataCopy)
ctx = metadata.NewOutgoingContext(ctx, metadataCopy)
err := invoker(ctx, method, req, reply, cc, opts...)
setTraceStatus(ctx, err)
return err
})
return err
}
func setTraceStatus(ctx context.Context, err error) {
if err != nil {
s, _ := status.FromError(err)
trace.SpanFromContext(ctx).SetStatus(s.Code())
} else {
trace.SpanFromContext(ctx).SetStatus(codes.OK)
}
}