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

Allow storing tags as object fields in ES - better kibana support #1018

Merged
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
Prev Previous commit
Next Next commit
Replace dot in query
Signed-off-by: Pavol Loffay <ploffay@redhat.com>
  • Loading branch information
pavolloffay committed Sep 7, 2018
commit 39259502ef491c99c8809015a2491160f2100afe
22 changes: 16 additions & 6 deletions plugin/storage/es/spanstore/dbmodel/to_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ 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)
Expand Down Expand Up @@ -69,7 +79,7 @@ func (td ToDomain) SpanToDomain(dbSpan *Span) (*model.Span, error) {
refs = model.MaybeAddParentSpanID(traceID, parentSpanID, refs)
}

fieldTags, err := convertTagFields(dbSpan.Tag, td.tagDotReplacement)
fieldTags, err := td.convertTagFields(dbSpan.Tag)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -134,11 +144,11 @@ func (td ToDomain) convertKeyValues(tags []KeyValue) ([]model.KeyValue, error) {
return retMe, nil
}

func convertTagFields(tagsMap map[string]interface{}, deDotChar string) ([]model.KeyValue, error) {
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 := convertTagField(k, v, deDotChar)
tag, err := td.convertTagField(k, v)
if err != nil {
return nil, err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

In theory, it can also be something else. How about adding a log statement for that case?

Copy link
Member Author

Choose a reason for hiding this comment

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

That should not really happen. In this case it's better to return an error.

Expand All @@ -148,8 +158,8 @@ func convertTagFields(tagsMap map[string]interface{}, deDotChar string) ([]model
return kvs, nil
}

func convertTagField(k string, v interface{}, deDotChar string) (model.KeyValue, error) {
dKey := strings.Replace(k, deDotChar, ".", -1)
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 {
Expand Down Expand Up @@ -231,7 +241,7 @@ func (td ToDomain) convertProcess(process Process) (*model.Process, error) {
if err != nil {
return nil, err
}
fieldTags, err := convertTagFields(process.Tag, td.tagDotReplacement)
fieldTags, err := td.convertTagFields(process.Tag)
if err != nil {
return nil, err
}
Expand Down
9 changes: 8 additions & 1 deletion plugin/storage/es/spanstore/dbmodel/to_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,18 @@ func TestTagsMap(t *testing.T) {
{fieldTags: map[string]interface{}{"binary": []byte("foo")}, expected: []model.KeyValue{model.Binary("binary", []byte("foo"))}},
{fieldTags: map[string]interface{}{"unsupported": struct{}{}}, err: fmt.Errorf("invalid tag type in %+v", struct{}{})},
}
converter := NewToDomain(":")
for i, test := range tests {
t.Run(fmt.Sprintf("%d, %s", i, test.fieldTags), func(t *testing.T) {
tags, err := convertTagFields(test.fieldTags, ":")
tags, err := converter.convertTagFields(test.fieldTags)
assert.Equal(t, test.expected, tags)
assert.Equal(t, test.err, err)
})
}
}

func TestDotReplacement(t *testing.T) {
converter := NewToDomain("#")
k := "foo.foo"
assert.Equal(t, k, converter.ReplaceDotReplacement(converter.ReplaceDot(k)))
}
10 changes: 5 additions & 5 deletions plugin/storage/es/spanstore/fixtures/query_01.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"bool":{
"must":{
"match":{
"tag.bat":{
"tag.bat@foo":{
"query":"spook"
}
}
Expand All @@ -16,7 +16,7 @@
"bool":{
"must":{
"match":{
"process.tag.bat":{
"process.tag.bat@foo":{
"query":"spook"
}
}
Expand All @@ -32,7 +32,7 @@
{
"match":{
"tags.key":{
"query":"bat"
"query":"bat.foo"
}
}
},
Expand All @@ -57,7 +57,7 @@
{
"match":{
"process.tags.key":{
"query":"bat"
"query":"bat.foo"
}
}
},
Expand All @@ -82,7 +82,7 @@
{
"match":{
"logs.fields.key":{
"query":"bat"
"query":"bat.foo"
}
}
},
Expand Down
3 changes: 1 addition & 2 deletions plugin/storage/es/spanstore/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -468,7 +467,7 @@ func (s *SpanReader) buildOperationNameQuery(operationName string) elastic.Query
func (s *SpanReader) buildTagQuery(k string, v string) elastic.Query {
objectTagListLen := len(objectTagFieldList)
queries := make([]elastic.Query, len(nestedTagFieldList)+objectTagListLen)
kd := strings.Replace(k, ".", ":", -1)
kd := s.spanConverter.ReplaceDot(k)
for i := range objectTagFieldList {
queries[i] = s.buildObjectQuery(objectTagFieldList[i], kd, v)
}
Expand Down
11 changes: 6 additions & 5 deletions plugin/storage/es/spanstore/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ func withSpanReader(fn func(r *spanReaderTest)) {
logger: logger,
logBuffer: logBuffer,
reader: newSpanReader(SpanReaderParams{
jpkrohling marked this conversation as resolved.
Show resolved Hide resolved
Client: client,
Logger: zap.NewNop(),
MaxLookback: 0,
IndexPrefix: "",
Client: client,
Logger: zap.NewNop(),
MaxLookback: 0,
IndexPrefix: "",
TagDotReplacement: "@",
}),
}
fn(r)
Expand Down Expand Up @@ -868,7 +869,7 @@ func TestSpanReader_buildTagQuery(t *testing.T) {
inStr, err := ioutil.ReadFile("fixtures/query_01.json")
require.NoError(t, err)
withSpanReader(func(r *spanReaderTest) {
tagQuery := r.reader.buildTagQuery("bat", "spook")
tagQuery := r.reader.buildTagQuery("bat.foo", "spook")
actual, err := tagQuery.Source()
require.NoError(t, err)

Expand Down