Core

/app/lib/telemetry/grpc.go (1.3 KB)

 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
// $PF_HAS_MODULE(grpc)$
package telemetry

import (
"time"

"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
_ "google.golang.org/grpc/encoding/gzip" // zip responses
"google.golang.org/grpc/keepalive"
)

func NewGRPCServer(maxMsgSize int, opts ...grpc.ServerOption) *grpc.Server {
allOpts := append([]grpc.ServerOption{
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor()),
grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor()),
grpc.MaxRecvMsgSize(maxMsgSize),
grpc.MaxSendMsgSize(maxMsgSize),
}, opts...)
return grpc.NewServer(allOpts...)
}

func NewGRPCConnection(address string, maxMsgSize int, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
si := grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor())
ui := grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor())
ka := grpc.WithKeepaliveParams(keepalive.ClientParameters{Time: 1 * time.Minute, Timeout: 10 * time.Minute, PermitWithoutStream: true})
insec := grpc.WithTransportCredentials(insecure.NewCredentials())
size := grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize))
allOpts := append([]grpc.DialOption{insec, ka, si, ui, size}, opts...)
return grpc.Dial(address, allOpts...)
}