This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
/
translator.go
229 lines (192 loc) · 5.96 KB
/
translator.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
package honeycomb
import (
"errors"
"time"
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
"github.com/golang/protobuf/ptypes/timestamp"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/sdk/export/trace"
"go.opentelemetry.io/otel/sdk/resource"
apitrace "go.opentelemetry.io/otel/trace"
)
// timestampToTime creates a Go time.Time value from a Google protobuf Timestamp.
func timestampToTime(ts *timestamp.Timestamp) (t time.Time) {
if ts == nil {
return
}
return time.Unix(ts.Seconds, int64(ts.Nanos))
}
// Get SpanKind from an OC Span_SpanKind
func oTelSpanKind(kind tracepb.Span_SpanKind) apitrace.SpanKind {
// note that tracepb.SpanKindInternal, tracepb.SpanKindProducer and tracepb.SpanKindConsumer
// have no equivalent OC proto type.
switch kind {
case tracepb.Span_SPAN_KIND_UNSPECIFIED:
return apitrace.SpanKindUnspecified
case tracepb.Span_SERVER:
return apitrace.SpanKindServer
case tracepb.Span_CLIENT:
return apitrace.SpanKindClient
default:
return apitrace.SpanKindUnspecified
}
}
// Creates an OpenTelemetry SpanContext from information in an OC Span.
// Note that the OC Span has no equivalent to TraceFlags field in the
// OpenTelemetry SpanContext type.
func spanContext(traceID []byte, spanID []byte) apitrace.SpanContext {
ctx := apitrace.SpanContext{}
if traceID != nil {
copy(ctx.TraceID[:], traceID[:])
}
if spanID != nil {
copy(ctx.SpanID[:], spanID[:])
}
return ctx
}
func spanResource(span *tracepb.Span) *resource.Resource {
if span.Resource == nil {
return nil
}
attrs := make([]label.KeyValue, len(span.Resource.Labels))
i := 0
for k, v := range span.Resource.Labels {
attrs[i] = label.String(k, v)
i++
}
return resource.NewWithAttributes(attrs...)
}
// Create []kv.KeyValue attributes from an OC *Span_Attributes
func createOTelAttributes(attributes *tracepb.Span_Attributes) []label.KeyValue {
if attributes == nil || attributes.AttributeMap == nil {
return nil
}
oTelAttrs := make([]label.KeyValue, len(attributes.AttributeMap))
i := 0
for key, attributeValue := range attributes.AttributeMap {
keyValue := label.KeyValue{
Key: label.Key(key),
}
switch val := attributeValue.Value.(type) {
case *tracepb.AttributeValue_StringValue:
keyValue.Value = label.StringValue(attributeValueAsString(attributeValue))
case *tracepb.AttributeValue_BoolValue:
keyValue.Value = label.BoolValue(val.BoolValue)
case *tracepb.AttributeValue_IntValue:
keyValue.Value = label.Int64Value(val.IntValue)
case *tracepb.AttributeValue_DoubleValue:
keyValue.Value = label.Float64Value(val.DoubleValue)
}
oTelAttrs[i] = keyValue
i++
}
return oTelAttrs
}
// Create Span Links (including their attributes) from an OC Span
func createSpanLinks(spanLinks *tracepb.Span_Links) []apitrace.Link {
if spanLinks == nil {
return nil
}
links := make([]apitrace.Link, len(spanLinks.Link))
for i, link := range spanLinks.Link {
traceLink := apitrace.Link{
SpanContext: spanContext(link.GetTraceId(), link.GetSpanId()),
Attributes: createOTelAttributes(link.Attributes),
}
links[i] = traceLink
}
return links
}
func createMessageEvents(spanEvents *tracepb.Span_TimeEvents) []trace.Event {
if spanEvents == nil {
return nil
}
annotations := 0
for _, event := range spanEvents.TimeEvent {
if annotation := event.GetAnnotation(); annotation != nil {
annotations++
}
}
events := make([]trace.Event, annotations)
for i, event := range spanEvents.TimeEvent {
if annotation := event.GetAnnotation(); annotation != nil {
events[i] = trace.Event{
Time: timestampToTime(event.GetTime()),
Name: annotation.GetDescription().GetValue(),
Attributes: createOTelAttributes(annotation.GetAttributes()),
}
}
}
return events
}
func attributeValueAsString(val *tracepb.AttributeValue) string {
if wrapper := val.GetStringValue(); wrapper != nil {
return wrapper.GetValue()
}
return ""
}
func getDroppedLinkCount(links *tracepb.Span_Links) int {
if links != nil {
return int(links.DroppedLinksCount)
}
return 0
}
func getChildSpanCount(span *tracepb.Span) int {
if count := span.GetChildSpanCount(); count != nil {
return int(count.GetValue())
}
return 0
}
func getSpanName(span *tracepb.Span) string {
if name := span.GetName(); name != nil {
return name.GetValue()
}
return ""
}
func getHasRemoteParent(span *tracepb.Span) bool {
if sameProcess := span.GetSameProcessAsParentSpan(); sameProcess != nil {
return !sameProcess.Value
}
return false
}
func getStatusCode(span *tracepb.Span) codes.Code {
if span.Status != nil {
return codes.Code(span.Status.Code)
}
return codes.Ok
}
func getStatusMessage(span *tracepb.Span) string {
switch {
case span.Status == nil:
return codes.Ok.String()
case span.Status.Message != "":
return span.Status.Message
default:
return codes.Code(span.Status.Code).String()
}
}
// OCProtoSpanToOTelSpanSnapshot converts an OC Span to an OTel SpanSnapshot
func OCProtoSpanToOTelSpanSnapshot(span *tracepb.Span) (*trace.SpanSnapshot, error) {
if span == nil {
return nil, errors.New("expected a non-nil span")
}
spanData := &trace.SpanSnapshot{
SpanContext: spanContext(span.GetTraceId(), span.GetSpanId()),
}
copy(spanData.ParentSpanID[:], span.GetParentSpanId()[:])
spanData.Name = getSpanName(span)
spanData.SpanKind = oTelSpanKind(span.GetKind())
spanData.Links = createSpanLinks(span.GetLinks())
spanData.Attributes = createOTelAttributes(span.GetAttributes())
spanData.MessageEvents = createMessageEvents(span.GetTimeEvents())
spanData.StartTime = timestampToTime(span.GetStartTime())
spanData.EndTime = timestampToTime(span.GetEndTime())
spanData.StatusCode = getStatusCode(span)
spanData.StatusMessage = getStatusMessage(span)
spanData.HasRemoteParent = getHasRemoteParent(span)
spanData.DroppedLinkCount = getDroppedLinkCount(span.GetLinks())
spanData.ChildSpanCount = getChildSpanCount(span)
spanData.Resource = spanResource(span)
return spanData, nil
}