This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter.go
138 lines (121 loc) · 4.18 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
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
// 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/jaeger-lib/metrics"
"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"
)
const (
jaegerBatches = "jaeger"
zipkinBatches = "zipkin"
)
type batchMetrics struct {
// Number of successful batch submissions to collector
BatchesSubmitted metrics.Counter `metric:"batches.submitted"`
// Number of failed batch submissions to collector
BatchesFailures metrics.Counter `metric:"batches.failures"`
// Number of spans in a batch submitted to collector
BatchSize metrics.Gauge `metric:"batch_size"`
// Number of successful span submissions to collector
SpansSubmitted metrics.Counter `metric:"spans.submitted"`
// Number of failed span submissions to collector
SpansFailures metrics.Counter `metric:"spans.failures"`
}
// Reporter forwards received spans to central collector tier over TChannel.
type Reporter struct {
channel *tchannel.Channel
zClient zipkincore.TChanZipkinCollector
jClient jaeger.TChanCollector
peerListMgr *peerlistmgr.PeerListManager
batchesMetrics map[string]batchMetrics
logger *zap.Logger
}
// New creates new TChannel-based Reporter.
func New(
collectorServiceName string,
channel *tchannel.Channel,
peerListMgr *peerlistmgr.PeerListManager,
mFactory metrics.Factory,
zlogger *zap.Logger,
) *Reporter {
thriftClient := thrift.NewClient(channel, collectorServiceName, nil)
zClient := zipkincore.NewTChanZipkinCollectorClient(thriftClient)
jClient := jaeger.NewTChanCollectorClient(thriftClient)
batchesMetrics := map[string]batchMetrics{}
tcReporterNS := mFactory.Namespace("tc-reporter", nil)
for _, s := range []string{zipkinBatches, jaegerBatches} {
nsByType := tcReporterNS.Namespace(s, nil)
bm := batchMetrics{}
metrics.Init(&bm, nsByType, nil)
batchesMetrics[s] = bm
}
return &Reporter{
channel: channel,
zClient: zClient,
jClient: jClient,
peerListMgr: peerListMgr,
logger: zlogger,
batchesMetrics: batchesMetrics,
}
}
// 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)),
r.batchesMetrics[zipkinBatches],
)
}
// 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)),
r.batchesMetrics[jaegerBatches],
)
}
func (r *Reporter) submitAndReport(submissionFunc func(ctx thrift.Context) error, errMsg string, size int64, batchMetrics batchMetrics) error {
ctx, cancel := tchannel.NewContextBuilder(time.Second).DisableTracing().Build()
defer cancel()
if err := submissionFunc(ctx); err != nil {
batchMetrics.BatchesFailures.Inc(1)
batchMetrics.SpansFailures.Inc(size)
r.logger.Error(errMsg, zap.Error(err))
return err
}
batchMetrics.BatchSize.Update(size)
batchMetrics.BatchesSubmitted.Inc(1)
batchMetrics.SpansSubmitted.Inc(size)
return nil
}