Codebase list golang-github-go-kit-kit / 2eb3ace
Zipkin and Kafka Collector updates Minor code formatting fixes Added missing comments on exported functions Added CollectionError to aid with handling errors returned by MultiCollector Bas van Beek 8 years ago
4 changed file(s) with 154 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
00 package zipkin
11
2 import (
3 "errors"
4 "strings"
5 )
2 import "strings"
63
74 // Collector represents a Zipkin trace collector, which is probably a set of
85 // remote endpoints.
1613
1714 // Collect implements Collector.
1815 func (NopCollector) Collect(*Span) error { return nil }
19 func (NopCollector) Close() error { return nil }
16
17 // Close implements Collector.
18 func (NopCollector) Close() error { return nil }
2019
2120 // MultiCollector implements Collector by sending spans to all collectors.
2221 type MultiCollector []Collector
2625 return c.aggregateErrors(func(coll Collector) error { return coll.Collect(s) })
2726 }
2827
28 // Close implements Collector.
2929 func (c MultiCollector) Close() error {
3030 return c.aggregateErrors(func(coll Collector) error { return coll.Close() })
3131 }
3232
3333 func (c MultiCollector) aggregateErrors(f func(c Collector) error) error {
34 var e *collectionError
35 for i, collector := range c {
36 if err := f(collector); err != nil {
37 if e == nil {
38 e = &collectionError{
39 errs: make([]error, len(c)),
40 }
41 }
42 e.errs[i] = err
43 }
44 }
45 return e
46 }
47
48 // CollectionError represents an array of errors returned by one or more
49 // failed Collector methods.
50 type CollectionError interface {
51 Error() string
52 GetErrors() []error
53 }
54
55 type collectionError struct {
56 errs []error
57 }
58
59 func (c *collectionError) Error() string {
3460 errs := []string{}
35 for _, collector := range c {
36 if err := f(collector); err != nil {
61 for _, err := range c.errs {
62 if err != nil {
3763 errs = append(errs, err.Error())
3864 }
3965 }
40 if len(errs) > 0 {
41 return errors.New(strings.Join(errs, "; "))
42 }
43 return nil
66 return strings.Join(errs, "; ")
4467 }
68
69 // GetErrors implements CollectionError
70 func (c *collectionError) GetErrors() []error {
71 return c.errs
72 }
66 "github.com/go-kit/kit/tracing/zipkin"
77 )
88
9 var s *zipkin.Span = zipkin.NewSpan("203.0.113.10:1234", "service1", "avg", 123, 456, 0)
9 var s = zipkin.NewSpan("203.0.113.10:1234", "service1", "avg", 123, 456, 0)
1010
1111 func TestNopCollector(t *testing.T) {
1212 c := zipkin.NopCollector{}
3131 }
3232 return nil
3333 }
34
3435 func (c *stubCollector) Close() error {
3536 c.closed = true
3637 if c.errid != 0 {
4647 &stubCollector{errid: 2},
4748 }
4849 err := cs.Collect(s)
49 wanted := "error 1; error 2"
50 if err == nil || err.Error() != wanted {
51 t.Errorf("errors not propagated. got %v, wanted %s", err, wanted)
50 if err == nil {
51 t.Fatal("wanted error, got none")
52 }
53 if want, have := "error 1; error 2", err.Error(); want != have {
54 t.Errorf("want %q, have %q", want, have)
55 }
56 collectionError := err.(zipkin.CollectionError).GetErrors()
57 if want, have := 3, len(collectionError); want != have {
58 t.Fatalf("want %d, have %d", want, have)
59 }
60 if want, have := cs[0].Collect(s).Error(), collectionError[0].Error(); want != have {
61 t.Errorf("want %q, have %q", want, have)
62 }
63 if want, have := cs[1].Collect(s), collectionError[1]; want != have {
64 t.Errorf("want %q, have %q", want, have)
65 }
66 if want, have := cs[2].Collect(s).Error(), collectionError[2].Error(); want != have {
67 t.Errorf("want %q, have %q", want, have)
5268 }
5369
5470 for _, c := range cs {
55 sc := c.(*stubCollector)
56 if !sc.collected {
71 if !c.(*stubCollector).collected {
5772 t.Error("collect not called")
5873 }
5974 }
6681 &stubCollector{errid: 2},
6782 }
6883 err := cs.Close()
69 wanted := "error 1; error 2"
70 if err == nil || err.Error() != wanted {
71 t.Errorf("errors not propagated. got %v, wanted %s", err, wanted)
84 if err == nil {
85 t.Fatal("wanted error, got none")
86 }
87 if want, have := "error 1; error 2", err.Error(); want != have {
88 t.Errorf("want %q, have %q", want, have)
7289 }
7390
7491 for _, c := range cs {
75 sc := c.(*stubCollector)
76 if !sc.closed {
92 if !c.(*stubCollector).closed {
7793 t.Error("close not called")
7894 }
7995 }
66 "github.com/go-kit/kit/log"
77 )
88
9 // KafkaTopic sets the Kafka topic our Collector will publish on. The
10 // default topic for zipkin-receiver-kafka is "zipkin", see:
11 // https://github.com/openzipkin/zipkin/tree/master/zipkin-receiver-kafka
912 var KafkaTopic = "zipkin"
1013
1114 // KafkaCollector implements Collector by forwarding spans to a Kafka
6871 return nil
6972 }
7073
74 // Close implements Collector.
7175 func (c *KafkaCollector) Close() error {
7276 return c.producer.Close()
7377 }
11
22 import (
33 "encoding/binary"
4 "fmt"
5 "math"
46 "net"
57 "strconv"
68 "time"
6264 return nil
6365 }
6466 endpoint := zipkincore.NewEndpoint()
65 binary.LittleEndian.PutUint32(addrs[0], (uint32)(endpoint.Ipv4))
67 endpoint.Ipv4 = (int32)(binary.BigEndian.Uint32(addrs[0].To4()))
6668 endpoint.Port = int16(portInt)
6769 endpoint.ServiceName = serviceName
6870 return endpoint
9395 s.AnnotateDuration(value, 0)
9496 }
9597
96 // AnnotateBinary annotates the span with a key and a byte value.
97 func (s *Span) AnnotateBinary(key string, value []byte) {
98 // AnnotateBinary annotates the span with a key and a value that will be []byte
99 // encoded.
100 func (s *Span) AnnotateBinary(key string, value interface{}) {
101 var a zipkincore.AnnotationType
102 var b []byte
103 // We are not using zipkincore.AnnotationType_I16 for types that could fit
104 // as reporting on it seems to be broken on the zipkin web interface
105 // (however, we can properly extract the number from zipkin storage
106 // directly). int64 has issues with negative numbers but seems ok for
107 // positive numbers needing more than 32 bit.
108 switch v := value.(type) {
109 case bool:
110 a = zipkincore.AnnotationType_BOOL
111 b = []byte("\x00")
112 if v {
113 b = []byte("\x01")
114 }
115 case []byte:
116 a = zipkincore.AnnotationType_BYTES
117 b = v
118 case byte:
119 a = zipkincore.AnnotationType_I32
120 b = make([]byte, 4)
121 binary.BigEndian.PutUint32(b, uint32(v))
122 case int8:
123 a = zipkincore.AnnotationType_I32
124 b = make([]byte, 4)
125 binary.BigEndian.PutUint32(b, uint32(v))
126 case int16:
127 a = zipkincore.AnnotationType_I32
128 b = make([]byte, 4)
129 binary.BigEndian.PutUint32(b, uint32(v))
130 case uint16:
131 a = zipkincore.AnnotationType_I32
132 b = make([]byte, 4)
133 binary.BigEndian.PutUint32(b, uint32(v))
134 case int32:
135 a = zipkincore.AnnotationType_I32
136 b = make([]byte, 4)
137 binary.BigEndian.PutUint32(b, uint32(v))
138 case uint32:
139 a = zipkincore.AnnotationType_I32
140 b = make([]byte, 4)
141 binary.BigEndian.PutUint32(b, uint32(v))
142 case int64:
143 a = zipkincore.AnnotationType_I64
144 b = make([]byte, 8)
145 binary.BigEndian.PutUint64(b, uint64(v))
146 case int:
147 a = zipkincore.AnnotationType_I32
148 b = make([]byte, 8)
149 binary.BigEndian.PutUint32(b, uint32(v))
150 case uint:
151 a = zipkincore.AnnotationType_I32
152 b = make([]byte, 8)
153 binary.BigEndian.PutUint32(b, uint32(v))
154 case uint64:
155 a = zipkincore.AnnotationType_I64
156 b = make([]byte, 8)
157 binary.BigEndian.PutUint64(b, uint64(v))
158 case float32:
159 a = zipkincore.AnnotationType_DOUBLE
160 b = make([]byte, 8)
161 bits := math.Float64bits(float64(v))
162 binary.BigEndian.PutUint64(b, bits)
163 case float64:
164 a = zipkincore.AnnotationType_DOUBLE
165 b = make([]byte, 8)
166 bits := math.Float64bits(v)
167 binary.BigEndian.PutUint64(b, bits)
168 case string:
169 a = zipkincore.AnnotationType_STRING
170 b = []byte(v)
171 default:
172 // we have no handler for type's value, but let's get a string
173 // representation of it.
174 a = zipkincore.AnnotationType_STRING
175 b = []byte(fmt.Sprintf("%+v", value))
176 }
98177 s.binaryAnnotations = append(s.binaryAnnotations, binaryAnnotation{
99178 key: key,
100 value: value,
101 annotationType: zipkincore.AnnotationType_BYTES,
179 value: b,
180 annotationType: a,
102181 host: s.host,
103182 })
104183 }