Skip to content

Commit fe2840c

Browse files
committed
cleanup
Signed-off-by: Manik2708 <[email protected]>
1 parent 8d8f7e2 commit fe2840c

File tree

3 files changed

+65
-51
lines changed

3 files changed

+65
-51
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ func TestFromDbModel_Fixtures(t *testing.T) {
543543
unmarshaller := ptrace.JSONUnmarshaler{}
544544
expectedTd, err := unmarshaller.UnmarshalTraces(tracesData)
545545
require.NoError(t, err)
546-
spans := ToDBModel(expectedTd)
546+
spans := ToDBModel(expectedTd, false, nil, ".")
547547
assert.Len(t, spans, 1)
548548
testSpans(t, spansData, spans[0])
549549
actualTd, err := FromDBModel(spans)

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

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

99
import (
1010
"encoding/hex"
11+
"strings"
1112

1213
"go.opentelemetry.io/collector/pdata/pcommon"
1314
"go.opentelemetry.io/collector/pdata/ptrace"
@@ -29,7 +30,12 @@ const (
2930

3031
// ToDBModel translates internal trace data into the DB Spans.
3132
// Returns slice of translated DB Spans and error if translation failed.
32-
func ToDBModel(td ptrace.Traces) []dbmodel.Span {
33+
func ToDBModel(td ptrace.Traces, allTagsAsObject bool, tagKeysAsFields []string, tagDotReplacement string) []dbmodel.Span {
34+
tags := map[string]bool{}
35+
for _, k := range tagKeysAsFields {
36+
tags[k] = true
37+
}
38+
toDb := newToDBModel(allTagsAsObject, tags, tagDotReplacement)
3339
resourceSpans := td.ResourceSpans()
3440

3541
if resourceSpans.Len() == 0 {
@@ -39,7 +45,7 @@ func ToDBModel(td ptrace.Traces) []dbmodel.Span {
3945
batches := make([]dbmodel.Span, 0, resourceSpans.Len())
4046
for i := 0; i < resourceSpans.Len(); i++ {
4147
rs := resourceSpans.At(i)
42-
batch := resourceSpansToDbSpans(rs)
48+
batch := toDb.resourceSpansToDbSpans(rs)
4349
if batch != nil {
4450
batches = append(batches, batch...)
4551
}
@@ -48,31 +54,45 @@ func ToDBModel(td ptrace.Traces) []dbmodel.Span {
4854
return batches
4955
}
5056

51-
func resourceSpansToDbSpans(resourceSpans ptrace.ResourceSpans) []dbmodel.Span {
57+
type toDBModel struct {
58+
allTagsAsFields bool
59+
tagKeysAsFields map[string]bool
60+
tagDotReplacement string
61+
}
62+
63+
func newToDBModel(allTagsAsFields bool, tagKeysAsFields map[string]bool, tagDotReplacement string) *toDBModel {
64+
return &toDBModel{
65+
allTagsAsFields: allTagsAsFields,
66+
tagKeysAsFields: tagKeysAsFields,
67+
tagDotReplacement: tagDotReplacement,
68+
}
69+
}
70+
71+
func (t *toDBModel) resourceSpansToDbSpans(resourceSpans ptrace.ResourceSpans) []dbmodel.Span {
5272
resource := resourceSpans.Resource()
5373
scopeSpans := resourceSpans.ScopeSpans()
5474

5575
if scopeSpans.Len() == 0 {
5676
return []dbmodel.Span{}
5777
}
5878

59-
process := resourceToDbProcess(resource)
79+
process := t.resourceToDbProcess(resource)
6080

6181
// Approximate the number of the spans as the number of the spans in the first
6282
// instrumentation library info.
6383
dbSpans := make([]dbmodel.Span, 0, scopeSpans.At(0).Spans().Len())
6484

6585
for _, scopeSpan := range scopeSpans.All() {
6686
for _, span := range scopeSpan.Spans().All() {
67-
dbSpan := spanToDbSpan(span, scopeSpan.Scope(), process)
87+
dbSpan := t.spanToDbSpan(span, scopeSpan.Scope(), process)
6888
dbSpans = append(dbSpans, dbSpan)
6989
}
7090
}
7191

7292
return dbSpans
7393
}
7494

75-
func resourceToDbProcess(resource pcommon.Resource) dbmodel.Process {
95+
func (t *toDBModel) resourceToDbProcess(resource pcommon.Resource) dbmodel.Process {
7696
process := dbmodel.Process{}
7797
attrs := resource.Attributes()
7898
if attrs.Len() == 0 {
@@ -87,10 +107,31 @@ func resourceToDbProcess(resource pcommon.Resource) dbmodel.Process {
87107
}
88108
tags = append(tags, attributeToDbTag(key, attr))
89109
}
90-
process.Tags = tags
110+
tagSlice, tagMap := t.embedTagDotReplacement(tags)
111+
process.Tags = tagSlice
112+
process.Tag = tagMap
91113
return process
92114
}
93115

116+
func (t *toDBModel) embedTagDotReplacement(keyValues []dbmodel.KeyValue) ([]dbmodel.KeyValue, map[string]any) {
117+
var tagsMap map[string]any
118+
var kvs []dbmodel.KeyValue
119+
for _, kv := range keyValues {
120+
if kv.Type != dbmodel.BinaryType && (t.allTagsAsFields || t.tagKeysAsFields[kv.Key]) {
121+
if tagsMap == nil {
122+
tagsMap = map[string]any{}
123+
}
124+
tagsMap[strings.ReplaceAll(kv.Key, ".", t.tagDotReplacement)] = kv.Value
125+
} else {
126+
kvs = append(kvs, kv)
127+
}
128+
}
129+
if kvs == nil {
130+
kvs = make([]dbmodel.KeyValue, 0)
131+
}
132+
return kvs, tagsMap
133+
}
134+
94135
func appendTagsFromAttributes(dest []dbmodel.KeyValue, attrs pcommon.Map) []dbmodel.KeyValue {
95136
for key, attr := range attrs.All() {
96137
dest = append(dest, attributeToDbTag(key, attr))
@@ -123,10 +164,12 @@ func attributeToDbTag(key string, attr pcommon.Value) dbmodel.KeyValue {
123164
return tag
124165
}
125166

126-
func spanToDbSpan(span ptrace.Span, libraryTags pcommon.InstrumentationScope, process dbmodel.Process) dbmodel.Span {
167+
func (t *toDBModel) spanToDbSpan(span ptrace.Span, libraryTags pcommon.InstrumentationScope, process dbmodel.Process) dbmodel.Span {
127168
traceID := dbmodel.TraceID(span.TraceID().String())
128169
parentSpanID := dbmodel.SpanID(span.ParentSpanID().String())
129170
startTime := span.StartTimestamp().AsTime()
171+
tags := getDbSpanTags(span, libraryTags)
172+
tagSlice, tagMap := t.embedTagDotReplacement(tags)
130173
return dbmodel.Span{
131174
TraceID: traceID,
132175
SpanID: dbmodel.SpanID(span.SpanID().String()),
@@ -135,7 +178,8 @@ func spanToDbSpan(span ptrace.Span, libraryTags pcommon.InstrumentationScope, pr
135178
StartTime: model.TimeAsEpochMicroseconds(startTime),
136179
StartTimeMillis: model.TimeAsEpochMicroseconds(startTime) / 1000,
137180
Duration: model.DurationAsMicroseconds(span.EndTimestamp().AsTime().Sub(startTime)),
138-
Tags: getDbSpanTags(span, libraryTags),
181+
Tags: tagSlice,
182+
Tag: tagMap,
139183
Logs: spanEventsToDbSpanLogs(span.Events()),
140184
Process: process,
141185
Flags: span.Flags(),

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

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,21 @@ func TestGetTagFromStatusCode(t *testing.T) {
6161

6262
func TestEmptyAttributes(t *testing.T) {
6363
traces := ptrace.NewTraces()
64-
spans := traces.ResourceSpans().AppendEmpty()
65-
scopeSpans := spans.ScopeSpans().AppendEmpty()
66-
spanScope := scopeSpans.Scope()
67-
span := scopeSpans.Spans().AppendEmpty()
68-
modelSpan := spanToDbSpan(span, spanScope, dbmodel.Process{})
69-
assert.Empty(t, modelSpan.Tags)
64+
traces.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty()
65+
modelSpan := ToDBModel(traces, false, nil, ".")
66+
assert.Len(t, modelSpan, 1)
67+
assert.Empty(t, modelSpan[0].Process.Tags)
7068
}
7169

7270
func TestEmptyLinkRefs(t *testing.T) {
7371
traces := ptrace.NewTraces()
74-
spans := traces.ResourceSpans().AppendEmpty()
75-
scopeSpans := spans.ScopeSpans().AppendEmpty()
76-
spanScope := scopeSpans.Scope()
77-
span := scopeSpans.Spans().AppendEmpty()
72+
span := traces.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty()
7873
spanLink := span.Links().AppendEmpty()
7974
spanLink.Attributes().PutStr("testing-key", "testing-inputValue")
80-
modelSpan := spanToDbSpan(span, spanScope, dbmodel.Process{})
81-
assert.Len(t, modelSpan.References, 1)
82-
assert.Equal(t, dbmodel.FollowsFrom, modelSpan.References[0].RefType)
75+
modelSpan := ToDBModel(traces, false, nil, ".")
76+
assert.Len(t, modelSpan, 1)
77+
assert.Len(t, modelSpan[0].References, 1)
78+
assert.Equal(t, dbmodel.FollowsFrom, modelSpan[0].References[0].RefType)
8379
}
8480

8581
func TestGetTagFromStatusMsg(t *testing.T) {
@@ -95,32 +91,6 @@ func TestGetTagFromStatusMsg(t *testing.T) {
9591
}, got)
9692
}
9793

98-
func Test_resourceToDbProcess(t *testing.T) {
99-
traces := ptrace.NewTraces()
100-
resourceSpans := traces.ResourceSpans().AppendEmpty()
101-
resource := resourceSpans.Resource()
102-
resource.Attributes().PutStr(conventions.AttributeServiceName, "service")
103-
resource.Attributes().PutStr("foo", "bar")
104-
process := resourceToDbProcess(resource)
105-
assert.Equal(t, "service", process.ServiceName)
106-
expected := []dbmodel.KeyValue{
107-
{
108-
Key: "foo",
109-
Value: "bar",
110-
Type: dbmodel.StringType,
111-
},
112-
}
113-
assert.Equal(t, expected, process.Tags)
114-
}
115-
116-
func Test_resourceToDbProcess_WhenOnlyServiceNameIsPresent(t *testing.T) {
117-
traces := ptrace.NewTraces()
118-
spans := traces.ResourceSpans().AppendEmpty()
119-
spans.Resource().Attributes().PutStr(conventions.AttributeServiceName, "service")
120-
process := resourceToDbProcess(spans.Resource())
121-
assert.Equal(t, "service", process.ServiceName)
122-
}
123-
12494
func Test_appendTagsFromResourceAttributes_empty_attrs(t *testing.T) {
12595
traces := ptrace.NewTraces()
12696
emptyAttrs := traces.ResourceSpans().AppendEmpty().Resource().Attributes()
@@ -275,7 +245,7 @@ func TestToDbModel_Fixtures(t *testing.T) {
275245
td, err := FromDBModel([]dbmodel.Span{span})
276246
require.NoError(t, err)
277247
testTraces(t, tracesStr, td)
278-
spans := ToDBModel(td)
248+
spans := ToDBModel(td, false, nil, ".")
279249
assert.Len(t, spans, 1)
280250
testSpans(t, spansStr, spans[0])
281251
}
@@ -333,7 +303,7 @@ func BenchmarkInternalTracesToDbSpans(b *testing.B) {
333303

334304
b.ResetTimer()
335305
for n := 0; n < b.N; n++ {
336-
batches := ToDBModel(td)
306+
batches := ToDBModel(td, false, nil, ".")
337307
assert.NotEmpty(b, batches)
338308
}
339309
}

0 commit comments

Comments
 (0)