This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
123 lines (108 loc) · 3.38 KB
/
collector.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
package httptrace
import (
"encoding/binary"
"encoding/json"
"fmt"
"net"
"github.com/asecurityteam/logevent"
"github.com/openzipkin/zipkin-go-opentracing/thrift/gen-go/zipkincore"
)
// collector implements the openzipkin Collector interface.
type collector struct {
logevent.Logger
}
func (c *collector) Collect(s *zipkincore.Span) error {
var zipkinLog = structFromSpan(s)
c.Info(zipkinLog)
return nil
}
func (c *collector) Close() error {
return nil
}
func structFromSpan(s *zipkincore.Span) frame {
var result = frame{}
result.Zipkin.TraceID = fmt.Sprintf("%016x", s.GetTraceID())
result.Zipkin.SpanID = fmt.Sprintf("%016x", s.GetID())
if s.IsSetParentID() {
result.Zipkin.ParentID = fmt.Sprintf("%016x", s.GetParentID())
}
if s.IsSetDuration() {
result.Zipkin.Duration = s.GetDuration()
}
if s.IsSetTimestamp() {
result.Zipkin.Timestamp = s.GetTimestamp()
}
result.Zipkin.Name = s.GetName()
var annotations = s.GetAnnotations()
var binaryAnnotations = s.GetBinaryAnnotations()
result.Zipkin.Annotations = make([]annotation, len(annotations))
result.Zipkin.BinaryAnnotations = make([]binaryAnnotation, len(binaryAnnotations))
for offset, an := range annotations {
var host = an.GetHost()
result.Zipkin.Annotations[offset] = annotation{
Timestamp: an.GetTimestamp(),
Value: an.GetValue(),
}
if host != nil {
var ip = make(net.IP, 4)
binary.BigEndian.PutUint32(ip, uint32(host.GetIpv4()))
result.Zipkin.Annotations[offset].Endpoint = endpoint{
Ipv4: netIP(ip),
Port: int(host.GetPort()),
ServiceName: host.GetServiceName(),
}
}
}
for offset, binan := range binaryAnnotations {
var host = binan.GetHost()
result.Zipkin.BinaryAnnotations[offset] = binaryAnnotation{
Key: binan.GetKey(),
Value: string(binan.GetValue()),
}
if host != nil {
var ip = make(net.IP, 4)
binary.BigEndian.PutUint32(ip, uint32(host.GetIpv4()))
result.Zipkin.BinaryAnnotations[offset].Endpoint = endpoint{
Ipv4: netIP(ip),
Port: int(host.GetPort()),
ServiceName: host.GetServiceName(),
}
}
}
return result
}
// This is a set of types we can do a Marshal on for JSON.
type netIP net.IP
// MarshalJSON implements the Marshal interface for the netIP type.
func (ip netIP) MarshalJSON() ([]byte, error) {
return json.Marshal(net.IP(ip).String())
}
type endpoint struct {
ServiceName string `logevent:"serviceName"`
Port int `logevent:"port"`
Ipv4 netIP `logevent:"ipv4"`
}
type annotation struct {
Timestamp int64 `logevent:"timestamp"`
Value string `logevent:"value"`
Endpoint endpoint `logevent:"endpoint"`
}
type binaryAnnotation struct {
Key string `logevent:"key"`
Value string `logevent:"value"`
Endpoint endpoint `logevent:"endpoint"`
}
type jsonSpan struct {
TraceID string `logevent:"traceId"`
SpanID string `logevent:"id"`
ParentID string `logevent:"parentId"`
Name string `logevent:"name"`
Timestamp int64 `logevent:"timestamp"`
Duration int64 `logevent:"duration"`
Annotations []annotation `logevent:"annotations"`
BinaryAnnotations []binaryAnnotation `logevent:"binaryAnnotations"`
}
type frame struct {
Zipkin jsonSpan `logevent:"zipkin"`
Message string `logevent:"message,default=span-complete"`
}