/
protospan_to_zipkinv1.go
369 lines (319 loc) · 10.6 KB
/
protospan_to_zipkinv1.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zipkin
import (
"net"
"strconv"
"time"
commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
zipkinmodel "github.com/openzipkin/zipkin-go/model"
"github.com/pkg/errors"
"go.opentelemetry.io/collector/internal"
tracetranslator "go.opentelemetry.io/collector/translator/trace"
)
const (
statusCodeTagKey = "error"
statusDescriptionTagKey = "opencensus.status_description"
)
var (
sampledTrue = true
canonicalCodes = [...]string{
"OK",
"CANCELLED",
"UNKNOWN",
"INVALID_ARGUMENT",
"DEADLINE_EXCEEDED",
"NOT_FOUND",
"ALREADY_EXISTS",
"PERMISSION_DENIED",
"RESOURCE_EXHAUSTED",
"FAILED_PRECONDITION",
"ABORTED",
"OUT_OF_RANGE",
"UNIMPLEMENTED",
"INTERNAL",
"UNAVAILABLE",
"DATA_LOSS",
"UNAUTHENTICATED",
}
errNilSpan = errors.New("expected a non-nil span")
)
func canonicalCodeString(code int32) string {
if code < 0 || int(code) >= len(canonicalCodes) {
return "error code " + strconv.FormatInt(int64(code), 10)
}
return canonicalCodes[code]
}
// zipkinEndpointFromAttributes extracts zipkin endpoint information
// from a set of attributes (in the format of OC SpanData). It returns the built
// zipkin endpoint and insert the attribute keys that were made redundant
// (because now they can be represented by the endpoint) into the redundantKeys
// map (function assumes that this was created by the caller). Per call at most
// 3 attribute keys can be made redundant.
func zipkinEndpointFromAttributes(
attrMap map[string]*tracepb.AttributeValue,
serviceName string,
endpointType zipkinDirection,
redundantKeys map[string]bool,
) (endpoint *zipkinmodel.Endpoint) {
if attrMap == nil {
return nil
}
// The data in the Attributes map was saved in the format
// {
// "ipv4": "192.168.99.101",
// "port": "9000",
// "serviceName": "backend",
// }
var ipv4Key, ipv6Key, portKey string
if endpointType == isLocalEndpoint {
ipv4Key, ipv6Key, portKey = LocalEndpointIPv4, LocalEndpointIPv6, LocalEndpointPort
} else {
ipv4Key, ipv6Key, portKey = RemoteEndpointIPv4, RemoteEndpointIPv6, RemoteEndpointPort
}
var ip net.IP
ipv6Selected := false
if ipv4Str, ok := extractStringAttribute(attrMap, ipv4Key); ok {
ip = net.ParseIP(ipv4Str)
redundantKeys[ipv4Key] = true
} else if ipv6Str, ok := extractStringAttribute(attrMap, ipv6Key); ok {
ip = net.ParseIP(ipv6Str)
ipv6Selected = true
redundantKeys[ipv6Key] = true
}
var port uint64
if portStr, ok := extractStringAttribute(attrMap, portKey); ok {
port, _ = strconv.ParseUint(portStr, 10, 16)
redundantKeys[portKey] = true
}
if serviceName == "" && len(ip) == 0 && port == 0 {
// Nothing to put on the endpoint
return nil
}
zEndpoint := &zipkinmodel.Endpoint{
ServiceName: serviceName,
Port: uint16(port),
}
if ipv6Selected {
zEndpoint.IPv6 = ip
} else {
zEndpoint.IPv4 = ip
}
return zEndpoint
}
func extractStringAttribute(attributes map[string]*tracepb.AttributeValue, key string) (string, bool) {
av, ok := attributes[key]
if !ok || av == nil || av.Value == nil {
return "", false
}
if value, ok := av.Value.(*tracepb.AttributeValue_StringValue); ok {
return value.StringValue.GetValue(), true
}
return "", false
}
func convertTraceID(t []byte) zipkinmodel.TraceID {
h, l, _ := tracetranslator.BytesToUInt64TraceID(t)
return zipkinmodel.TraceID{High: h, Low: l}
}
func convertSpanID(s []byte) zipkinmodel.ID {
id, _ := tracetranslator.BytesToUInt64SpanID(s)
return zipkinmodel.ID(id)
}
// spanKind returns the Kind and a boolean indicating if the kind was extracted from the attributes.
func spanKind(s *tracepb.Span) (zipkinmodel.Kind, bool) {
// First try span kinds that are set directly in SpanKind field.
switch s.Kind {
case tracepb.Span_CLIENT:
return zipkinmodel.Client, false
case tracepb.Span_SERVER:
return zipkinmodel.Server, false
}
// SpanKind==SpanKindUnspecified, check if TagSpanKind attribute is set.
// This can happen if span kind had no equivalent in OC, so we could represent it in
// the SpanKind. In that case we had set a special attribute TagSpanKind in
// the span to preserve the span kind. Now we will do a reverse translation.
if s.Attributes == nil || s.Attributes.AttributeMap == nil {
return zipkinmodel.Undetermined, false
}
spanKindStr, ok := extractStringAttribute(s.Attributes.AttributeMap, tracetranslator.TagSpanKind)
if ok {
// Check if it is one of the kinds that are defined by conventions.
switch tracetranslator.OpenTracingSpanKind(spanKindStr) {
case tracetranslator.OpenTracingSpanKindClient:
return zipkinmodel.Client, true
case tracetranslator.OpenTracingSpanKindServer:
return zipkinmodel.Server, true
case tracetranslator.OpenTracingSpanKindConsumer:
return zipkinmodel.Consumer, true
case tracetranslator.OpenTracingSpanKindProducer:
return zipkinmodel.Producer, true
default:
// Unknown kind. Keep the attribute, but return Undetermined to our caller.
return zipkinmodel.Undetermined, false
}
}
return zipkinmodel.Undetermined, false
}
type zipkinDirection bool
const (
isLocalEndpoint zipkinDirection = true
isRemoteEndpoint zipkinDirection = false
)
func serviceNameOrDefault(node *commonpb.Node, defaultServiceName string) string {
if node == nil || node.ServiceInfo == nil {
return defaultServiceName
}
if node.ServiceInfo.Name == "" {
return defaultServiceName
}
return node.ServiceInfo.Name
}
func OCSpanProtoToZipkin(
node *commonpb.Node,
resource *resourcepb.Resource,
s *tracepb.Span,
defaultServiceName string,
) (*zipkinmodel.SpanModel, error) {
if s == nil {
return nil, errNilSpan
}
var attrMap map[string]*tracepb.AttributeValue
if s.Attributes != nil {
attrMap = s.Attributes.AttributeMap
}
// Per call to zipkinEndpointFromAttributes at most 3 attribute keys can be made redundant,
// give a hint when calling make that we expect at most 2*3+2 items on the map.
redundantKeys := make(map[string]bool, 8)
localEndpointServiceName := serviceNameOrDefault(node, defaultServiceName)
localEndpoint := zipkinEndpointFromAttributes(attrMap, localEndpointServiceName, isLocalEndpoint, redundantKeys)
remoteServiceName, okServiceName := extractStringAttribute(attrMap, RemoteEndpointServiceName)
if okServiceName {
redundantKeys[RemoteEndpointServiceName] = true
}
remoteEndpoint := zipkinEndpointFromAttributes(attrMap, remoteServiceName, isRemoteEndpoint, redundantKeys)
sk, spanKindFromAttributes := spanKind(s)
if spanKindFromAttributes {
redundantKeys[tracetranslator.TagSpanKind] = true
}
startTime := internal.TimestampToTime(s.StartTime)
if _, ok := attrMap[StartTimeAbsent]; ok {
redundantKeys[StartTimeAbsent] = true
startTime = time.Time{}
}
z := &zipkinmodel.SpanModel{
SpanContext: zipkinmodel.SpanContext{
TraceID: convertTraceID(s.TraceId),
ID: convertSpanID(s.SpanId),
Sampled: &sampledTrue,
},
Kind: sk,
Name: s.Name.GetValue(),
Timestamp: startTime,
Shared: false,
LocalEndpoint: localEndpoint,
RemoteEndpoint: remoteEndpoint,
}
resourceLabelesCount := 0
if resource != nil {
resourceLabelesCount = len(resource.Labels)
}
//
z.Tags = make(map[string]string, len(attrMap)+2+resourceLabelesCount)
resourceToSpanAttributes(resource, z.Tags)
if s.ParentSpanId != nil {
id := convertSpanID(s.ParentSpanId)
z.ParentID = &id
}
if endTime := internal.TimestampToTime(s.EndTime); !startTime.IsZero() && !endTime.IsZero() {
z.Duration = endTime.Sub(startTime)
}
// construct Tags from s.Attributes and s.Status.
attributesToSpanTags(attrMap, redundantKeys, z.Tags)
status := s.Status
if status != nil && (status.Code != 0 || status.Message != "") {
if z.Tags == nil {
z.Tags = make(map[string]string, 2)
}
if s.Status.Code != 0 {
z.Tags[statusCodeTagKey] = canonicalCodeString(s.Status.Code)
}
if s.Status.Message != "" {
z.Tags[statusDescriptionTagKey] = s.Status.Message
}
}
// construct Annotations from s.Annotations and s.MessageEvents.
z.Annotations = timeEventsToSpanAnnotations(s.TimeEvents)
return z, nil
}
func resourceToSpanAttributes(resource *resourcepb.Resource, tags map[string]string) {
if resource != nil {
for key, value := range resource.Labels {
tags[key] = value
}
}
}
func timeEventsToSpanAnnotations(timeEvents *tracepb.Span_TimeEvents) []zipkinmodel.Annotation {
var annotations []zipkinmodel.Annotation
// construct Annotations from s.Annotations and s.MessageEvents.
if timeEvents != nil && len(timeEvents.TimeEvent) != 0 {
annotations = make([]zipkinmodel.Annotation, 0, len(timeEvents.TimeEvent))
for _, te := range timeEvents.TimeEvent {
if te == nil || te.Value == nil {
continue
}
switch tme := te.Value.(type) {
case *tracepb.Span_TimeEvent_Annotation_:
annotations = append(annotations, zipkinmodel.Annotation{
Timestamp: internal.TimestampToTime(te.Time),
Value: tme.Annotation.Description.GetValue(),
})
case *tracepb.Span_TimeEvent_MessageEvent_:
a := zipkinmodel.Annotation{
Timestamp: internal.TimestampToTime(te.Time),
Value: tme.MessageEvent.Type.String(),
}
annotations = append(annotations, a)
}
}
}
return annotations
}
func attributesToSpanTags(attrMap map[string]*tracepb.AttributeValue, redundantKeys map[string]bool, tags map[string]string) {
if len(attrMap) == 0 {
return
}
for key, value := range attrMap {
if redundantKeys[key] {
// Already represented by something other than an attribute,
// skip it.
continue
}
switch v := value.Value.(type) {
case *tracepb.AttributeValue_BoolValue:
if v.BoolValue {
tags[key] = "true"
} else {
tags[key] = "false"
}
case *tracepb.AttributeValue_IntValue:
tags[key] = strconv.FormatInt(v.IntValue, 10)
case *tracepb.AttributeValue_DoubleValue:
tags[key] = strconv.FormatFloat(v.DoubleValue, 'f', -1, 64)
case *tracepb.AttributeValue_StringValue:
tags[key] = v.StringValue.GetValue()
}
}
}