Ai
1 Star 0 Fork 0

micro-tools/wf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
grpctracing_interceptor_server.go 3.80 KB
一键复制 编辑 原始数据 按行查看 历史
545403892 提交于 2023-09-27 22:16 +08:00 . 升级go-ole
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/micro-tools/wf/extend/utils.
package grpctracing
import (
"context"
"gitee.com/micro-tools/wf/extend/utils/krpc/internal"
"gitee.com/micro-tools/wf/extend/utils/krpc/internal/grpcctx"
"gitee.com/micro-tools/wf/extend/utils/krpc/internal/grpcutils"
"gitee.com/micro-tools/wf/net/gtrace"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
grpcCodes "google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"sync"
)
var mu sync.Mutex
// UnaryServerInterceptor returns a grpc.UnaryServerInterceptor suitable
// for use in a grpc.NewServer call.
func UnaryServerInterceptor(
ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (interface{}, error) {
tracer := otel.GetTracerProvider().Tracer(
tracingInstrumentGrpcServer,
trace.WithInstrumentationVersion(internal.VERSION),
)
requestMetadata, _ := metadata.FromIncomingContext(ctx)
metadataCopy := requestMetadata.Copy()
entries, spanCtx := Extract(ctx, metadataCopy)
ctx = baggage.ContextWithValues(ctx, entries...)
ctx = trace.ContextWithRemoteSpanContext(ctx, spanCtx)
name, attr := spanInfo(info.FullMethod, peerFromCtx(ctx))
ctx, span := tracer.Start(
ctx,
name,
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(attr...),
)
defer func() {
span.End()
}()
span.SetAttributes(gtrace.CommonLabels()...)
span.AddEvent(tracingEventGrpcRequest, trace.WithAttributes(
attribute.Any(tracingEventGrpcRequestBaggage, gtrace.GetBaggageMap(ctx)),
attribute.Any(tracingEventGrpcMetadataIncoming, grpcctx.Ctx.IncomingMap(ctx)),
attribute.String(
tracingEventGrpcRequestMessage,
grpcutils.MarshalMessageToJsonStringForTracing(
req, "Request", tracingMaxContentLogSize,
),
),
))
res, err := handler(ctx, req)
span.AddEvent(tracingEventGrpcResponse, trace.WithAttributes(
attribute.String(
tracingEventGrpcResponseMessage,
grpcutils.MarshalMessageToJsonStringForTracing(
res, "Response", tracingMaxContentLogSize,
),
),
))
if err != nil {
s, _ := status.FromError(err)
span.SetStatus(codes.Error, s.Message())
span.SetAttributes(statusCodeAttr(s.Code()))
} else {
span.SetAttributes(statusCodeAttr(grpcCodes.OK))
}
return res, err
}
// StreamServerInterceptor returns a grpc.StreamServerInterceptor suitable
// for use in a grpc.NewServer call.
func StreamServerInterceptor(
srv interface{},
ss grpc.ServerStream,
info *grpc.StreamServerInfo,
handler grpc.StreamHandler) error {
tracer := otel.GetTracerProvider().Tracer(
tracingInstrumentGrpcServer,
trace.WithInstrumentationVersion(internal.VERSION),
)
ctx := ss.Context()
requestMetadata, _ := metadata.FromIncomingContext(ctx)
metadataCopy := requestMetadata.Copy()
entries, spanCtx := Extract(ctx, metadataCopy)
ctx = baggage.ContextWithValues(ctx, entries...)
ctx = trace.ContextWithRemoteSpanContext(ctx, spanCtx)
name, attr := spanInfo(info.FullMethod, peerFromCtx(ctx))
ctx, span := tracer.Start(
ctx,
name,
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(attr...),
)
defer span.End()
span.SetAttributes(gtrace.CommonLabels()...)
err := handler(srv, wrapServerStream(ctx, ss))
if err != nil {
s, _ := status.FromError(err)
span.SetStatus(codes.Error, s.Message())
span.SetAttributes(statusCodeAttr(s.Code()))
} else {
span.SetAttributes(statusCodeAttr(grpcCodes.OK))
}
return err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/micro-tools/wf.git
git@gitee.com:micro-tools/wf.git
micro-tools
wf
wf
v1.0.2

搜索帮助