Codebase list golang-github-go-kit-kit / 359fb6b
Added BinaryAnnotations to span Nicholas J 8 years ago
2 changed file(s) with 93 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
2525 spanID int64
2626 parentSpanID int64
2727
28 annotations []annotation
29 //binaryAnnotations []BinaryAnnotation // TODO
28 annotations []annotation
29 binaryAnnotations []binaryAnnotation
3030 }
3131
3232 // NewSpan returns a new Span, which can be annotated and collected by a
9393 s.AnnotateDuration(value, 0)
9494 }
9595
96 // AnnotateBinary annotates the span with a key and a byte value.
97 func (s *Span) AnnotateBinary(key string, value []byte) {
98 s.binaryAnnotations = append(s.binaryAnnotations, binaryAnnotation{
99 key: key,
100 value: value,
101 annotationType: zipkincore.AnnotationType_BYTES,
102 host: s.host,
103 })
104 }
105
106 // AnnotateString annotates the span with a key and a string value.
107 func (s *Span) AnnotateString(key, value string) {
108 s.binaryAnnotations = append(s.binaryAnnotations, binaryAnnotation{
109 key: key,
110 value: []byte(value),
111 annotationType: zipkincore.AnnotationType_STRING,
112 host: s.host,
113 })
114 }
115
96116 // AnnotateDuration annotates the span with the given value and duration.
97117 func (s *Span) AnnotateDuration(value string, duration time.Duration) {
98118 s.annotations = append(s.annotations, annotation{
108128 // TODO lots of garbage here. We can improve by preallocating e.g. the
109129 // Thrift stuff into an encoder struct, owned by the ScribeCollector.
110130 zs := zipkincore.Span{
111 TraceId: s.traceID,
112 Name: s.methodName,
113 Id: s.spanID,
114 BinaryAnnotations: []*zipkincore.BinaryAnnotation{}, // TODO
115 Debug: true, // TODO
131 TraceId: s.traceID,
132 Name: s.methodName,
133 Id: s.spanID,
134 // BinaryAnnotations: []*zipkincore.BinaryAnnotation{}, // TODO
135 Debug: true, // TODO
116136 }
137
117138 if s.parentSpanID != 0 {
118139 zs.ParentId = new(int64)
119140 (*zs.ParentId) = s.parentSpanID
120141 }
142
121143 zs.Annotations = make([]*zipkincore.Annotation, len(s.annotations))
122144 for i, a := range s.annotations {
123145 zs.Annotations[i] = &zipkincore.Annotation{
125147 Value: a.value,
126148 Host: a.host,
127149 }
150
128151 if a.duration > 0 {
129152 zs.Annotations[i].Duration = new(int32)
130153 *(zs.Annotations[i].Duration) = int32(a.duration / time.Microsecond)
131154 }
132155 }
156
157 zs.BinaryAnnotations = make([]*zipkincore.BinaryAnnotation, len(s.binaryAnnotations))
158 for i, a := range s.binaryAnnotations {
159 zs.BinaryAnnotations[i] = &zipkincore.BinaryAnnotation{
160 Key: a.key,
161 Value: a.value,
162 AnnotationType: a.annotationType,
163 Host: a.host,
164 }
165 }
166
133167 return &zs
134168 }
135169
139173 duration time.Duration // optional
140174 host *zipkincore.Endpoint
141175 }
176
177 type binaryAnnotation struct {
178 key string
179 value []byte
180 annotationType zipkincore.AnnotationType
181 host *zipkincore.Endpoint
182 }
0 package zipkin_test
1
2 import (
3 "testing"
4
5 "github.com/go-kit/kit/tracing/zipkin"
6 )
7
8 func TestAnnotateBinaryEncodesKeyValueAsBytes(t *testing.T) {
9 key := "awesome-bytes-test"
10 value := []byte("this is neat")
11
12 span := &zipkin.Span{}
13 span.AnnotateBinary(key, value)
14
15 encodedSpan := span.Encode()
16 annotations := encodedSpan.GetBinaryAnnotations()
17
18 if annotations[0].Key != key {
19 t.Errorf("Error: expected %s got %s", key, annotations[0].Key)
20 }
21
22 if string(annotations[0].Value) != string(value) {
23 t.Errorf("Error: expected %s got %s", string(value), string(annotations[0].Value))
24 }
25 }
26
27 func TestAnnotateStringEncodesKeyValueAsBytes(t *testing.T) {
28 key := "awesome-string-test"
29 value := "this is neat"
30
31 span := &zipkin.Span{}
32 span.AnnotateString(key, value)
33
34 encodedSpan := span.Encode()
35 annotations := encodedSpan.GetBinaryAnnotations()
36
37 if annotations[0].Key != key {
38 t.Errorf("Error: expected %s got %s", key, annotations[0].Key)
39 }
40
41 if string(annotations[0].Value) != value {
42 t.Errorf("Error: expected %s got %s", value, string(annotations[0].Value))
43 }
44 }