forked from lightstep/lightstep-tracer-go
/
thrift_collector_client.go
193 lines (166 loc) · 5.61 KB
/
thrift_collector_client.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
package lightstep
import (
"fmt"
"strconv"
"strings"
"time"
"golang.org/x/net/context"
"github.com/lightstep/lightstep-tracer-go/lightstep_thrift"
"github.com/lightstep/lightstep-tracer-go/thrift_0_9_2/lib/go/thrift"
)
// thriftCollectorClient specifies how to send reports back to a LightStep
// collector via thrift
type thriftCollectorClient struct {
// auth and runtime information
auth *lightstep_thrift.Auth
attributes map[string]string
startTime time.Time
reporterID uint64
lastReportAttempt time.Time
maxReportingPeriod time.Duration
reportInFlight bool
// Remote service that will receive reports
thriftClient lightstep_thrift.ReportingService
// apiURL is the base URL of the LightStep web API, used for
// explicit trace collection requests.
collectorURL string
// AccessToken is the access token used for explicit trace
// collection requests.
AccessToken string
verbose bool
// flags replacement
maxLogMessageLen int
maxLogKeyLen int
reportTimeout time.Duration
thriftConnectorFactory ConnectorFactory
}
func newThriftCollectorClient(opts Options, guid uint64, attributes map[string]string) *thriftCollectorClient {
reportTimeout := 60 * time.Second
if opts.ReportTimeout > 0 {
reportTimeout = opts.ReportTimeout
}
now := time.Now()
rec := &thriftCollectorClient{
auth: &lightstep_thrift.Auth{
AccessToken: thrift.StringPtr(opts.AccessToken),
},
attributes: attributes,
startTime: now,
maxReportingPeriod: opts.ReportingPeriod,
verbose: opts.Verbose,
collectorURL: opts.Collector.URL(),
AccessToken: opts.AccessToken,
maxLogMessageLen: opts.MaxLogValueLen,
maxLogKeyLen: opts.MaxLogKeyLen,
reportTimeout: reportTimeout,
thriftConnectorFactory: opts.ConnFactory,
reporterID: guid,
}
return rec
}
func (client *thriftCollectorClient) ConnectClient() (Connection, error) {
var conn Connection
if client.thriftConnectorFactory != nil {
unchecked_client, transport, err := client.thriftConnectorFactory()
if err != nil {
return nil, err
}
thriftClient, ok := unchecked_client.(lightstep_thrift.ReportingService)
if !ok {
return nil, fmt.Errorf("Thrift connector factory did not provide valid client!")
}
conn = transport
client.thriftClient = thriftClient
} else {
transport, err := thrift.NewTHttpPostClient(client.collectorURL, client.reportTimeout)
if err != nil {
maybeLogError(err, client.verbose)
return nil, err
}
conn = transport
client.thriftClient = lightstep_thrift.NewReportingServiceClientFactory(
transport, thrift.NewTBinaryProtocolFactoryDefault())
}
return conn, nil
}
func (*thriftCollectorClient) ShouldReconnect() bool {
return false
}
func (client *thriftCollectorClient) Report(_ context.Context, buffer *reportBuffer) (collectorResponse, error) {
rawSpans := buffer.rawSpans
// Convert them to thrift.
recs := make([]*lightstep_thrift.SpanRecord, len(rawSpans))
// TODO: could pool lightstep_thrift.SpanRecords
for i, raw := range rawSpans {
var joinIds []*lightstep_thrift.TraceJoinId
var attributes []*lightstep_thrift.KeyValue
for key, value := range raw.Tags {
if strings.HasPrefix(key, "join:") {
joinIds = append(joinIds, &lightstep_thrift.TraceJoinId{key, fmt.Sprint(value)})
} else {
attributes = append(attributes, &lightstep_thrift.KeyValue{key, fmt.Sprint(value)})
}
}
logs := make([]*lightstep_thrift.LogRecord, len(raw.Logs))
for j, log := range raw.Logs {
thriftLogRecord := &lightstep_thrift.LogRecord{
TimestampMicros: thrift.Int64Ptr(log.Timestamp.UnixNano() / 1000),
}
// In the deprecated thrift case, we can reuse a single "field"
// encoder across all of the N log fields.
lfe := thriftLogFieldEncoder{thriftLogRecord, client}
for _, f := range log.Fields {
f.Marshal(&lfe)
}
logs[j] = thriftLogRecord
}
// TODO implement baggage
if raw.ParentSpanID != 0 {
attributes = append(attributes, &lightstep_thrift.KeyValue{ParentSpanGUIDKey,
strconv.FormatUint(raw.ParentSpanID, 16)})
}
recs[i] = &lightstep_thrift.SpanRecord{
SpanGuid: thrift.StringPtr(strconv.FormatUint(raw.Context.SpanID, 16)),
TraceGuid: thrift.StringPtr(strconv.FormatUint(raw.Context.TraceID, 16)),
SpanName: thrift.StringPtr(raw.Operation),
JoinIds: joinIds,
OldestMicros: thrift.Int64Ptr(raw.Start.UnixNano() / 1000),
YoungestMicros: thrift.Int64Ptr(raw.Start.Add(raw.Duration).UnixNano() / 1000),
Attributes: attributes,
LogRecords: logs,
}
}
metrics := lightstep_thrift.Metrics{
Counts: []*lightstep_thrift.MetricsSample{
&lightstep_thrift.MetricsSample{
Name: "spans.dropped",
Int64Value: &buffer.droppedSpanCount,
},
},
}
req := &lightstep_thrift.ReportRequest{
OldestMicros: thrift.Int64Ptr(buffer.reportEnd.UnixNano() / 1000),
YoungestMicros: thrift.Int64Ptr(buffer.reportStart.UnixNano() / 1000),
Runtime: client.thriftRuntime(),
SpanRecords: recs,
InternalMetrics: &metrics,
}
resp, err := client.thriftClient.Report(client.auth, req)
if err != nil {
return nil, err
}
return resp, err
}
// caller must hold r.lock
func (r *thriftCollectorClient) thriftRuntime() *lightstep_thrift.Runtime {
guid := strconv.FormatUint(r.reporterID, 10)
runtimeAttrs := []*lightstep_thrift.KeyValue{}
for k, v := range r.attributes {
runtimeAttrs = append(runtimeAttrs, &lightstep_thrift.KeyValue{k, v})
}
return &lightstep_thrift.Runtime{
StartMicros: thrift.Int64Ptr(r.startTime.UnixNano() / 1000),
Attrs: runtimeAttrs,
Guid: &guid,
}
}