feat 初始化
This commit is contained in:
@@ -2,8 +2,11 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.hlsq.asia/alex/proto/proto/rs/rscommon"
|
||||
"git.hlsq.asia/alex/service-common/log"
|
||||
"git.hlsq.asia/alex/service-common/net/http/httpresp"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
otelcodes "go.opentelemetry.io/otel/codes"
|
||||
@@ -12,6 +15,7 @@ import (
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -25,6 +29,19 @@ func (s *Base) RecoveryInterceptor(ctx context.Context, req interface{}, info *g
|
||||
return handler(ctx, req)
|
||||
}
|
||||
|
||||
func (s *Base) CheckSID(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||
if md, ok := metadata.FromIncomingContext(ctx); ok {
|
||||
sidArr := md.Get("X-Sid")
|
||||
if len(sidArr) > 0 {
|
||||
if strconv.Itoa(int(s.SID)) != sidArr[0] {
|
||||
err = status.Error(codes.Internal, "SID mismatch")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
return handler(ctx, req)
|
||||
}
|
||||
|
||||
func (s *Base) LoggingInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||
usn := ""
|
||||
if md, ok := metadata.FromIncomingContext(ctx); ok {
|
||||
@@ -37,14 +54,17 @@ func (s *Base) LoggingInterceptor(ctx context.Context, req interface{}, info *gr
|
||||
_, span := otel.Tracer("grpc.interceptor.logging").Start(ctx, info.FullMethod)
|
||||
defer span.End()
|
||||
|
||||
reqString := fmt.Sprintf("[usn:%v] method: %s, Request: %v", usn, info.FullMethod, req)
|
||||
reqStr, _ := json.Marshal(req)
|
||||
reqString := fmt.Sprintf("[usn:%v] ↓↓↓ method: %s, Request: %v", usn, info.FullMethod, string(reqStr))
|
||||
span.SetAttributes(attribute.String("rpc.req", reqString))
|
||||
span.SetAttributes(attribute.String("usn", usn))
|
||||
log.Infof(reqString)
|
||||
start := time.Now()
|
||||
|
||||
resp, err = handler(ctx, req)
|
||||
|
||||
respString := fmt.Sprintf("[usn:%v] method: %s, Duration: %v, Response: %v, Error: %v", usn, info.FullMethod, time.Since(start), resp, err)
|
||||
respStr, _ := json.Marshal(resp)
|
||||
respString := fmt.Sprintf("[usn:%v] ↑↑↑ method: %s, Duration: %v, Response: %v, Error: %v", usn, info.FullMethod, time.Since(start), string(respStr), err)
|
||||
span.SetAttributes(attribute.String("rpc.resp", respString))
|
||||
log.Infof(respString)
|
||||
|
||||
@@ -53,8 +73,31 @@ func (s *Base) LoggingInterceptor(ctx context.Context, req interface{}, info *gr
|
||||
span.AddEvent("Stack Trace", trace.WithAttributes(
|
||||
attribute.String("stack.trace", fmt.Sprintf("%v", stack.StackTrace())),
|
||||
))
|
||||
if original, ok := err.(interface{ Original() error }); ok {
|
||||
if wrap, ok := original.Original().(interface{ Wrap() error }); ok {
|
||||
err = wrap.Wrap()
|
||||
} else {
|
||||
err = checkGRPCError(original.Original())
|
||||
}
|
||||
}
|
||||
} else if wrap, ok := err.(interface{ Wrap() error }); ok {
|
||||
err = wrap.Wrap()
|
||||
} else {
|
||||
err = checkGRPCError(err)
|
||||
}
|
||||
span.SetStatus(otelcodes.Error, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func checkGRPCError(err error) error {
|
||||
if original, ok := err.(interface{ GRPCStatus() *status.Status }); ok {
|
||||
details := original.GRPCStatus().Details()
|
||||
if len(details) > 0 {
|
||||
if ei, ok := details[0].(*rscommon.ErrorInfo); ok {
|
||||
return httpresp.NewCode(int(ei.Code), ei.Msg).Wrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user