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

fix(otel): allow other data types for span data values #784

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 5 additions & 5 deletions otel/span_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ func getTraceParentContext(ctx context.Context) sentry.TraceParentContext {

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

for _, kv := range s.Attributes() {
attributes[kv.Key] = kv.Value.Emit()
attributes[kv.Key] = kv.Value.AsInterface()
}
for _, kv := range s.Resource().Attributes() {
resource[kv.Key] = kv.Value.Emit()
resource[kv.Key] = kv.Value.AsInterface()
}

transaction.SetContext("otel", map[string]interface{}{
Expand All @@ -153,6 +153,6 @@ func updateSpanWithOtelData(span *sentry.Span, s otelSdkTrace.ReadOnlySpan) {
span.Description = spanAttributes.Description
span.SetData("otel.kind", s.SpanKind().String())
for _, kv := range s.Attributes() {
span.SetData(string(kv.Key), kv.Value.Emit())
span.SetData(string(kv.Key), kv.Value.AsInterface())
}
}
4 changes: 2 additions & 2 deletions otel/span_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ func TestOnEndWithTransaction(t *testing.T) {
t,
otelContextGot,
map[string]interface{}{
"attributes": map[attribute.Key]string{
"attributes": map[attribute.Key]interface{}{
"key1": "value1",
},
"resource": map[attribute.Key]string{
"resource": map[attribute.Key]interface{}{
"service.name": "test-otel",
},
},
Expand Down
6 changes: 5 additions & 1 deletion tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ func (s *Span) SetTag(name, value string) {
// SetData sets a data on the span. It is recommended to use SetData instead of
// accessing the data map directly as SetData takes care of initializing the map
// when necessary.
func (s *Span) SetData(name, value string) {
func (s *Span) SetData(name string, value interface{}) {
if value == nil {
return
}

s.mu.Lock()
defer s.mu.Unlock()

Expand Down
7 changes: 5 additions & 2 deletions tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,11 @@ func TestSetData(t *testing.T) {
})
span := StartSpan(ctx, "Test Span")
span.SetData("key", "value")

if (span.Data == nil) || (span.Data["key"] != "value") {
span.SetData("key.nil", nil)
span.SetData("key.number", 123)
span.SetData("key.bool", true)
span.SetData("key.slice", []string{"foo", "bar"})
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"}) {
t.Fatalf("Data mismatch, got %v", span.Data)
}
}
Expand Down
Loading