feat 链路追踪

This commit is contained in:
2026-01-16 22:01:35 +08:00
parent 7cb057fc91
commit e59d106700
11 changed files with 394 additions and 108 deletions

View File

@@ -1,15 +1,13 @@
package service
import (
"context"
"fmt"
"git.hlsq.asia/mmorpg/service-common/discover"
"git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/utils"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/status"
"net"
"sync"
"time"
@@ -21,12 +19,13 @@ type IService interface {
}
type Base struct {
Target string
SID string
Serve *grpc.Server
EtcdTTL int64
OnInit func(serve *grpc.Server)
OnClose func()
Target string
ServiceName string
SID string
Serve *grpc.Server
EtcdTTL int64
OnInit func(serve *grpc.Server)
OnClose func()
wg *sync.WaitGroup
}
@@ -48,17 +47,11 @@ func (s *Base) Init(addr string, port int32) {
}
s.Serve = grpc.NewServer(
grpc.UnaryInterceptor(
func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
defer func() {
if r := recover(); r != nil {
log.Errorf("service Panic: %v", r)
err = status.Error(codes.Internal, fmt.Sprintf("%v", r))
}
}()
return handler(ctx, req)
},
grpc.ChainUnaryInterceptor(
s.RecoveryInterceptor,
s.LoggingInterceptor,
),
grpc.StatsHandler(otelgrpc.NewServerHandler()),
grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
MinTime: 20 * time.Second,
PermitWithoutStream: true,