forked from jaegertracing/jaeger
/
reporter.go
108 lines (95 loc) · 3.15 KB
/
reporter.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// 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 tchannel
import (
"time"
"github.com/uber/tchannel-go"
"github.com/uber/tchannel-go/thrift"
"go.uber.org/zap"
"github.com/jaegertracing/jaeger/pkg/discovery/peerlistmgr"
"github.com/jaegertracing/jaeger/thrift-gen/jaeger"
"github.com/jaegertracing/jaeger/thrift-gen/zipkincore"
)
// Reporter forwards received spans to central collector tier over TChannel.
type Reporter struct {
channel *tchannel.Channel
zClient zipkincore.TChanZipkinCollector
jClient jaeger.TChanCollector
reportTimeout time.Duration
peerListMgr *peerlistmgr.PeerListManager
logger *zap.Logger
serviceName string
}
// New creates new TChannel-based Reporter.
func New(
collectorServiceName string,
channel *tchannel.Channel,
reportTimeout time.Duration,
peerListMgr *peerlistmgr.PeerListManager,
zlogger *zap.Logger,
) *Reporter {
thriftClient := thrift.NewClient(channel, collectorServiceName, nil)
zClient := zipkincore.NewTChanZipkinCollectorClient(thriftClient)
jClient := jaeger.NewTChanCollectorClient(thriftClient)
return &Reporter{
channel: channel,
zClient: zClient,
jClient: jClient,
reportTimeout: reportTimeout,
peerListMgr: peerListMgr,
logger: zlogger,
serviceName: collectorServiceName,
}
}
// Channel returns the TChannel used by the reporter.
func (r *Reporter) Channel() *tchannel.Channel {
return r.channel
}
// EmitZipkinBatch implements EmitZipkinBatch() of Reporter
func (r *Reporter) EmitZipkinBatch(spans []*zipkincore.Span) error {
submissionFunc := func(ctx thrift.Context) error {
_, err := r.zClient.SubmitZipkinBatch(ctx, spans)
return err
}
return r.submitAndReport(
submissionFunc,
"Could not submit zipkin batch",
int64(len(spans)),
)
}
// EmitBatch implements EmitBatch() of Reporter
func (r *Reporter) EmitBatch(batch *jaeger.Batch) error {
submissionFunc := func(ctx thrift.Context) error {
_, err := r.jClient.SubmitBatches(ctx, []*jaeger.Batch{batch})
return err
}
return r.submitAndReport(
submissionFunc,
"Could not submit jaeger batch",
int64(len(batch.Spans)),
)
}
func (r *Reporter) submitAndReport(submissionFunc func(ctx thrift.Context) error, errMsg string, size int64) error {
ctx, cancel := tchannel.NewContextBuilder(r.reportTimeout).DisableTracing().Build()
defer cancel()
if err := submissionFunc(ctx); err != nil {
return err
}
r.logger.Debug("Span batch submitted by the agent", zap.Int64("span-count", size))
return nil
}
// CollectorServiceName returns collector service name.
func (r *Reporter) CollectorServiceName() string {
return r.serviceName
}