Skip to content

Commit bf0966a

Browse files
authored
Fix resource attribute mutation bug (#907)
This commit fixes a bug when "service.name" attribute is removed from resource structure during translation to jaeger proto format
1 parent 5736800 commit bf0966a

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

translator/trace/jaeger/traces_to_jaegerproto.go

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,42 +117,60 @@ func resourceToJaegerProtoProcess(resource pdata.Resource) *model.Process {
117117
process := model.Process{}
118118
if serviceName, ok := attrs.Get(conventions.AttributeServiceName); ok {
119119
process.ServiceName = serviceName.StringVal()
120-
attrs.Delete(conventions.AttributeServiceName)
121120
}
122-
process.Tags = attributesToJaegerProtoTags(attrs)
121+
process.Tags = resourceAttributesToJaegerProtoTags(attrs)
123122
return &process
124123

125124
}
126125

127-
func attributesToJaegerProtoTags(attrs pdata.AttributeMap) []model.KeyValue {
126+
func resourceAttributesToJaegerProtoTags(attrs pdata.AttributeMap) []model.KeyValue {
128127
if attrs.Cap() == 0 {
129128
return nil
130129
}
131130

132131
tags := make([]model.KeyValue, 0, attrs.Cap())
133132
attrs.ForEach(func(key string, attr pdata.AttributeValue) {
134-
tag := model.KeyValue{Key: key}
135-
switch attr.Type() {
136-
case pdata.AttributeValueSTRING:
137-
// Jaeger-to-Internal maps binary tags to string attributes and encodes them as
138-
// base64 strings. Blindingly attempting to decode base64 seems too much.
139-
tag.VType = model.ValueType_STRING
140-
tag.VStr = attr.StringVal()
141-
case pdata.AttributeValueINT:
142-
tag.VType = model.ValueType_INT64
143-
tag.VInt64 = attr.IntVal()
144-
case pdata.AttributeValueBOOL:
145-
tag.VType = model.ValueType_BOOL
146-
tag.VBool = attr.BoolVal()
147-
case pdata.AttributeValueDOUBLE:
148-
tag.VType = model.ValueType_FLOAT64
149-
tag.VFloat64 = attr.DoubleVal()
133+
if key == conventions.AttributeServiceName {
134+
return
150135
}
151-
tags = append(tags, tag)
136+
tags = append(tags, attributeToJaegerProtoTag(key, attr))
152137
})
153138
return tags
154139
}
155140

141+
func attributesToJaegerProtoTags(attrs pdata.AttributeMap) []model.KeyValue {
142+
if attrs.Cap() == 0 {
143+
return nil
144+
}
145+
146+
tags := make([]model.KeyValue, 0, attrs.Cap())
147+
attrs.ForEach(func(key string, attr pdata.AttributeValue) {
148+
tags = append(tags, attributeToJaegerProtoTag(key, attr))
149+
})
150+
return tags
151+
}
152+
153+
func attributeToJaegerProtoTag(key string, attr pdata.AttributeValue) model.KeyValue {
154+
tag := model.KeyValue{Key: key}
155+
switch attr.Type() {
156+
case pdata.AttributeValueSTRING:
157+
// Jaeger-to-Internal maps binary tags to string attributes and encodes them as
158+
// base64 strings. Blindingly attempting to decode base64 seems too much.
159+
tag.VType = model.ValueType_STRING
160+
tag.VStr = attr.StringVal()
161+
case pdata.AttributeValueINT:
162+
tag.VType = model.ValueType_INT64
163+
tag.VInt64 = attr.IntVal()
164+
case pdata.AttributeValueBOOL:
165+
tag.VType = model.ValueType_BOOL
166+
tag.VBool = attr.BoolVal()
167+
case pdata.AttributeValueDOUBLE:
168+
tag.VType = model.ValueType_FLOAT64
169+
tag.VFloat64 = attr.DoubleVal()
170+
}
171+
return tag
172+
}
173+
156174
func spanToJaegerProto(span pdata.Span) (*model.Span, error) {
157175
if span.IsNil() {
158176
return nil, nil

translator/trace/jaeger/traces_to_jaegerproto_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/open-telemetry/opentelemetry-collector/consumer/pdata"
2626
"github.com/open-telemetry/opentelemetry-collector/internal/data/testdata"
27+
"github.com/open-telemetry/opentelemetry-collector/translator/conventions"
2728
tracetranslator "github.com/open-telemetry/opentelemetry-collector/translator/trace"
2829
)
2930

@@ -188,6 +189,7 @@ func TestAttributesToJaegerProtoTags(t *testing.T) {
188189
attributes.InsertInt("int-val", 123)
189190
attributes.InsertString("string-val", "abc")
190191
attributes.InsertDouble("double-val", 1.23)
192+
attributes.InsertString(conventions.AttributeServiceName, "service-name")
191193

192194
expected := []model.KeyValue{
193195
{
@@ -210,9 +212,21 @@ func TestAttributesToJaegerProtoTags(t *testing.T) {
210212
VType: model.ValueType_FLOAT64,
211213
VFloat64: 1.23,
212214
},
215+
{
216+
Key: conventions.AttributeServiceName,
217+
VType: model.ValueType_STRING,
218+
VStr: "service-name",
219+
},
213220
}
214221

215222
require.EqualValues(t, expected, attributesToJaegerProtoTags(attributes))
223+
224+
got := attributesToJaegerProtoTags(attributes)
225+
require.EqualValues(t, expected, got)
226+
227+
// The last item in expected ("service-name") must be skipped in resource tags translation
228+
got = resourceAttributesToJaegerProtoTags(attributes)
229+
require.EqualValues(t, expected[:4], got)
216230
}
217231

218232
func TestInternalTracesToJaegerProto(t *testing.T) {

0 commit comments

Comments
 (0)