-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathzipkin.go
351 lines (304 loc) · 7.99 KB
/
zipkin.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
package zipkin
import (
"math/rand"
"sync"
"time"
"github.com/elodina/go-zipkin/gen-go/zipkincore"
"github.com/elodina/siesta-producer"
"github.com/elodina/siesta"
"github.com/yanzay/log"
"net"
"errors"
"encoding/binary"
"bytes"
"github.com/elodina/go-avro"
)
var localhost int32 = 127 * 256 * 256 * 256 + 1
type Collector interface {
Collect([]byte)
}
type Tracer struct {
collector Collector
ip int32
port int16
serviceName string
rate int
}
func NewTracer(serviceName string, rate int, producer *producer.KafkaProducer, ip string, port int16, topic string) *Tracer {
log.Infof("[Zipkin] Creating new tracer for service %s with rate 1:%d, topic %s, ip %s, port %d", serviceName, rate,
topic, ip, port)
collector := &KafkaCollector{producer: producer, topic: topic}
convertedIp, err := convertIp(ip); if err != nil {
log.Warningf("Given ip %s is not a valid ipv4 ip address, going with localhost ip")
convertedIp = &localhost
}
tracer := &Tracer{ip: *convertedIp, port: port, collector: collector, rate: rate, serviceName: serviceName}
return tracer
}
func convertIp(ip string) (*int32, error) {
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
return nil, errors.New("Unable to parse given ip")
}
var ipInInt int32
buf := bytes.NewReader([]byte{parsedIP[0], parsedIP[1], parsedIP[2], parsedIP[3]})
err := binary.Read(buf, binary.LittleEndian, &ipInInt); if err == nil {
return &ipInInt, nil
} else {
return nil, err
}
}
func DefaultTopic() string {
return "zipkin"
}
func DefaultPort() int16 {
return 0
}
func DefaultProducer(brokerList []string) (*producer.KafkaProducer, error) {
producerConfig := producer.NewProducerConfig()
producerConfig.BatchSize = 200
producerConfig.ClientID = "zipkin"
kafkaConnectorConfig := siesta.NewConnectorConfig()
kafkaConnectorConfig.BrokerList = brokerList
connector, err := siesta.NewDefaultConnector(kafkaConnectorConfig)
if err != nil {
return nil, err
}
return producer.NewKafkaProducer(producerConfig, producer.ByteSerializer, producer.ByteSerializer, connector), nil
}
func LocalNetworkIP() string {
ip, err := determineLocalIp()
if err == nil {
return ip
} else {
log.Warningf("Unable to determine local network IP address, going with localhost IP")
return "127.0.0.1"
}
}
func determineLocalIp() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range ifaces {
if iface.Flags & net.FlagUp == 0 {
continue // interface down
}
if iface.Flags & net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String(), nil
}
}
return "", errors.New("Not connected to network")
}
func (t *Tracer) NewSpan(name string) *Span {
log.Debugf("[Zipkin] Creating new span: %s", name)
sampled := rand.Intn(t.rate) == 0
if !sampled {
return &Span{sampled: false}
}
span := newSpan(name, newID(), newID(), nil, t.serviceName)
span.collector = t.collector
span.port = t.port
span.ip = t.ip
span.sampled = true
return span
}
type Span struct {
sync.Mutex
span *zipkincore.Span
collector Collector
ip int32
port int16
sampled bool
serviceName string
}
func newSpan(name string, traceID int64, spanId int64, parentID *int64, serviceName string) *Span {
zipkinSpan := &zipkincore.Span{
Name: name,
ID: spanId,
TraceID: traceID,
ParentID: parentID,
Annotations: make([]*zipkincore.Annotation, 0),
BinaryAnnotations: make([]*zipkincore.BinaryAnnotation, 0),
}
return &Span{span: zipkinSpan, serviceName: serviceName}
}
func (s *Span) Sampled() bool {
return s.sampled
}
func (s *Span) TraceID() int64 {
return s.span.TraceID
}
func (s *Span) ParentID() *int64 {
return s.span.ParentID
}
func (s *Span) ID() int64 {
return s.span.ID
}
func (s *Span) ServerReceive() {
log.Debugf("[Zipkin] ServerReceive")
s.Annotate(zipkincore.SERVER_RECV)
}
func (s *Span) ServerSend() {
log.Debugf("[Zipkin] ServerSend")
s.Annotate(zipkincore.SERVER_SEND)
}
func (s *Span) ServerSendAndCollect() {
s.ServerSend()
s.Collect()
}
func (s *Span) ServerReceiveAndCollect() {
s.ServerReceive()
s.Collect()
}
func (s *Span) ClientSend() {
log.Debugf("[Zipkin] ClientSend")
s.Annotate(zipkincore.CLIENT_SEND)
}
func (s *Span) ClientReceive() {
log.Debugf("[Zipkin] ClientReceive")
s.Annotate(zipkincore.CLIENT_RECV)
}
func (s *Span) ClientReceiveAndCollect() {
s.ClientReceive()
s.Collect()
}
func (s *Span) ClientSendAndCollect() {
s.ClientSend()
s.Collect()
}
func (s *Span) NewChild(name string) *Span {
log.Debugf("[Zipkin] Creating new child span: %s", name)
if !s.sampled {
return &Span{}
}
child := newSpan(name, s.span.TraceID, newID(), &s.span.ID, s.serviceName)
child.collector = s.collector
child.ip = s.ip
child.port = s.port
child.sampled = true
return child
}
func (t *Tracer) NewSpanFromAvro(name string, traceInfo interface{}) *Span {
if traceInfo == nil {
return t.NewSpanFromRequest(name, nil, nil, nil, nil);
}
traceInfoDef := traceInfo.(*avro.GenericRecord)
var traceId *int64 = nil
if traceIdAvro := traceInfoDef.Get("traceId"); traceIdAvro != nil {
traceIdDef := traceIdAvro.(int64)
traceId = &traceIdDef
}
var spanId *int64 = nil
if spanIdAvro := traceInfoDef.Get("spanId"); spanIdAvro != nil {
spanIdDef := spanIdAvro.(int64)
spanId = &spanIdDef
}
var sampled *bool = nil
if sampledAvro := traceInfoDef.Get("sampled"); sampledAvro != nil {
sampledDef := sampledAvro.(bool)
sampled = &sampledDef
}
var parentId *int64 = nil
if parentIdAvro := traceInfoDef.Get("parentSpanId"); parentIdAvro != nil {
parentIdDef := parentIdAvro.(int64)
parentId = &parentIdDef
}
return t.NewSpanFromRequest(name, traceId, spanId, parentId, sampled)
}
func (t *Tracer) NewSpanFromRequest(name string, traceId *int64, spanId *int64, parentId *int64, sampled *bool) *Span {
nonSampled := &Span{sampled: false}
if sampled == nil {
log.Debugf("[Zipkin] Empty trace info provided. Ignoring")
return nonSampled
}
if !*sampled {
log.Debugf("[Zipkin] The input trace info not sampled. Ignoring")
return nonSampled
}
if spanId == nil || traceId == nil {
log.Debugf("[Zipkin] The input trace info incomplete. Ignoring")
return nonSampled
}
log.Debugf("[Zipkin] Creating new span %s from request: traceID %s, spanID %s, parentID %s, sampled %s", name,
traceId, spanId, parentId, sampled)
span := newSpan(name, *traceId, *spanId, nil, t.serviceName)
span.collector = t.collector
span.port = t.port
span.ip = t.ip
span.sampled = true
return span
}
func (s *Span) GetAvroTraceInfo() *avro.GenericRecord {
if s.sampled {
traceInfo := avro.NewGenericRecord(NewTraceInfo().Schema())
traceInfo.Set("traceId", s.TraceID())
traceInfo.Set("spanId", s.ID())
if parentId := s.ParentID(); parentId != nil {
traceInfo.Set("parentSpanId", *parentId)
}
traceInfo.Set("sampled", true)
return traceInfo
} else {
return nil
}
}
func (s *Span) Collect() error {
log.Debugf("[Zipkin] Sending spans: %b", s.sampled)
if !s.sampled {
return nil
}
log.Debugf("[Zipkin] Serializing span: %v", s.span)
bytes, err := SerializeSpan(s.span)
if err != nil {
return err
}
s.collector.Collect(bytes)
return nil
}
func (s *Span) Annotate(value string) {
if !s.sampled {
return
}
now := nowMicrosecond()
annotation := &zipkincore.Annotation{
Value: value,
Timestamp: *now,
Host: &zipkincore.Endpoint{
ServiceName: s.serviceName,
Ipv4: s.ip,
Port: s.port,
},
}
s.Lock()
s.span.Annotations = append(s.span.Annotations, annotation)
s.Unlock()
}
func nowMicrosecond() *int64 {
now := time.Now().UnixNano() / 1000
return &now
}
func newID() int64 {
return rand.Int63()
}