This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
/
grpc.go
49 lines (43 loc) · 1.47 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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package component
import (
"math"
"github.com/TheThingsNetwork/api/trace"
"github.com/TheThingsNetwork/go-utils/grpc/rpcerror"
"github.com/TheThingsNetwork/go-utils/grpc/rpclog"
"github.com/TheThingsNetwork/ttn/utils/errors"
"github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/mwitkow/go-grpc-middleware" // See https://github.com/grpc/grpc-go/issues/711"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
func (c *Component) ServerOptions() []grpc.ServerOption {
opts := []grpc.ServerOption{
grpc.MaxConcurrentStreams(math.MaxUint16),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_prometheus.UnaryServerInterceptor,
rpcerror.UnaryServerInterceptor(errors.BuildGRPCError),
rpclog.UnaryServerInterceptor(c.Ctx),
)),
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
grpc_prometheus.StreamServerInterceptor,
rpcerror.StreamServerInterceptor(errors.BuildGRPCError),
rpclog.StreamServerInterceptor(c.Ctx),
)),
}
if c.tlsConfig != nil {
opts = append(opts, grpc.Creds(credentials.NewTLS(c.tlsConfig)))
}
return opts
}
func init() {
// Disable gRPC tracing
// SEE: https://github.com/grpc/grpc-go/issues/695
grpc.EnableTracing = false
// Initialize TTN tracing
OnInitialize(func(c *Component) error {
trace.SetComponent(c.Identity.ServiceName, c.Identity.ID)
return nil
})
}