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
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ type Base struct {
|
||||
func (s *Base) Init(ready *sync.WaitGroup) {
|
||||
s.wg = &sync.WaitGroup{}
|
||||
s.wg.Add(1)
|
||||
s.SID = utils.SnowflakeInstance().Generate().Int64()
|
||||
s.SID = utils.SnowflakeInt64()
|
||||
go func() {
|
||||
defer s.wg.Done()
|
||||
defer s.OnClose()
|
||||
@@ -55,6 +55,7 @@ func (s *Base) Init(ready *sync.WaitGroup) {
|
||||
options := []grpc.ServerOption{
|
||||
grpc.ChainUnaryInterceptor(
|
||||
s.RecoveryInterceptor,
|
||||
s.CheckSID,
|
||||
s.LoggingInterceptor,
|
||||
),
|
||||
grpc.StatsHandler(otelgrpc.NewServerHandler()),
|
||||
@@ -79,6 +80,7 @@ func (s *Base) Init(ready *sync.WaitGroup) {
|
||||
}
|
||||
// 准备好了
|
||||
ready.Done()
|
||||
log.Infof("%v registered grpc sid: %v", s.Target, s.SID)
|
||||
if err = s.Serve.Serve(lis); err != nil {
|
||||
log.Errorf("%v Serve err: %v", s.Target, err)
|
||||
return
|
||||
@@ -91,7 +93,6 @@ func (s *Base) SetReady() {
|
||||
if s.healthcheck != nil {
|
||||
s.healthcheck.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
|
||||
}
|
||||
log.Infof("%v server ready, port: %v", s.Target, s.Cfg.Port)
|
||||
}
|
||||
|
||||
func (s *Base) Close() {
|
||||
|
||||
Reference in New Issue
Block a user