-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
226 lines (197 loc) · 6.55 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package grpc
import (
"context"
"fmt"
"net"
"time"
"github.com/galexrt/fivenet/pkg/config"
"github.com/galexrt/fivenet/pkg/grpc/auth"
"github.com/galexrt/fivenet/pkg/grpc/auth/userinfo"
grpc_auth "github.com/galexrt/fivenet/pkg/grpc/interceptors/auth"
grpc_permission "github.com/galexrt/fivenet/pkg/grpc/interceptors/permission"
grpc_sanitizer "github.com/galexrt/fivenet/pkg/grpc/interceptors/sanitizer"
"github.com/galexrt/fivenet/pkg/grpc/interceptors/tracing"
"github.com/galexrt/fivenet/pkg/perms"
grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/validator"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"go.uber.org/fx"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
ErrInternalServer = status.Error(codes.Internal, "errors.general.internal_error.title;errors.general.internal_error.content")
)
// Setup metric for panic recoveries
var panicsTotal = promauto.With(prometheus.DefaultRegisterer).NewCounter(prometheus.CounterOpts{
Name: "grpc_req_panics_recovered_total",
Help: "Total number of gRPC requests recovered from internal panic.",
})
func wrapLogger(log *zap.Logger) *zap.Logger {
return log.Named("grpc_server")
}
var ServerModule = fx.Module("grpcserver",
fx.Provide(
NewServer,
),
fx.Decorate(wrapLogger),
)
type ServerParams struct {
fx.In
LC fx.Lifecycle
Logger *zap.Logger
Config *config.Config
TP *tracesdk.TracerProvider
TokenMgr *auth.TokenMgr
UserInfo userinfo.UserInfoRetriever
Perms perms.Permissions
GRPCAuth *auth.GRPCAuth
GRPCPerm *auth.GRPCPerm
Services []Service `group:"grpcservices"`
}
type ServerResult struct {
fx.Out
Server *grpc.Server
}
func NewServer(p ServerParams) (ServerResult, error) {
extraLogFields := func(ctx context.Context) logging.Fields {
if span := trace.SpanContextFromContext(ctx); span.IsSampled() {
return logging.Fields{"traceID", span.TraceID().String()}
}
return nil
}
// Setup metrics
srvMetrics := grpcprom.NewServerMetrics(
grpcprom.WithServerHandlingTimeHistogram(
grpcprom.WithHistogramBuckets([]float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120}),
),
)
prometheus.MustRegister(srvMetrics)
exemplarFromContext := func(ctx context.Context) prometheus.Labels {
if span := trace.SpanContextFromContext(ctx); span.IsSampled() {
return prometheus.Labels{"traceID": span.TraceID().String()}
}
return nil
}
// Setup GRPC tracing
otel.SetTracerProvider(p.TP)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
otelgrpcHandler := otelgrpc.NewServerHandler()
srv := grpc.NewServer(
grpc.StatsHandler(otelgrpcHandler),
grpc.ChainUnaryInterceptor(
srvMetrics.UnaryServerInterceptor(grpcprom.WithExemplarFromContext(exemplarFromContext)),
logging.UnaryServerInterceptor(InterceptorLogger(p.Logger),
logging.WithFieldsFromContext(extraLogFields),
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
),
tracing.UnaryServerInterceptor(),
grpc_auth.UnaryServerInterceptor(p.GRPCAuth.GRPCAuthFunc),
grpc_permission.UnaryServerInterceptor(p.GRPCPerm.GRPCPermissionUnaryFunc),
validator.UnaryServerInterceptor(),
grpc_sanitizer.UnaryServerInterceptor(),
recovery.UnaryServerInterceptor(
recovery.WithRecoveryHandler(grpcPanicRecoveryHandler(p.Logger)),
),
),
grpc.ChainStreamInterceptor(
srvMetrics.StreamServerInterceptor(grpcprom.WithExemplarFromContext(exemplarFromContext)),
logging.StreamServerInterceptor(InterceptorLogger(p.Logger),
logging.WithFieldsFromContext(extraLogFields),
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
),
tracing.StreamServerInterceptor(),
grpc_auth.StreamServerInterceptor(p.GRPCAuth.GRPCAuthFunc),
grpc_permission.StreamServerInterceptor(p.GRPCPerm.GRPCPermissionStreamFunc),
validator.StreamServerInterceptor(),
recovery.StreamServerInterceptor(
recovery.WithRecoveryHandler(grpcPanicRecoveryHandler(p.Logger)),
),
),
)
for _, service := range p.Services {
service.RegisterServer(srv)
}
p.LC.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
ln, err := net.Listen("tcp", p.Config.GRPC.Listen)
if err != nil {
return err
}
p.Logger.Info("grpc server listening", zap.String("address", p.Config.GRPC.Listen))
go func() {
if err := srv.Serve(ln); err != nil {
p.Logger.Error("failed to serve grpc server", zap.Error(err))
}
}()
return nil
},
OnStop: func(ctx context.Context) error {
go func() {
srv.GracefulStop()
}()
// Wait 3 seconds before "forceful stop
time.Sleep(3 * time.Second)
srv.Stop()
return nil
},
})
return ServerResult{
Server: srv,
}, nil
}
func grpcPanicRecoveryHandler(logger *zap.Logger) recovery.RecoveryHandlerFunc {
return func(pa any) (err error) {
panicsTotal.Inc()
if e, ok := pa.(error); ok {
logger.Error("recovered from panic", zap.Error(e))
} else {
logger.Error("recovered from panic", zap.Any("err", pa), zap.Stack("stacktrace"))
}
return ErrInternalServer
}
}
// InterceptorLogger adapts zap logger to interceptor logger.
// This code is simple enough to be copied and not imported.
func InterceptorLogger(l *zap.Logger) logging.Logger {
return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) {
f := make([]zap.Field, 0, len(fields)/2)
for i := 0; i < len(fields); i += 2 {
key := fields[i]
value := fields[i+1]
switch v := value.(type) {
case string:
f = append(f, zap.String(key.(string), v))
case int:
f = append(f, zap.Int(key.(string), v))
case bool:
f = append(f, zap.Bool(key.(string), v))
default:
f = append(f, zap.Any(key.(string), v))
}
}
logger := l.WithOptions(zap.AddCallerSkip(1)).With(f...)
switch lvl {
case logging.LevelDebug:
logger.Debug(msg)
case logging.LevelInfo:
logger.Info(msg)
case logging.LevelWarn:
logger.Warn(msg)
case logging.LevelError:
logger.Error(msg)
default:
panic(fmt.Sprintf("unknown level %v", lvl))
}
})
}