-
Notifications
You must be signed in to change notification settings - Fork 559
/
grpc.go
64 lines (55 loc) · 1.97 KB
/
grpc.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
package telemetry
import (
"context"
grpc "google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"github.com/dagger/dagger/engine/slog"
)
func MeasuringUnaryClientInterceptor() grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
reqSize := proto.Size(req.(proto.Message))
err := invoker(ctx, method, req, reply, cc, opts...)
respSize := proto.Size(reply.(proto.Message))
slog.ExtraDebug("measuring gRPC client request",
"reqSize", reqSize,
"respSize", respSize)
return err
}
}
func MeasuringUnaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
reqSize := proto.Size(req.(proto.Message))
resp, err = handler(ctx, req)
respSize := proto.Size(resp.(proto.Message))
slog.ExtraDebug("measuring gRPC server method",
"method", info.FullMethod,
"reqSize", reqSize,
"respSize", respSize)
return resp, err
}
}
func MeasuringStreamClientInterceptor() grpc.StreamClientInterceptor {
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
clientStream, err := streamer(ctx, desc, cc, method, opts...)
if err != nil {
return nil, err
}
return &measuringClientStream{ClientStream: clientStream}, nil
}
}
type measuringClientStream struct {
grpc.ClientStream
}
func (s *measuringClientStream) SendMsg(m any) error {
msgSize := proto.Size(m.(proto.Message))
slog.ExtraDebug("measuring client stream SendMsg", "msgSize", msgSize)
return s.ClientStream.SendMsg(m)
}
func (s *measuringClientStream) RecvMsg(m any) error {
err := s.ClientStream.RecvMsg(m)
if err == nil {
msgSize := proto.Size(m.(proto.Message))
slog.ExtraDebug("measuring client stream RecvMsg", "msgSize", msgSize)
}
return err
}