forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_domain.go
255 lines (232 loc) · 6.79 KB
/
to_domain.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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// 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 dbmodel
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
"github.com/jaegertracing/jaeger/model"
)
// NewToDomain creates ToDomain
func NewToDomain(tagDotReplacement string) ToDomain {
return ToDomain{tagDotReplacement: tagDotReplacement}
}
// ToDomain is used to convert Span to model.Span
type ToDomain struct {
tagDotReplacement string
}
// ReplaceDot replaces dot with dotReplacement
func (td ToDomain) ReplaceDot(k string) string {
return strings.Replace(k, ".", td.tagDotReplacement, -1)
}
// ReplaceDotReplacement replaces dotReplacement with dot
func (td ToDomain) ReplaceDotReplacement(k string) string {
return strings.Replace(k, td.tagDotReplacement, ".", -1)
}
// SpanToDomain converts db span into model Span
func (td ToDomain) SpanToDomain(dbSpan *Span) (*model.Span, error) {
tags, err := td.convertKeyValues(dbSpan.Tags)
if err != nil {
return nil, err
}
logs, err := td.convertLogs(dbSpan.Logs)
if err != nil {
return nil, err
}
refs, err := td.convertRefs(dbSpan.References)
if err != nil {
return nil, err
}
process, err := td.convertProcess(dbSpan.Process)
if err != nil {
return nil, err
}
traceID, err := model.TraceIDFromString(string(dbSpan.TraceID))
if err != nil {
return nil, err
}
spanIDInt, err := model.SpanIDFromString(string(dbSpan.SpanID))
if err != nil {
return nil, err
}
if dbSpan.ParentSpanID != "" {
parentSpanID, err := model.SpanIDFromString(string(dbSpan.ParentSpanID))
if err != nil {
return nil, err
}
refs = model.MaybeAddParentSpanID(traceID, parentSpanID, refs)
}
fieldTags, err := td.convertTagFields(dbSpan.Tag)
if err != nil {
return nil, err
}
tags = append(tags, fieldTags...)
span := &model.Span{
TraceID: traceID,
SpanID: model.NewSpanID(uint64(spanIDInt)),
OperationName: dbSpan.OperationName,
References: refs,
Flags: model.Flags(uint32(dbSpan.Flags)),
StartTime: model.EpochMicrosecondsAsTime(dbSpan.StartTime),
Duration: model.MicrosecondsAsDuration(dbSpan.Duration),
Tags: tags,
Logs: logs,
Process: process,
}
return span, nil
}
func (td ToDomain) convertRefs(refs []Reference) ([]model.SpanRef, error) {
retMe := make([]model.SpanRef, len(refs))
for i, r := range refs {
// There are some inconsistencies with ReferenceTypes, hence the hacky fix.
var refType model.SpanRefType
switch r.RefType {
case ChildOf:
refType = model.ChildOf
case FollowsFrom:
refType = model.FollowsFrom
default:
return nil, fmt.Errorf("not a valid SpanRefType string %s", string(r.RefType))
}
traceID, err := model.TraceIDFromString(string(r.TraceID))
if err != nil {
return nil, err
}
spanID, err := strconv.ParseUint(string(r.SpanID), 16, 64)
if err != nil {
return nil, err
}
retMe[i] = model.SpanRef{
RefType: refType,
TraceID: traceID,
SpanID: model.NewSpanID(spanID),
}
}
return retMe, nil
}
func (td ToDomain) convertKeyValues(tags []KeyValue) ([]model.KeyValue, error) {
retMe := make([]model.KeyValue, len(tags))
for i := range tags {
kv, err := td.convertKeyValue(&tags[i])
if err != nil {
return nil, err
}
retMe[i] = kv
}
return retMe, nil
}
func (td ToDomain) convertTagFields(tagsMap map[string]interface{}) ([]model.KeyValue, error) {
kvs := make([]model.KeyValue, len(tagsMap))
i := 0
for k, v := range tagsMap {
tag, err := td.convertTagField(k, v)
if err != nil {
return nil, err
}
kvs[i] = tag
i++
}
return kvs, nil
}
func (td ToDomain) convertTagField(k string, v interface{}) (model.KeyValue, error) {
dKey := td.ReplaceDotReplacement(k)
// The number is always a float64 therefore type assertion on int (v.(int/64/32)) does not work.
// If 1.0, 2.0.. was stored as float it will be read as int
if pInt, err := strconv.ParseInt(fmt.Sprintf("%v", v), 10, 64); err == nil {
return model.Int64(dKey, pInt), nil
}
switch val := v.(type) {
case float64:
return model.Float64(dKey, val), nil
case bool:
return model.Bool(dKey, val), nil
case string:
return model.String(dKey, val), nil
// the binary is never returned, ES returns it as string with base64 encoding
case []byte:
return model.Binary(dKey, val), nil
default:
return model.String("", ""), fmt.Errorf("invalid tag type in %+v", v)
}
}
// convertKeyValue expects the Value field to be string, because it only works
// as a reverse transformation after FromDomain() for ElasticSearch model.
func (td ToDomain) convertKeyValue(tag *KeyValue) (model.KeyValue, error) {
if tag.Value == nil {
return model.KeyValue{}, fmt.Errorf("invalid nil Value in %v", tag)
}
tagValue, ok := tag.Value.(string)
if !ok {
return model.KeyValue{}, fmt.Errorf("non-string Value of type %t in %v", tag.Value, tag)
}
switch tag.Type {
case StringType:
return model.String(tag.Key, tagValue), nil
case BoolType:
value, err := strconv.ParseBool(tagValue)
if err != nil {
return model.KeyValue{}, err
}
return model.Bool(tag.Key, value), nil
case Int64Type:
value, err := strconv.ParseInt(tagValue, 10, 64)
if err != nil {
return model.KeyValue{}, err
}
return model.Int64(tag.Key, value), nil
case Float64Type:
value, err := strconv.ParseFloat(tagValue, 64)
if err != nil {
return model.KeyValue{}, err
}
return model.Float64(tag.Key, value), nil
case BinaryType:
value, err := hex.DecodeString(tagValue)
if err != nil {
return model.KeyValue{}, err
}
return model.Binary(tag.Key, value), nil
}
return model.KeyValue{}, fmt.Errorf("not a valid ValueType string %s", string(tag.Type))
}
func (td ToDomain) convertLogs(logs []Log) ([]model.Log, error) {
retMe := make([]model.Log, len(logs))
for i, l := range logs {
fields, err := td.convertKeyValues(l.Fields)
if err != nil {
return nil, err
}
retMe[i] = model.Log{
Timestamp: model.EpochMicrosecondsAsTime(l.Timestamp),
Fields: fields,
}
}
return retMe, nil
}
func (td ToDomain) convertProcess(process Process) (*model.Process, error) {
tags, err := td.convertKeyValues(process.Tags)
if err != nil {
return nil, err
}
fieldTags, err := td.convertTagFields(process.Tag)
if err != nil {
return nil, err
}
tags = append(tags, fieldTags...)
return &model.Process{
Tags: tags,
ServiceName: process.ServiceName,
}, nil
}