Codebase list golang-github-go-kit-kit / 724fee9
Merge pull request #191 from taion809/feature-binary-annotations Minimal Binary Annotations Support Peter Bourgon 8 years ago
2 changed file(s) with 101 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 Debug: true, // TODO
116135 }
136
117137 if s.parentSpanID != 0 {
118138 zs.ParentId = new(int64)
119139 (*zs.ParentId) = s.parentSpanID
120140 }
141
121142 zs.Annotations = make([]*zipkincore.Annotation, len(s.annotations))
122143 for i, a := range s.annotations {
123144 zs.Annotations[i] = &zipkincore.Annotation{
125146 Value: a.value,
126147 Host: a.host,
127148 }
149
128150 if a.duration > 0 {
129151 zs.Annotations[i].Duration = new(int32)
130152 *(zs.Annotations[i].Duration) = int32(a.duration / time.Microsecond)
131153 }
132154 }
155
156 zs.BinaryAnnotations = make([]*zipkincore.BinaryAnnotation, len(s.binaryAnnotations))
157 for i, a := range s.binaryAnnotations {
158 zs.BinaryAnnotations[i] = &zipkincore.BinaryAnnotation{
159 Key: a.key,
160 Value: a.value,
161 AnnotationType: a.annotationType,
162 Host: a.host,
163 }
164 }
165
133166 return &zs
134167 }
135168
139172 duration time.Duration // optional
140173 host *zipkincore.Endpoint
141174 }
175
176 type binaryAnnotation struct {
177 key string
178 value []byte
179 annotationType zipkincore.AnnotationType
180 host *zipkincore.Endpoint
181 }
0 package zipkin_test
1
2 import (
3 "bytes"
4 "testing"
5
6 "github.com/go-kit/kit/tracing/zipkin"
7 )
8
9 func TestAnnotateBinaryEncodesKeyValueAsBytes(t *testing.T) {
10 key := "awesome-bytes-test"
11 value := []byte("this is neat")
12
13 span := &zipkin.Span{}
14 span.AnnotateBinary(key, value)
15
16 encodedSpan := span.Encode()
17 annotations := encodedSpan.GetBinaryAnnotations()
18
19 if len(annotations) == 0 {
20 t.Error("want non-zero length slice, have empty slice")
21 }
22
23 if want, have := key, annotations[0].Key; want != have {
24 t.Errorf("want %q, got %q", want, have)
25 }
26
27 if want, have := value, annotations[0].Value; bytes.Compare(want, have) != 0 {
28 t.Errorf("want %s, got %s", want, have)
29 }
30 }
31
32 func TestAnnotateStringEncodesKeyValueAsBytes(t *testing.T) {
33 key := "awesome-string-test"
34 value := "this is neat"
35
36 span := &zipkin.Span{}
37 span.AnnotateString(key, value)
38
39 encodedSpan := span.Encode()
40 annotations := encodedSpan.GetBinaryAnnotations()
41
42 if len(annotations) == 0 {
43 t.Error("want non-zero length slice, have empty slice")
44 }
45
46 if want, have := key, annotations[0].Key; want != have {
47 t.Errorf("want %q, got %q", want, have)
48 }
49
50 if want, have := value, annotations[0].Value; bytes.Compare([]byte(want), have) != 0 {
51 t.Errorf("want %s, got %s", want, have)
52 }
53 }