Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not truncate tags in Elasticsearch #1970

Merged
merged 3 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/collector/app/sanitizer/utf8_sanitizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *utf8Sanitizer) logSpan(span *model.Span, message string, field zapcore.
func sanitizeKV(keyValues model.KeyValues) {
for i, kv := range keyValues {
if !utf8.ValidString(kv.Key) {
keyValues[i] = model.Binary(invalidTagKey, []byte(fmt.Sprintf("%s:%s", kv.Key, kv.AsString())))
keyValues[i] = model.Binary(invalidTagKey, []byte(fmt.Sprintf("%s:%s", kv.Key, kv.AsStringLossy())))
} else if kv.VType == model.StringType && !utf8.ValidString(kv.VStr) {
keyValues[i] = model.Binary(kv.Key, []byte(kv.VStr))
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/collector/app/span_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func TestSpanProcessorWithCollectorTags(t *testing.T) {
var foundTag bool
for _, tag := range span.Process.Tags {
if tag.GetKey() == k {
assert.Equal(t, v, tag.AsString())
assert.Equal(t, v, tag.AsStringLossy())
pavolloffay marked this conversation as resolved.
Show resolved Hide resolved
foundTag = true
break
}
Expand Down
2 changes: 1 addition & 1 deletion model/converter/json/from_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (fd fromDomain) convertKeyValuesString(keyValues model.KeyValues) []json.Ke
out[i] = json.KeyValue{
Key: kv.Key,
Type: json.ValueType(strings.ToLower(kv.VType.String())),
Value: kv.AsString(),
Value: kv.AsStringLossy(),
pavolloffay marked this conversation as resolved.
Show resolved Hide resolved
}
}
return out
Expand Down
25 changes: 23 additions & 2 deletions model/keyvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func (kv *KeyValue) Value() interface{} {
}
}

// AsString returns a potentially lossy string representation of the value.
func (kv *KeyValue) AsString() string {
// AsStringLossy returns a potentially lossy string representation of the value.
func (kv *KeyValue) AsStringLossy() string {
switch kv.VType {
case StringType:
return kv.VStr
Expand All @@ -144,6 +144,27 @@ func (kv *KeyValue) AsString() string {
}
}

// AsString returns a string representation of the value.
func (kv *KeyValue) AsString() string {
pavolloffay marked this conversation as resolved.
Show resolved Hide resolved
switch kv.VType {
case StringType:
return kv.VStr
case BoolType:
if kv.Bool() {
return "true"
}
return "false"
case Int64Type:
return strconv.FormatInt(kv.Int64(), 10)
case Float64Type:
return strconv.FormatFloat(kv.Float64(), 'g', 10, 64)
case BinaryType:
return hex.EncodeToString(kv.VBinary)
default:
return fmt.Sprintf("unknown type %d", kv.VType)
}
}

// IsLess compares KeyValue object with another KeyValue.
// The order is based first on the keys, then on type, and finally on the value.
func (kv *KeyValue) IsLess(two *KeyValue) bool {
Expand Down
4 changes: 2 additions & 2 deletions model/keyvalue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ func TestKeyValueAsStringAndValue(t *testing.T) {
for _, tt := range testCases {
testCase := tt // capture loop var
t.Run(testCase.str, func(t *testing.T) {
assert.Equal(t, testCase.str, testCase.kv.AsString())
assert.Equal(t, testCase.str, testCase.kv.AsStringLossy())
pavolloffay marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, testCase.val, testCase.kv.Value())
})
}
t.Run("invalid type", func(t *testing.T) {
kv := model.KeyValue{Key: "x", VType: model.ValueType(-1)}
assert.Equal(t, "unknown type -1", kv.AsString())
assert.Equal(t, "unknown type -1", kv.AsStringLossy())
pavolloffay marked this conversation as resolved.
Show resolved Hide resolved
assert.EqualError(t, kv.Value().(error), "unknown type -1")
})
}
Expand Down
4 changes: 2 additions & 2 deletions model/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ func (s *Span) Hash(w io.Writer) (err error) {
// HasSpanKind returns true if the span has a `span.kind` tag set to `kind`.
func (s *Span) HasSpanKind(kind ext.SpanKindEnum) bool {
if tag, ok := KeyValues(s.Tags).FindByKey(string(ext.SpanKind)); ok {
return tag.AsString() == string(kind)
return tag.AsStringLossy() == string(kind)
pavolloffay marked this conversation as resolved.
Show resolved Hide resolved
}
return false
}

// GetSpanKind returns value of `span.kind` tag and whether the tag can be found
func (s *Span) GetSpanKind() (spanKind string, found bool) {
if tag, ok := KeyValues(s.Tags).FindByKey(string(ext.SpanKind)); ok {
return tag.AsString(), true
return tag.AsStringLossy(), true
pavolloffay marked this conversation as resolved.
Show resolved Hide resolved
}
return "", false
}
Expand Down
6 changes: 3 additions & 3 deletions plugin/storage/badger/spanstore/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ func (w *SpanWriter) WriteSpan(span *model.Span) error {
for _, kv := range span.Tags {
// Convert everything to string since queries are done that way also
pavolloffay marked this conversation as resolved.
Show resolved Hide resolved
// KEY: it<serviceName><tagsKey><traceId> VALUE: <tagsValue>
entriesToStore = append(entriesToStore, w.createBadgerEntry(createIndexKey(tagIndexKey, []byte(span.Process.ServiceName+kv.Key+kv.AsString()), startTime, span.TraceID), nil, expireTime))
entriesToStore = append(entriesToStore, w.createBadgerEntry(createIndexKey(tagIndexKey, []byte(span.Process.ServiceName+kv.Key+kv.AsStringLossy()), startTime, span.TraceID), nil, expireTime))
}

for _, kv := range span.Process.Tags {
entriesToStore = append(entriesToStore, w.createBadgerEntry(createIndexKey(tagIndexKey, []byte(span.Process.ServiceName+kv.Key+kv.AsString()), startTime, span.TraceID), nil, expireTime))
entriesToStore = append(entriesToStore, w.createBadgerEntry(createIndexKey(tagIndexKey, []byte(span.Process.ServiceName+kv.Key+kv.AsStringLossy()), startTime, span.TraceID), nil, expireTime))
}

for _, log := range span.Logs {
for _, kv := range log.Fields {
entriesToStore = append(entriesToStore, w.createBadgerEntry(createIndexKey(tagIndexKey, []byte(span.Process.ServiceName+kv.Key+kv.AsString()), startTime, span.TraceID), nil, expireTime))
entriesToStore = append(entriesToStore, w.createBadgerEntry(createIndexKey(tagIndexKey, []byte(span.Process.ServiceName+kv.Key+kv.AsStringLossy()), startTime, span.TraceID), nil, expireTime))
}
}

Expand Down
2 changes: 1 addition & 1 deletion plugin/storage/cassandra/spanstore/dbmodel/unique_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func GetAllUniqueTags(span *model.Span, tagFilter TagFilter) []TagInsertion {
uniqueTags = append(uniqueTags, TagInsertion{
ServiceName: span.Process.ServiceName,
TagKey: allTags[i].Key,
TagValue: allTags[i].AsString(),
TagValue: allTags[i].AsStringLossy(),
pavolloffay marked this conversation as resolved.
Show resolved Hide resolved
})
}
return uniqueTags
Expand Down
37 changes: 7 additions & 30 deletions plugin/storage/es/spanstore/dbmodel/from_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
package dbmodel

import (
"encoding/hex"
"fmt"
"strconv"
"strings"

"github.com/jaegertracing/jaeger/model"
Expand Down Expand Up @@ -98,11 +95,7 @@ func (fd FromDomain) convertKeyValuesString(keyValues model.KeyValues) ([]KeyVal
}
tagsMap[strings.Replace(kv.Key, ".", fd.tagDotReplacement, -1)] = kv.Value()
} else {
kvs = append(kvs, KeyValue{
Key: kv.Key,
Type: ValueType(strings.ToLower(kv.VType.String())),
Value: convertKeyValueType(kv),
})
kvs = append(kvs, convertKeyValue(kv))
}
}
if kvs == nil {
Expand All @@ -116,11 +109,7 @@ func (fd FromDomain) convertLogs(logs []model.Log) []Log {
for i, log := range logs {
var kvs []KeyValue
for _, kv := range log.Fields {
kvs = append(kvs, KeyValue{
Key: kv.Key,
Type: ValueType(strings.ToLower(kv.VType.String())),
Value: convertKeyValueType(kv),
})
kvs = append(kvs, convertKeyValue(kv))
}
out[i] = Log{
Timestamp: model.TimeAsEpochMicroseconds(log.Timestamp),
Expand All @@ -139,22 +128,10 @@ func (fd FromDomain) convertProcess(process *model.Process) Process {
}
}

func convertKeyValueType(kv model.KeyValue) string {
switch kv.VType {
case model.StringType:
return kv.VStr
case model.BoolType:
if kv.Bool() {
return "true"
}
return "false"
case model.Int64Type:
return strconv.FormatInt(kv.Int64(), 10)
case model.Float64Type:
return strconv.FormatFloat(kv.Float64(), 'g', 10, 64)
case model.BinaryType:
return hex.EncodeToString(kv.VBinary)
default:
return fmt.Sprintf("unknown type %d", kv.VType)
func convertKeyValue(kv model.KeyValue) KeyValue {
return KeyValue{
Key: kv.Key,
Type: ValueType(strings.ToLower(kv.VType.String())),
Value: kv.AsString(),
}
}
35 changes: 18 additions & 17 deletions plugin/storage/es/spanstore/dbmodel/from_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,43 +114,44 @@ func TestConvertKeyValueValue(t *testing.T) {
Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues
Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues
Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues `
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was supposed to be "Bender is Great!" string repeated :-)

key := "key"
tests := []struct {
kv model.KeyValue
expected string
expected KeyValue
}{
{
kv: model.KeyValue{VType: model.ValueType_BOOL, VBool: true},
expected: "true",
kv: model.Bool(key, true),
expected: KeyValue{Key: key, Value: "true", Type: "bool"},
},
{
kv: model.KeyValue{VType: model.ValueType_BOOL, VBool: false},
expected: "false",
kv: model.Bool(key, false),
expected: KeyValue{Key: key, Value: "false", Type: "bool"},
},
{
kv: model.KeyValue{VType: model.ValueType_INT64, VInt64: int64(1499)},
expected: "1499",
kv: model.Int64(key, int64(1499)),
expected: KeyValue{Key: key, Value: "1499", Type: "int64"},
},
{
kv: model.KeyValue{VType: model.ValueType_FLOAT64, VFloat64: float64(15.66)},
expected: "15.66",
kv: model.Float64(key, float64(15.66)),
expected: KeyValue{Key: key, Value: "15.66", Type: "float64"},
},
{
kv: model.KeyValue{VType: model.ValueType_STRING, VStr: longString},
expected: longString,
kv: model.String(key, longString),
expected: KeyValue{Key: key, Value: longString, Type: "string"},
},
{
kv: model.KeyValue{VType: model.ValueType_BINARY, VBinary: []byte(longString)},
expected: hex.EncodeToString([]byte(longString)),
kv: model.Binary(key, []byte(longString)),
expected: KeyValue{Key: key, Value: hex.EncodeToString([]byte(longString)), Type: "binary"},
},
{
kv: model.KeyValue{VType: 1500},
expected: "unknown type 1500",
kv: model.KeyValue{VType: 1500, Key: key},
expected: KeyValue{Key: key, Value: "unknown type 1500", Type: "1500"},
},
}

for _, test := range tests {
t.Run(test.expected, func(t *testing.T) {
actual := convertKeyValueType(test.kv)
t.Run(fmt.Sprintf("%s:%s", test.expected.Type, test.expected.Key), func(t *testing.T) {
actual := convertKeyValue(test.kv)
assert.Equal(t, test.expected, actual)
})
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/storage/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (m *Store) validTrace(trace *model.Trace, query *spanstore.TraceQueryParame

func findKeyValueMatch(kvs model.KeyValues, key, value string) (model.KeyValue, bool) {
for _, kv := range kvs {
if kv.Key == key && kv.AsString() == value {
if kv.Key == key && kv.AsStringLossy() == value {
pavolloffay marked this conversation as resolved.
Show resolved Hide resolved
return kv, true
}
}
Expand Down