Skip to content

Commit 255c172

Browse files
authored
fix(otel): allow other data types for span data values (#784)
1 parent f1e97da commit 255c172

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

otel/span_processor.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ func getTraceParentContext(ctx context.Context) sentry.TraceParentContext {
122122

123123
func updateTransactionWithOtelData(transaction *sentry.Span, s otelSdkTrace.ReadOnlySpan) {
124124
// TODO(michi) This is crazy inefficient
125-
attributes := map[attribute.Key]string{}
126-
resource := map[attribute.Key]string{}
125+
attributes := map[attribute.Key]interface{}{}
126+
resource := map[attribute.Key]interface{}{}
127127

128128
for _, kv := range s.Attributes() {
129-
attributes[kv.Key] = kv.Value.Emit()
129+
attributes[kv.Key] = kv.Value.AsInterface()
130130
}
131131
for _, kv := range s.Resource().Attributes() {
132-
resource[kv.Key] = kv.Value.Emit()
132+
resource[kv.Key] = kv.Value.AsInterface()
133133
}
134134

135135
transaction.SetContext("otel", map[string]interface{}{
@@ -153,6 +153,6 @@ func updateSpanWithOtelData(span *sentry.Span, s otelSdkTrace.ReadOnlySpan) {
153153
span.Description = spanAttributes.Description
154154
span.SetData("otel.kind", s.SpanKind().String())
155155
for _, kv := range s.Attributes() {
156-
span.SetData(string(kv.Key), kv.Value.Emit())
156+
span.SetData(string(kv.Key), kv.Value.AsInterface())
157157
}
158158
}

otel/span_processor_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,10 @@ func TestOnEndWithTransaction(t *testing.T) {
257257
t,
258258
otelContextGot,
259259
map[string]interface{}{
260-
"attributes": map[attribute.Key]string{
260+
"attributes": map[attribute.Key]interface{}{
261261
"key1": "value1",
262262
},
263-
"resource": map[attribute.Key]string{
263+
"resource": map[attribute.Key]interface{}{
264264
"service.name": "test-otel",
265265
},
266266
},

tracing.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@ func (s *Span) SetTag(name, value string) {
226226
// SetData sets a data on the span. It is recommended to use SetData instead of
227227
// accessing the data map directly as SetData takes care of initializing the map
228228
// when necessary.
229-
func (s *Span) SetData(name, value string) {
229+
func (s *Span) SetData(name string, value interface{}) {
230+
if value == nil {
231+
return
232+
}
233+
230234
s.mu.Lock()
231235
defer s.mu.Unlock()
232236

tracing_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,11 @@ func TestSetData(t *testing.T) {
334334
})
335335
span := StartSpan(ctx, "Test Span")
336336
span.SetData("key", "value")
337-
338-
if (span.Data == nil) || (span.Data["key"] != "value") {
337+
span.SetData("key.nil", nil)
338+
span.SetData("key.number", 123)
339+
span.SetData("key.bool", true)
340+
span.SetData("key.slice", []string{"foo", "bar"})
341+
if (span.Data == nil) || (span.Data["key"] != "value") || (span.Data["key.number"] != 123) || (span.Data["key.bool"] != true) || !reflect.DeepEqual(span.Data["key.slice"], []string{"foo", "bar"}) {
339342
t.Fatalf("Data mismatch, got %v", span.Data)
340343
}
341344
}

0 commit comments

Comments
 (0)