-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathtrace_receiver.go
497 lines (426 loc) · 14.9 KB
/
trace_receiver.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jaegerreceiver
import (
"context"
"fmt"
"html"
"io/ioutil"
"mime"
"net"
"net/http"
"sync"
apacheThrift "github.com/apache/thrift/lib/go/thrift"
"github.com/gorilla/mux"
"github.com/jaegertracing/jaeger/cmd/agent/app/configmanager"
jSamplingConfig "github.com/jaegertracing/jaeger/cmd/agent/app/configmanager/grpc"
"github.com/jaegertracing/jaeger/cmd/agent/app/httpserver"
"github.com/jaegertracing/jaeger/cmd/agent/app/processors"
"github.com/jaegertracing/jaeger/cmd/agent/app/servers"
"github.com/jaegertracing/jaeger/cmd/agent/app/servers/thriftudp"
"github.com/jaegertracing/jaeger/cmd/collector/app/handler"
collectorSampling "github.com/jaegertracing/jaeger/cmd/collector/app/sampling"
staticStrategyStore "github.com/jaegertracing/jaeger/plugin/sampling/strategystore/static"
"github.com/jaegertracing/jaeger/proto-gen/api_v2"
"github.com/jaegertracing/jaeger/thrift-gen/agent"
"github.com/jaegertracing/jaeger/thrift-gen/baggage"
"github.com/jaegertracing/jaeger/thrift-gen/jaeger"
"github.com/jaegertracing/jaeger/thrift-gen/sampling"
"github.com/jaegertracing/jaeger/thrift-gen/zipkincore"
"github.com/uber/jaeger-lib/metrics"
"go.uber.org/zap"
"google.golang.org/grpc"
"go.opentelemetry.io/collector/client"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/obsreport"
jaegertranslator "go.opentelemetry.io/collector/translator/trace/jaeger"
)
// configuration defines the behavior and the ports that
// the Jaeger receiver will use.
type configuration struct {
CollectorThriftPort int
CollectorHTTPPort int
CollectorHTTPSettings confighttp.HTTPServerSettings
CollectorGRPCPort int
CollectorGRPCServerSettings configgrpc.GRPCServerSettings
AgentCompactThriftPort int
AgentCompactThriftConfig ServerConfigUDP
AgentBinaryThriftPort int
AgentBinaryThriftConfig ServerConfigUDP
AgentHTTPPort int
RemoteSamplingClientSettings configgrpc.GRPCClientSettings
RemoteSamplingStrategyFile string
}
// Receiver type is used to receive spans that were originally intended to be sent to Jaeger.
// This receiver is basically a Jaeger collector.
type jReceiver struct {
nextConsumer consumer.Traces
instanceName string
config *configuration
grpc *grpc.Server
collectorServer *http.Server
agentSamplingManager *jSamplingConfig.SamplingManager
agentProcessors []processors.Processor
agentServer *http.Server
goroutines sync.WaitGroup
logger *zap.Logger
}
const (
agentTransportBinary = "udp_thrift_binary"
agentTransportCompact = "udp_thrift_compact"
collectorHTTPTransport = "collector_http"
grpcTransport = "grpc"
thriftFormat = "thrift"
protobufFormat = "protobuf"
)
var (
acceptedThriftFormats = map[string]struct{}{
"application/x-thrift": {},
"application/vnd.apache.thrift.binary": {},
}
)
// newJaegerReceiver creates a TracesReceiver that receives traffic as a Jaeger collector, and
// also as a Jaeger agent.
func newJaegerReceiver(
instanceName string,
config *configuration,
nextConsumer consumer.Traces,
params component.ReceiverCreateParams,
) *jReceiver {
return &jReceiver{
config: config,
nextConsumer: nextConsumer,
instanceName: instanceName,
logger: params.Logger,
}
}
func (jr *jReceiver) agentCompactThriftAddr() string {
var port int
if jr.config != nil {
port = jr.config.AgentCompactThriftPort
}
return fmt.Sprintf(":%d", port)
}
func (jr *jReceiver) agentCompactThriftEnabled() bool {
return jr.config != nil && jr.config.AgentCompactThriftPort > 0
}
func (jr *jReceiver) agentBinaryThriftAddr() string {
var port int
if jr.config != nil {
port = jr.config.AgentBinaryThriftPort
}
return fmt.Sprintf(":%d", port)
}
func (jr *jReceiver) agentBinaryThriftEnabled() bool {
return jr.config != nil && jr.config.AgentBinaryThriftPort > 0
}
func (jr *jReceiver) agentHTTPAddr() string {
var port int
if jr.config != nil {
port = jr.config.AgentHTTPPort
}
return fmt.Sprintf(":%d", port)
}
func (jr *jReceiver) agentHTTPEnabled() bool {
return jr.config != nil && jr.config.AgentHTTPPort > 0
}
func (jr *jReceiver) collectorGRPCAddr() string {
var port int
if jr.config != nil {
port = jr.config.CollectorGRPCPort
}
return fmt.Sprintf(":%d", port)
}
func (jr *jReceiver) collectorGRPCEnabled() bool {
return jr.config != nil && jr.config.CollectorGRPCPort > 0
}
func (jr *jReceiver) collectorHTTPEnabled() bool {
return jr.config != nil && jr.config.CollectorHTTPPort > 0
}
func (jr *jReceiver) Start(_ context.Context, host component.Host) error {
if err := jr.startAgent(host); err != nil {
return err
}
if err := jr.startCollector(host); err != nil {
return err
}
return nil
}
func (jr *jReceiver) Shutdown(ctx context.Context) error {
var errs []error
if jr.agentServer != nil {
if aerr := jr.agentServer.Shutdown(ctx); aerr != nil {
errs = append(errs, aerr)
}
}
for _, processor := range jr.agentProcessors {
processor.Stop()
}
if jr.collectorServer != nil {
if cerr := jr.collectorServer.Shutdown(ctx); cerr != nil {
errs = append(errs, cerr)
}
}
if jr.grpc != nil {
jr.grpc.GracefulStop()
}
jr.goroutines.Wait()
return consumererror.Combine(errs)
}
func consumeTraces(ctx context.Context, batch *jaeger.Batch, consumer consumer.Traces) (int, error) {
if batch == nil {
return 0, nil
}
td := jaegertranslator.ThriftBatchToInternalTraces(batch)
return len(batch.Spans), consumer.ConsumeTraces(ctx, td)
}
var _ agent.Agent = (*agentHandler)(nil)
var _ api_v2.CollectorServiceServer = (*jReceiver)(nil)
var _ configmanager.ClientConfigManager = (*jReceiver)(nil)
type agentHandler struct {
name string
transport string
nextConsumer consumer.Traces
}
// EmitZipkinBatch is unsupported agent's
func (h *agentHandler) EmitZipkinBatch(context.Context, []*zipkincore.Span) (err error) {
panic("unsupported receiver")
}
// EmitBatch implements thrift-gen/agent/Agent and it forwards
// Jaeger spans received by the Jaeger agent processor.
func (h *agentHandler) EmitBatch(ctx context.Context, batch *jaeger.Batch) error {
ctx = obsreport.ReceiverContext(ctx, h.name, h.transport)
ctx = obsreport.StartTraceDataReceiveOp(ctx, h.name, h.transport)
numSpans, err := consumeTraces(ctx, batch, h.nextConsumer)
obsreport.EndTraceDataReceiveOp(ctx, thriftFormat, numSpans, err)
return err
}
func (jr *jReceiver) GetSamplingStrategy(ctx context.Context, serviceName string) (*sampling.SamplingStrategyResponse, error) {
return jr.agentSamplingManager.GetSamplingStrategy(ctx, serviceName)
}
func (jr *jReceiver) GetBaggageRestrictions(ctx context.Context, serviceName string) ([]*baggage.BaggageRestriction, error) {
br, err := jr.agentSamplingManager.GetBaggageRestrictions(ctx, serviceName)
if err != nil {
// Baggage restrictions are not yet implemented - refer to - https://github.com/jaegertracing/jaeger/issues/373
// As of today, GetBaggageRestrictions() always returns an error.
// However, we `return nil, nil` here in order to serve a valid `200 OK` response.
return nil, nil
}
return br, nil
}
func (jr *jReceiver) PostSpans(ctx context.Context, r *api_v2.PostSpansRequest) (*api_v2.PostSpansResponse, error) {
if c, ok := client.FromGRPC(ctx); ok {
ctx = client.NewContext(ctx, c)
}
ctx = obsreport.ReceiverContext(ctx, jr.instanceName, grpcTransport)
ctx = obsreport.StartTraceDataReceiveOp(ctx, jr.instanceName, grpcTransport)
td := jaegertranslator.ProtoBatchToInternalTraces(r.GetBatch())
err := jr.nextConsumer.ConsumeTraces(ctx, td)
obsreport.EndTraceDataReceiveOp(ctx, protobufFormat, len(r.GetBatch().Spans), err)
if err != nil {
return nil, err
}
return &api_v2.PostSpansResponse{}, nil
}
func (jr *jReceiver) startAgent(host component.Host) error {
if !jr.agentBinaryThriftEnabled() && !jr.agentCompactThriftEnabled() && !jr.agentHTTPEnabled() {
return nil
}
if jr.agentBinaryThriftEnabled() {
h := &agentHandler{
name: jr.instanceName,
transport: agentTransportBinary,
nextConsumer: jr.nextConsumer,
}
processor, err := jr.buildProcessor(jr.agentBinaryThriftAddr(), jr.config.AgentBinaryThriftConfig, apacheThrift.NewTBinaryProtocolFactoryDefault(), h)
if err != nil {
return err
}
jr.agentProcessors = append(jr.agentProcessors, processor)
}
if jr.agentCompactThriftEnabled() {
h := &agentHandler{
name: jr.instanceName,
transport: agentTransportCompact,
nextConsumer: jr.nextConsumer,
}
processor, err := jr.buildProcessor(jr.agentCompactThriftAddr(), jr.config.AgentCompactThriftConfig, apacheThrift.NewTCompactProtocolFactory(), h)
if err != nil {
return err
}
jr.agentProcessors = append(jr.agentProcessors, processor)
}
jr.goroutines.Add(len(jr.agentProcessors))
for _, processor := range jr.agentProcessors {
go func(p processors.Processor) {
defer jr.goroutines.Done()
p.Serve()
}(processor)
}
// Start upstream grpc client before serving sampling endpoints over HTTP
if jr.config.RemoteSamplingClientSettings.Endpoint != "" {
grpcOpts, err := jr.config.RemoteSamplingClientSettings.ToDialOptions()
if err != nil {
jr.logger.Error("Error creating grpc dial options for remote sampling endpoint", zap.Error(err))
return err
}
conn, err := grpc.Dial(jr.config.RemoteSamplingClientSettings.Endpoint, grpcOpts...)
if err != nil {
jr.logger.Error("Error creating grpc connection to jaeger remote sampling endpoint", zap.String("endpoint", jr.config.RemoteSamplingClientSettings.Endpoint))
return err
}
jr.agentSamplingManager = jSamplingConfig.NewConfigManager(conn)
}
if jr.agentHTTPEnabled() {
jr.agentServer = httpserver.NewHTTPServer(jr.agentHTTPAddr(), jr, metrics.NullFactory)
jr.goroutines.Add(1)
go func() {
defer jr.goroutines.Done()
if err := jr.agentServer.ListenAndServe(); err != http.ErrServerClosed {
host.ReportFatalError(fmt.Errorf("jaeger agent server error: %w", err))
}
}()
}
return nil
}
func (jr *jReceiver) buildProcessor(address string, cfg ServerConfigUDP, factory apacheThrift.TProtocolFactory, a agent.Agent) (processors.Processor, error) {
handler := agent.NewAgentProcessor(a)
transport, err := thriftudp.NewTUDPServerTransport(address)
if err != nil {
return nil, err
}
if cfg.SocketBufferSize > 0 {
if err = transport.SetSocketBufferSize(cfg.SocketBufferSize); err != nil {
return nil, err
}
}
server, err := servers.NewTBufferedServer(transport, cfg.QueueSize, cfg.MaxPacketSize, metrics.NullFactory)
if err != nil {
return nil, err
}
processor, err := processors.NewThriftProcessor(server, cfg.Workers, metrics.NullFactory, factory, handler, jr.logger)
if err != nil {
return nil, err
}
return processor, nil
}
func (jr *jReceiver) decodeThriftHTTPBody(r *http.Request) (*jaeger.Batch, *httpError) {
bodyBytes, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
return nil, &httpError{
handler.UnableToReadBodyErrFormat,
http.StatusInternalServerError,
}
}
contentType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
return nil, &httpError{
fmt.Sprintf("Cannot parse content type: %v", err),
http.StatusBadRequest,
}
}
if _, ok := acceptedThriftFormats[contentType]; !ok {
return nil, &httpError{
fmt.Sprintf("Unsupported content type: %v", contentType),
http.StatusBadRequest,
}
}
tdes := apacheThrift.NewTDeserializer()
batch := &jaeger.Batch{}
if err = tdes.Read(batch, bodyBytes); err != nil {
return nil, &httpError{
fmt.Sprintf(handler.UnableToReadBodyErrFormat, err),
http.StatusBadRequest,
}
}
return batch, nil
}
// HandleThriftHTTPBatch implements Jaeger HTTP Thrift handler.
func (jr *jReceiver) HandleThriftHTTPBatch(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if c, ok := client.FromHTTP(r); ok {
ctx = client.NewContext(ctx, c)
}
ctx = obsreport.ReceiverContext(ctx, jr.instanceName, collectorHTTPTransport)
ctx = obsreport.StartTraceDataReceiveOp(ctx, jr.instanceName, collectorHTTPTransport)
batch, hErr := jr.decodeThriftHTTPBody(r)
if hErr != nil {
http.Error(w, html.EscapeString(hErr.msg), hErr.statusCode)
obsreport.EndTraceDataReceiveOp(ctx, thriftFormat, 0, hErr)
return
}
numSpans, err := consumeTraces(ctx, batch, jr.nextConsumer)
if err != nil {
http.Error(w, fmt.Sprintf("Cannot submit Jaeger batch: %v", err), http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusAccepted)
}
obsreport.EndTraceDataReceiveOp(ctx, thriftFormat, numSpans, err)
}
func (jr *jReceiver) startCollector(host component.Host) error {
if !jr.collectorGRPCEnabled() && !jr.collectorHTTPEnabled() {
return nil
}
if jr.collectorHTTPEnabled() {
cln, cerr := jr.config.CollectorHTTPSettings.ToListener()
if cerr != nil {
return fmt.Errorf("failed to bind to Collector address %q: %v",
jr.config.CollectorHTTPSettings.Endpoint, cerr)
}
nr := mux.NewRouter()
nr.HandleFunc("/api/traces", jr.HandleThriftHTTPBatch).Methods(http.MethodPost)
jr.collectorServer = &http.Server{Handler: nr}
jr.goroutines.Add(1)
go func() {
defer jr.goroutines.Done()
if err := jr.collectorServer.Serve(cln); err != http.ErrServerClosed {
host.ReportFatalError(err)
}
}()
}
if jr.collectorGRPCEnabled() {
opts, err := jr.config.CollectorGRPCServerSettings.ToServerOption(host.GetExtensions())
if err != nil {
return fmt.Errorf("failed to build the options for the Jaeger gRPC Collector: %v", err)
}
jr.grpc = grpc.NewServer(opts...)
gaddr := jr.collectorGRPCAddr()
gln, gerr := net.Listen("tcp", gaddr)
if gerr != nil {
return fmt.Errorf("failed to bind to gRPC address %q: %v", gaddr, gerr)
}
api_v2.RegisterCollectorServiceServer(jr.grpc, jr)
// init and register sampling strategy store
ss, gerr := staticStrategyStore.NewStrategyStore(staticStrategyStore.Options{
StrategiesFile: jr.config.RemoteSamplingStrategyFile,
}, jr.logger)
if gerr != nil {
return fmt.Errorf("failed to create collector strategy store: %v", gerr)
}
api_v2.RegisterSamplingManagerServer(jr.grpc, collectorSampling.NewGRPCHandler(ss))
jr.goroutines.Add(1)
go func() {
defer jr.goroutines.Done()
if err := jr.grpc.Serve(gln); err != nil && err != grpc.ErrServerStopped {
host.ReportFatalError(err)
}
}()
}
return nil
}