master
Kevin Wan 3 years ago committed by GitHub
parent 6fd08027ff
commit 7618139dad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,7 +5,6 @@ import (
"github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/syncx" "github.com/tal-tech/go-zero/core/syncx"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/jaeger" "go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/propagation"

@ -4,7 +4,6 @@ import (
"net/http" "net/http"
"github.com/tal-tech/go-zero/core/opentelemetry" "github.com/tal-tech/go-zero/core/opentelemetry"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0" semconv "go.opentelemetry.io/otel/semconv/v1.4.0"

@ -8,9 +8,8 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/core/opentelemetry" "github.com/tal-tech/go-zero/core/opentelemetry"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
) )

@ -6,10 +6,9 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/tools/goctl/config" "github.com/tal-tech/go-zero/tools/goctl/config"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
"github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util"
) )

@ -4,12 +4,11 @@ import (
"context" "context"
"github.com/tal-tech/go-zero/core/opentelemetry" "github.com/tal-tech/go-zero/core/opentelemetry"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
"google.golang.org/grpc" "google.golang.org/grpc"
grpc_codes "google.golang.org/grpc/codes" gcodes "google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
) )
@ -23,7 +22,6 @@ func OpenTracingInterceptor() grpc.UnaryClientInterceptor {
requestMetadata, _ := metadata.FromOutgoingContext(ctx) requestMetadata, _ := metadata.FromOutgoingContext(ctx)
metadataCopy := requestMetadata.Copy() metadataCopy := requestMetadata.Copy()
tr := otel.Tracer(opentelemetry.TraceName) tr := otel.Tracer(opentelemetry.TraceName)
name, attr := opentelemetry.SpanInfo(method, cc.Target()) name, attr := opentelemetry.SpanInfo(method, cc.Target())
@ -37,22 +35,18 @@ func OpenTracingInterceptor() grpc.UnaryClientInterceptor {
opentelemetry.Inject(ctx, propagator, &metadataCopy) opentelemetry.Inject(ctx, propagator, &metadataCopy)
ctx = metadata.NewOutgoingContext(ctx, metadataCopy) ctx = metadata.NewOutgoingContext(ctx, metadataCopy)
opentelemetry.MessageSent.Event(ctx, 1, req) opentelemetry.MessageSent.Event(ctx, 1, req)
err := invoker(ctx, method, req, reply, cc, opts...)
opentelemetry.MessageReceived.Event(ctx, 1, reply) opentelemetry.MessageReceived.Event(ctx, 1, reply)
if err != nil { if err := invoker(ctx, method, req, reply, cc, opts...); err != nil {
s, _ := status.FromError(err) s, _ := status.FromError(err)
span.SetStatus(codes.Error, s.Message()) span.SetStatus(codes.Error, s.Message())
span.SetAttributes(opentelemetry.StatusCodeAttr(s.Code())) span.SetAttributes(opentelemetry.StatusCodeAttr(s.Code()))
} else { return err
span.SetAttributes(opentelemetry.StatusCodeAttr(grpc_codes.OK))
} }
return err span.SetAttributes(opentelemetry.StatusCodeAttr(gcodes.OK))
return nil
} }
} }
@ -98,7 +92,7 @@ func StreamOpenTracingInterceptor() grpc.StreamClientInterceptor {
span.SetStatus(codes.Error, s.Message()) span.SetStatus(codes.Error, s.Message())
span.SetAttributes(opentelemetry.StatusCodeAttr(s.Code())) span.SetAttributes(opentelemetry.StatusCodeAttr(s.Code()))
} else { } else {
span.SetAttributes(opentelemetry.StatusCodeAttr(grpc_codes.OK)) span.SetAttributes(opentelemetry.StatusCodeAttr(gcodes.OK))
} }
span.End() span.End()

@ -4,20 +4,20 @@ import (
"context" "context"
"github.com/tal-tech/go-zero/core/opentelemetry" "github.com/tal-tech/go-zero/core/opentelemetry"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
"google.golang.org/grpc" "google.golang.org/grpc"
grpc_codes "google.golang.org/grpc/codes" gcodes "google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
) )
func UnaryOpenTracingInterceptor() grpc.UnaryServerInterceptor { func UnaryOpenTracingInterceptor() grpc.UnaryServerInterceptor {
propagator := otel.GetTextMapPropagator() propagator := otel.GetTextMapPropagator()
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (interface{}, error) {
if !opentelemetry.Enabled() { if !opentelemetry.Enabled() {
return handler(ctx, req) return handler(ctx, req)
} }
@ -41,20 +41,19 @@ func UnaryOpenTracingInterceptor() grpc.UnaryServerInterceptor {
defer span.End() defer span.End()
opentelemetry.MessageReceived.Event(ctx, 1, req) opentelemetry.MessageReceived.Event(ctx, 1, req)
resp, err := handler(ctx, req) resp, err := handler(ctx, req)
if err != nil { if err != nil {
s, _ := status.FromError(err) s, _ := status.FromError(err)
span.SetStatus(codes.Error, s.Message()) span.SetStatus(codes.Error, s.Message())
span.SetAttributes(opentelemetry.StatusCodeAttr(s.Code())) span.SetAttributes(opentelemetry.StatusCodeAttr(s.Code()))
opentelemetry.MessageSent.Event(ctx, 1, s.Proto()) opentelemetry.MessageSent.Event(ctx, 1, s.Proto())
} else { return nil, err
span.SetAttributes(opentelemetry.StatusCodeAttr(grpc_codes.OK))
opentelemetry.MessageSent.Event(ctx, 1, resp)
} }
return resp, err span.SetAttributes(opentelemetry.StatusCodeAttr(gcodes.OK))
opentelemetry.MessageSent.Event(ctx, 1, resp)
return resp, nil
} }
} }
@ -89,7 +88,7 @@ func StreamOpenTracingInterceptor() grpc.StreamServerInterceptor {
span.SetStatus(codes.Error, s.Message()) span.SetStatus(codes.Error, s.Message())
span.SetAttributes(opentelemetry.StatusCodeAttr(s.Code())) span.SetAttributes(opentelemetry.StatusCodeAttr(s.Code()))
} else { } else {
span.SetAttributes(opentelemetry.StatusCodeAttr(grpc_codes.OK)) span.SetAttributes(opentelemetry.StatusCodeAttr(gcodes.OK))
} }
return err return err

Loading…
Cancel
Save