This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
jaeger.go
370 lines (326 loc) · 9.72 KB
/
jaeger.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
// Copyright 2018, OpenCensus 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 jaeger contains an OpenCensus tracing exporter for Jaeger.
package jaeger // import "contrib.go.opencensus.io/exporter/jaeger"
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"github.com/uber/jaeger-client-go/thrift"
"github.com/uber/jaeger-client-go/thrift-gen/jaeger"
"go.opencensus.io/trace"
"google.golang.org/api/support/bundler"
)
const defaultServiceName = "OpenCensus"
// Options are the options to be used when initializing a Jaeger exporter.
type Options struct {
// Endpoint is the Jaeger HTTP Thrift endpoint.
// For example, http://localhost:14268.
//
// Deprecated: Use CollectorEndpoint instead.
Endpoint string
// CollectorEndpoint is the full url to the Jaeger HTTP Thrift collector.
// For example, http://localhost:14268/api/traces
CollectorEndpoint string
// AgentEndpoint instructs exporter to send spans to jaeger-agent at this address.
// For example, localhost:6831.
AgentEndpoint string
// OnError is the hook to be called when there is
// an error occurred when uploading the stats data.
// If no custom hook is set, errors are logged.
// Optional.
OnError func(err error)
// Username to be used if basic auth is required.
// Optional.
Username string
// Password to be used if basic auth is required.
// Optional.
Password string
// ServiceName is the Jaeger service name.
// Deprecated: Specify Process instead.
ServiceName string
// Process contains the information about the exporting process.
Process Process
//BufferMaxCount defines the total number of traces that can be buffered in memory
BufferMaxCount int
}
// NewExporter returns a trace.Exporter implementation that exports
// the collected spans to Jaeger.
func NewExporter(o Options) (*Exporter, error) {
if o.Endpoint == "" && o.CollectorEndpoint == "" && o.AgentEndpoint == "" {
return nil, errors.New("missing endpoint for Jaeger exporter")
}
var endpoint string
var client *agentClientUDP
var err error
if o.Endpoint != "" {
endpoint = o.Endpoint + "/api/traces?format=jaeger.thrift"
log.Printf("Endpoint has been deprecated. Please use CollectorEndpoint instead.")
} else if o.CollectorEndpoint != "" {
endpoint = o.CollectorEndpoint
} else {
client, err = newAgentClientUDP(o.AgentEndpoint, udpPacketMaxLength)
if err != nil {
return nil, err
}
}
onError := func(err error) {
if o.OnError != nil {
o.OnError(err)
return
}
log.Printf("Error when uploading spans to Jaeger: %v", err)
}
service := o.Process.ServiceName
if service == "" && o.ServiceName != "" {
// fallback to old service name if specified
service = o.ServiceName
} else if service == "" {
service = defaultServiceName
}
tags := make([]*jaeger.Tag, len(o.Process.Tags))
for i, tag := range o.Process.Tags {
tags[i] = attributeToTag(tag.key, tag.value)
}
e := &Exporter{
endpoint: endpoint,
agentEndpoint: o.AgentEndpoint,
client: client,
username: o.Username,
password: o.Password,
process: &jaeger.Process{
ServiceName: service,
Tags: tags,
},
}
bundler := bundler.NewBundler((*jaeger.Span)(nil), func(bundle interface{}) {
if err := e.upload(bundle.([]*jaeger.Span)); err != nil {
onError(err)
}
})
// Set BufferedByteLimit with the total number of spans that are permissible to be held in memory.
// This needs to be done since the size of messages is always set to 1. Failing to set this would allow
// 1G messages to be held in memory since that is the default value of BufferedByteLimit.
if o.BufferMaxCount != 0 {
bundler.BufferedByteLimit = o.BufferMaxCount
}
e.bundler = bundler
return e, nil
}
// Process contains the information exported to jaeger about the source
// of the trace data.
type Process struct {
// ServiceName is the Jaeger service name.
ServiceName string
// Tags are added to Jaeger Process exports
Tags []Tag
}
// Tag defines a key-value pair
// It is limited to the possible conversions to *jaeger.Tag by attributeToTag
type Tag struct {
key string
value interface{}
}
// BoolTag creates a new tag of type bool, exported as jaeger.TagType_BOOL
func BoolTag(key string, value bool) Tag {
return Tag{key, value}
}
// StringTag creates a new tag of type string, exported as jaeger.TagType_STRING
func StringTag(key string, value string) Tag {
return Tag{key, value}
}
// Int64Tag creates a new tag of type int64, exported as jaeger.TagType_LONG
func Int64Tag(key string, value int64) Tag {
return Tag{key, value}
}
// Exporter is an implementation of trace.Exporter that uploads spans to Jaeger.
type Exporter struct {
endpoint string
agentEndpoint string
process *jaeger.Process
bundler *bundler.Bundler
client *agentClientUDP
username, password string
}
var _ trace.Exporter = (*Exporter)(nil)
// ExportSpan exports a SpanData to Jaeger.
func (e *Exporter) ExportSpan(data *trace.SpanData) {
e.bundler.Add(spanDataToThrift(data), 1)
// TODO(jbd): Handle oversized bundlers.
}
// As per the OpenCensus Status code mapping in
// https://opencensus.io/tracing/span/status/
// the status is OK if the code is 0.
const opencensusStatusCodeOK = 0
func spanDataToThrift(data *trace.SpanData) *jaeger.Span {
tags := make([]*jaeger.Tag, 0, len(data.Attributes))
for k, v := range data.Attributes {
tag := attributeToTag(k, v)
if tag != nil {
tags = append(tags, tag)
}
}
tags = append(tags,
attributeToTag("status.code", data.Status.Code),
attributeToTag("status.message", data.Status.Message),
)
// Ensure that if Status.Code is not OK, that we set the "error" tag on the Jaeger span.
// See Issue https://github.com/census-instrumentation/opencensus-go/issues/1041
if data.Status.Code != opencensusStatusCodeOK {
tags = append(tags, attributeToTag("error", true))
}
var logs []*jaeger.Log
for _, a := range data.Annotations {
fields := make([]*jaeger.Tag, 0, len(a.Attributes))
for k, v := range a.Attributes {
tag := attributeToTag(k, v)
if tag != nil {
fields = append(fields, tag)
}
}
fields = append(fields, attributeToTag("message", a.Message))
logs = append(logs, &jaeger.Log{
Timestamp: a.Time.UnixNano() / 1000,
Fields: fields,
})
}
var refs []*jaeger.SpanRef
for _, link := range data.Links {
refs = append(refs, &jaeger.SpanRef{
TraceIdHigh: bytesToInt64(link.TraceID[0:8]),
TraceIdLow: bytesToInt64(link.TraceID[8:16]),
SpanId: bytesToInt64(link.SpanID[:]),
})
}
return &jaeger.Span{
TraceIdHigh: bytesToInt64(data.TraceID[0:8]),
TraceIdLow: bytesToInt64(data.TraceID[8:16]),
SpanId: bytesToInt64(data.SpanID[:]),
ParentSpanId: bytesToInt64(data.ParentSpanID[:]),
OperationName: name(data),
Flags: int32(data.TraceOptions),
StartTime: data.StartTime.UnixNano() / 1000,
Duration: data.EndTime.Sub(data.StartTime).Nanoseconds() / 1000,
Tags: tags,
Logs: logs,
References: refs,
}
}
func name(sd *trace.SpanData) string {
n := sd.Name
switch sd.SpanKind {
case trace.SpanKindClient:
n = "Sent." + n
case trace.SpanKindServer:
n = "Recv." + n
}
return n
}
func attributeToTag(key string, a interface{}) *jaeger.Tag {
var tag *jaeger.Tag
switch value := a.(type) {
case bool:
tag = &jaeger.Tag{
Key: key,
VBool: &value,
VType: jaeger.TagType_BOOL,
}
case string:
tag = &jaeger.Tag{
Key: key,
VStr: &value,
VType: jaeger.TagType_STRING,
}
case int64:
tag = &jaeger.Tag{
Key: key,
VLong: &value,
VType: jaeger.TagType_LONG,
}
case int32:
v := int64(value)
tag = &jaeger.Tag{
Key: key,
VLong: &v,
VType: jaeger.TagType_LONG,
}
case float64:
v := float64(value)
tag = &jaeger.Tag{
Key: key,
VDouble: &v,
VType: jaeger.TagType_DOUBLE,
}
}
return tag
}
// Flush waits for exported trace spans to be uploaded.
//
// This is useful if your program is ending and you do not want to lose recent spans.
func (e *Exporter) Flush() {
e.bundler.Flush()
}
func (e *Exporter) upload(spans []*jaeger.Span) error {
batch := &jaeger.Batch{
Spans: spans,
Process: e.process,
}
if e.endpoint != "" {
return e.uploadCollector(batch)
}
return e.uploadAgent(batch)
}
func (e *Exporter) uploadAgent(batch *jaeger.Batch) error {
return e.client.EmitBatch(batch)
}
func (e *Exporter) uploadCollector(batch *jaeger.Batch) error {
body, err := serialize(batch)
if err != nil {
return err
}
req, err := http.NewRequest("POST", e.endpoint, body)
if err != nil {
return err
}
if e.username != "" && e.password != "" {
req.SetBasicAuth(e.username, e.password)
}
req.Header.Set("Content-Type", "application/x-thrift")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("failed to upload traces; HTTP status code: %d", resp.StatusCode)
}
return nil
}
func serialize(obj thrift.TStruct) (*bytes.Buffer, error) {
buf := thrift.NewTMemoryBuffer()
if err := obj.Write(thrift.NewTBinaryProtocolTransport(buf)); err != nil {
return nil, err
}
return buf.Buffer, nil
}
func bytesToInt64(buf []byte) int64 {
u := binary.BigEndian.Uint64(buf)
return int64(u)
}