Skip to content

Commit 6a90827

Browse files
Manik2708amol-verma-allen
authored andcommitted
[ES][v2] Change the DB Tag value from string to any type (jaegertracing#6994)
## Which problem is this PR solving? Fixes a part of: jaegertracing#6458 ## Description of the changes - As discussed in jaegertracing#6946 we need to change the value which was saved as string in v1 to a raw value in v2 ## How was this change tested? - Unit Tests ## Checklist - [x] I have read https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md - [x] I have signed all commits - [x] I have added unit tests for the new functionality - [x] I have run lint and test steps successfully - for `jaeger`: `make lint test` - for `jaeger-ui`: `npm run lint` and `npm run test` Signed-off-by: Manik2708 <[email protected]> Signed-off-by: amol-verma-allen <[email protected]>
1 parent 0f03fde commit 6a90827

File tree

6 files changed

+277
-24
lines changed

6 files changed

+277
-24
lines changed

internal/storage/v2/elasticsearch/tracestore/fixtures/es_01.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
{
4343
"key": "peer.ipv4",
4444
"type": "int64",
45-
"value": "23456"
45+
"value": 23456
4646
},
4747
{
4848
"key": "blob",
@@ -52,12 +52,12 @@
5252
{
5353
"key": "temperature",
5454
"type": "float64",
55-
"value": "72.5"
55+
"value": 72.5
5656
},
5757
{
5858
"key": "error",
5959
"type": "bool",
60-
"value": "true"
60+
"value": true
6161
}
6262
],
6363
"logs": [
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"traceID": "00000000000000010000000000000000",
3+
"spanID": "0000000000000002",
4+
"flags": 1,
5+
"operationName": "test-general-conversion",
6+
"references": [
7+
{
8+
"refType": "CHILD_OF",
9+
"traceID": "00000000000000010000000000000000",
10+
"spanID": "0000000000000003"
11+
},
12+
{
13+
"refType": "FOLLOWS_FROM",
14+
"traceID": "00000000000000010000000000000000",
15+
"spanID": "0000000000000004"
16+
},
17+
{
18+
"refType": "CHILD_OF",
19+
"traceID": "00000000000000ff0000000000000000",
20+
"spanID": "00000000000000ff"
21+
}
22+
],
23+
"startTime": 1485467191639875,
24+
"startTimeMillis": 1485467191639,
25+
"duration": 5,
26+
"tags": [
27+
{
28+
"key": "otel.scope.name",
29+
"type": "string",
30+
"value": "testing-library"
31+
},
32+
{
33+
"key": "otel.scope.version",
34+
"type": "string",
35+
"value": "1.1.1"
36+
},
37+
{
38+
"key": "peer.service",
39+
"type": "string",
40+
"value": "service-y"
41+
},
42+
{
43+
"key": "peer.ipv4",
44+
"type": "int64",
45+
"value": "23456"
46+
},
47+
{
48+
"key": "blob",
49+
"type": "binary",
50+
"value": "00003039"
51+
},
52+
{
53+
"key": "temperature",
54+
"type": "float64",
55+
"value": "72.5"
56+
},
57+
{
58+
"key": "error",
59+
"type": "bool",
60+
"value": "true"
61+
}
62+
],
63+
"logs": [
64+
{
65+
"timestamp": 1485467191639875,
66+
"fields": [
67+
{
68+
"key": "event",
69+
"type": "string",
70+
"value": "testing-event"
71+
},
72+
{
73+
"key": "event-x",
74+
"type": "string",
75+
"value": "event-y"
76+
}
77+
]
78+
},
79+
{
80+
"timestamp": 1485467191639875,
81+
"fields": [
82+
{
83+
"key": "x",
84+
"type": "string",
85+
"value": "y"
86+
}
87+
]
88+
}
89+
],
90+
"process": {
91+
"serviceName": "service-x",
92+
"tags": [
93+
{
94+
"key": "sdk.version",
95+
"type": "string",
96+
"value": "1.2.1"
97+
}
98+
]
99+
}
100+
}

internal/storage/v2/elasticsearch/tracestore/from_dbmodel.go

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package tracestore
88

99
import (
1010
"encoding/hex"
11+
"encoding/json"
1112
"errors"
1213
"fmt"
1314
"strconv"
@@ -134,12 +135,19 @@ func dbTagsToAttributes(tags []dbmodel.KeyValue, attributes pcommon.Map) {
134135
for _, tag := range tags {
135136
tagValue, ok := tag.Value.(string)
136137
if !ok {
137-
// We have to do this as we are unsure that whether bool will be a string or a bool
138-
tagBoolVal, boolOk := tag.Value.(bool)
139-
if boolOk && tag.Type == dbmodel.BoolType {
140-
attributes.PutBool(tag.Key, tagBoolVal)
141-
} else {
142-
attributes.PutStr(tag.Key, "Got non string inputValue for the key "+tag.Key)
138+
switch tag.Type {
139+
case dbmodel.Float64Type, dbmodel.Int64Type:
140+
fromDBNumber(tag, attributes)
141+
case dbmodel.BoolType:
142+
v, ok := tag.Value.(bool)
143+
if !ok {
144+
recordTagInvalidTypeError(tag, attributes)
145+
} else {
146+
attributes.PutBool(tag.Key, v)
147+
}
148+
default:
149+
// This means type is string/binary but value is of non string type, hence record the type error
150+
recordTagInvalidTypeError(tag, attributes)
143151
}
144152
continue
145153
}
@@ -180,6 +188,41 @@ func dbTagsToAttributes(tags []dbmodel.KeyValue, attributes pcommon.Map) {
180188
}
181189
}
182190

191+
func fromDBNumber(kv dbmodel.KeyValue, dest pcommon.Map) {
192+
if kv.Type == dbmodel.Int64Type {
193+
switch v := kv.Value.(type) {
194+
case int64:
195+
dest.PutInt(kv.Key, v)
196+
case float64:
197+
// This case is possible as unmarshalling the JSON converts every int value to float64
198+
dest.PutInt(kv.Key, int64(v))
199+
case json.Number:
200+
n, err := v.Int64()
201+
if err == nil {
202+
dest.PutInt(kv.Key, n)
203+
}
204+
default:
205+
recordTagInvalidTypeError(kv, dest)
206+
}
207+
} else if kv.Type == dbmodel.Float64Type {
208+
switch v := kv.Value.(type) {
209+
case float64:
210+
dest.PutDouble(kv.Key, v)
211+
case json.Number:
212+
n, err := v.Float64()
213+
if err == nil {
214+
dest.PutDouble(kv.Key, n)
215+
}
216+
default:
217+
recordTagInvalidTypeError(kv, dest)
218+
}
219+
}
220+
}
221+
222+
func recordTagInvalidTypeError(kv dbmodel.KeyValue, dest pcommon.Map) {
223+
dest.PutStr(kv.Key, fmt.Sprintf("invalid %s type in %+v", string(kv.Type), kv.Value))
224+
}
225+
183226
func recordTagConversionError(kv dbmodel.KeyValue, err error, dest pcommon.Map) {
184227
dest.PutStr(kv.Key, fmt.Sprintf("Can't convert the type %s for the key %s: %v", string(kv.Type), kv.Key, err))
185228
}

internal/storage/v2/elasticsearch/tracestore/from_dbmodel_test.go

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,17 @@ func TestSetAttributesFromDbTags(t *testing.T) {
215215
p.PutBool("bool-val", true)
216216
},
217217
},
218+
{
219+
name: "wrong non string int input value",
220+
keyModel: dbmodel.KeyValue{
221+
Key: "bool-val",
222+
Type: dbmodel.BoolType,
223+
Value: 12,
224+
},
225+
expectedValueFn: func(p pcommon.Map) {
226+
p.PutStr("bool-val", "invalid bool type in 12")
227+
},
228+
},
218229
{
219230
name: "wrong int input value",
220231
keyModel: dbmodel.KeyValue{
@@ -237,6 +248,50 @@ func TestSetAttributesFromDbTags(t *testing.T) {
237248
p.PutInt("int-val", 123)
238249
},
239250
},
251+
{
252+
name: "right non string int input value",
253+
keyModel: dbmodel.KeyValue{
254+
Key: "int-val",
255+
Type: dbmodel.Int64Type,
256+
Value: int64(123),
257+
},
258+
expectedValueFn: func(p pcommon.Map) {
259+
p.PutInt("int-val", 123)
260+
},
261+
},
262+
{
263+
name: "right non string int float input value",
264+
keyModel: dbmodel.KeyValue{
265+
Key: "int-val",
266+
Type: dbmodel.Int64Type,
267+
Value: float64(123),
268+
},
269+
expectedValueFn: func(p pcommon.Map) {
270+
p.PutInt("int-val", 123)
271+
},
272+
},
273+
{
274+
name: "right non string json number int input value",
275+
keyModel: dbmodel.KeyValue{
276+
Key: "int-val",
277+
Type: dbmodel.Int64Type,
278+
Value: json.Number("123"),
279+
},
280+
expectedValueFn: func(p pcommon.Map) {
281+
p.PutInt("int-val", 123)
282+
},
283+
},
284+
{
285+
name: "wrong non string int input value",
286+
keyModel: dbmodel.KeyValue{
287+
Key: "int-val",
288+
Type: dbmodel.Int64Type,
289+
Value: true,
290+
},
291+
expectedValueFn: func(p pcommon.Map) {
292+
p.PutStr("int-val", "invalid int64 type in true")
293+
},
294+
},
240295
{
241296
name: "wrong double input value",
242297
keyModel: dbmodel.KeyValue{
@@ -259,6 +314,39 @@ func TestSetAttributesFromDbTags(t *testing.T) {
259314
p.PutDouble("double-val", 1.23)
260315
},
261316
},
317+
{
318+
name: "right non string double input value",
319+
keyModel: dbmodel.KeyValue{
320+
Key: "double-val",
321+
Type: dbmodel.Float64Type,
322+
Value: 25.6,
323+
},
324+
expectedValueFn: func(p pcommon.Map) {
325+
p.PutDouble("double-val", 25.6)
326+
},
327+
},
328+
{
329+
name: "right non string json number double input value",
330+
keyModel: dbmodel.KeyValue{
331+
Key: "double-val",
332+
Type: dbmodel.Float64Type,
333+
Value: json.Number("123.56"),
334+
},
335+
expectedValueFn: func(p pcommon.Map) {
336+
p.PutDouble("double-val", 123.56)
337+
},
338+
},
339+
{
340+
name: "wrong non string float input value",
341+
keyModel: dbmodel.KeyValue{
342+
Key: "double-val",
343+
Type: dbmodel.Float64Type,
344+
Value: true,
345+
},
346+
expectedValueFn: func(p pcommon.Map) {
347+
p.PutStr("double-val", "invalid float64 type in true")
348+
},
349+
},
262350
{
263351
name: "wrong binary input value",
264352
keyModel: dbmodel.KeyValue{
@@ -282,14 +370,14 @@ func TestSetAttributesFromDbTags(t *testing.T) {
282370
},
283371
},
284372
{
285-
name: "non-string input value",
373+
name: "non-string input value with string type",
286374
keyModel: dbmodel.KeyValue{
287375
Key: "bool-val",
288-
Type: dbmodel.Int64Type,
376+
Type: dbmodel.StringType,
289377
Value: 123,
290378
},
291379
expectedValueFn: func(p pcommon.Map) {
292-
p.PutStr("bool-val", "Got non string inputValue for the key bool-val")
380+
p.PutStr("bool-val", "invalid string type in 123")
293381
},
294382
},
295383
{
@@ -551,6 +639,17 @@ func TestFromDbModel_Fixtures(t *testing.T) {
551639
testTraces(t, tracesData, actualTd)
552640
}
553641

642+
func TestToDbModel_Fixtures_StringTags(t *testing.T) {
643+
spanData, err := os.ReadFile("fixtures/es_01_string_tags.json")
644+
require.NoError(t, err)
645+
var dbSpan dbmodel.Span
646+
require.NoError(t, json.Unmarshal(spanData, &dbSpan))
647+
td, err := FromDBModel([]dbmodel.Span{dbSpan})
648+
require.NoError(t, err)
649+
expectedTraces := loadTraces(t, 1)
650+
testTraces(t, expectedTraces, td)
651+
}
652+
554653
func getDbTraceIdFromByteArray(arr [16]byte) dbmodel.TraceID {
555654
return dbmodel.TraceID(hex.EncodeToString(arr[:]))
556655
}

internal/storage/v2/elasticsearch/tracestore/to_dbmodel.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ func appendTagsFromAttributes(dest []dbmodel.KeyValue, attrs pcommon.Map) []dbmo
100100

101101
func attributeToDbTag(key string, attr pcommon.Value) dbmodel.KeyValue {
102102
var tag dbmodel.KeyValue
103-
if attr.Type() == pcommon.ValueTypeBytes {
103+
switch attr.Type() {
104+
case pcommon.ValueTypeBytes:
104105
tag = dbmodel.KeyValue{Key: key, Value: hex.EncodeToString(attr.Bytes().AsRaw())}
105-
} else {
106-
// TODO why are all values being converted to strings?
106+
case pcommon.ValueTypeMap:
107107
tag = dbmodel.KeyValue{Key: key, Value: attr.AsString()}
108+
default:
109+
tag = dbmodel.KeyValue{Key: key, Value: attr.AsRaw()}
108110
}
109111
switch attr.Type() {
110112
case pcommon.ValueTypeStr:
@@ -294,7 +296,7 @@ func getTagFromStatusCode(statusCode ptrace.StatusCode) (dbmodel.KeyValue, bool)
294296
return dbmodel.KeyValue{
295297
Key: tagError,
296298
Type: dbmodel.BoolType,
297-
Value: "true",
299+
Value: true,
298300
}, true
299301
} else if statusCode == ptrace.StatusCodeOk {
300302
return dbmodel.KeyValue{

0 commit comments

Comments
 (0)