forked from lightstep/lightstep-tracer-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector_client_grpc.go
157 lines (132 loc) · 4.09 KB
/
collector_client_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
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
package lightstep
import (
"context"
"fmt"
"reflect"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
// N.B.(jmacd): Do not use google.golang.org/glog in this package.
"github.com/lightstep/lightstep-tracer-common/golang/gogo/collectorpb"
)
const (
spansDropped = "spans.dropped"
logEncoderErrors = "log_encoder.errors"
)
var (
intType = reflect.TypeOf(int64(0))
)
// grpcCollectorClient specifies how to send reports back to a LightStep
// collector via grpc.
type grpcCollectorClient struct {
// accessToken is the access token used for explicit trace collection requests.
accessToken string
maxReportingPeriod time.Duration // set by GrpcOptions.MaxReportingPeriod
reconnectPeriod time.Duration // set by GrpcOptions.ReconnectPeriod
reportingTimeout time.Duration // set by GrpcOptions.ReportTimeout
// Remote service that will receive reports.
address string
grpcClient collectorpb.CollectorServiceClient
connTimestamp time.Time
dialOptions []grpc.DialOption
// For testing purposes only
grpcConnectorFactory ConnectorFactory
}
func newGrpcCollectorClient(opts Options) (*grpcCollectorClient, error) {
rec := &grpcCollectorClient{
accessToken: opts.AccessToken,
maxReportingPeriod: opts.ReportingPeriod,
reconnectPeriod: opts.ReconnectPeriod,
reportingTimeout: opts.ReportTimeout,
dialOptions: opts.DialOptions,
grpcConnectorFactory: opts.ConnFactory,
}
if len(opts.Collector.Scheme) > 0 {
rec.address = opts.Collector.urlWithoutPath()
} else {
rec.address = opts.Collector.SocketAddress()
}
rec.dialOptions = append(rec.dialOptions, grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(opts.GRPCMaxCallSendMsgSizeBytes)))
if opts.Collector.Plaintext {
rec.dialOptions = append(rec.dialOptions, grpc.WithInsecure())
return rec, nil
}
if len(opts.Collector.CustomCACertFile) > 0 {
creds, err := credentials.NewClientTLSFromFile(opts.Collector.CustomCACertFile, "")
if err != nil {
return nil, err
}
rec.dialOptions = append(rec.dialOptions, grpc.WithTransportCredentials(creds))
} else {
rec.dialOptions = append(rec.dialOptions, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")))
}
return rec, nil
}
func (client *grpcCollectorClient) ConnectClient() (Connection, error) {
now := time.Now()
var conn Connection
if client.grpcConnectorFactory != nil {
uncheckedClient, transport, err := client.grpcConnectorFactory()
if err != nil {
return nil, err
}
grpcClient, ok := uncheckedClient.(collectorpb.CollectorServiceClient)
if !ok {
return nil, fmt.Errorf("gRPC connector factory did not provide valid client")
}
conn = transport
client.grpcClient = grpcClient
} else {
transport, err := grpc.Dial(client.address, client.dialOptions...)
if err != nil {
return nil, err
}
conn = transport
client.grpcClient = collectorpb.NewCollectorServiceClient(transport)
}
client.connTimestamp = now
return conn, nil
}
func (client *grpcCollectorClient) ShouldReconnect() bool {
return time.Since(client.connTimestamp) > client.reconnectPeriod
}
func (client *grpcCollectorClient) Report(ctx context.Context, req reportRequest) (collectorResponse, error) {
if req.protoRequest == nil {
return nil, fmt.Errorf("protoRequest cannot be null")
}
ctx = metadata.NewOutgoingContext(
ctx,
metadata.Pairs(
accessTokenHeader,
client.accessToken,
),
)
resp, err := client.grpcClient.Report(ctx, req.protoRequest)
if err != nil {
return nil, err
}
return protoResponse{ReportResponse: resp}, nil
}
func (client *grpcCollectorClient) Translate(protoRequest *collectorpb.ReportRequest) (reportRequest, error) {
return reportRequest{protoRequest: protoRequest}, nil
}
type protoResponse struct {
*collectorpb.ReportResponse
}
func (res protoResponse) Disable() bool {
for _, command := range res.GetCommands() {
if command.Disable {
return true
}
}
return false
}
func (res protoResponse) DevMode() bool {
for _, command := range res.GetCommands() {
if command.DevMode {
return true
}
}
return false
}