From c9f15267ebf2a378179ab67aa38f747998ed2169 Mon Sep 17 00:00:00 2001 From: Oscar Reyes Date: Fri, 15 Dec 2023 10:05:16 -0600 Subject: [PATCH] fix(server): Return type for browser spans (#3443) * fix(server): Return type for browser spans * adding test * adding test * More tests and fixing bugs --- server/traces/trace_entities.go | 10 ++++++++++ server/traces/traces_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/server/traces/trace_entities.go b/server/traces/trace_entities.go index 1ef1a4bb27..227d851074 100644 --- a/server/traces/trace_entities.go +++ b/server/traces/trace_entities.go @@ -130,7 +130,16 @@ func getRootSpan(allRoots []*Span) *Span { return root } +// TODO: this is temp while we decide what to do with browser spans and how to handle them +func isBrowserSpan(attrs Attributes) bool { + return attrs.Get("event_type") != "" || attrs.Get(TracetestMetadataFieldName) == "documentLoad" +} + func spanType(attrs Attributes) string { + if isBrowserSpan(attrs) { + return "general" + } + // based on https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/trace/semantic_conventions // using the first required attribute for each type for key := range attrs.Values() { @@ -149,6 +158,7 @@ func spanType(attrs Attributes) string { return "exception" } } + return "general" } diff --git a/server/traces/traces_test.go b/server/traces/traces_test.go index 555400e5e4..fc55df5bee 100644 --- a/server/traces/traces_test.go +++ b/server/traces/traces_test.go @@ -548,6 +548,36 @@ func TestUnmarshalLargeTrace(t *testing.T) { assert.Greater(t, len(trace.Flat), 0) } +func TestBrowserSpan(t *testing.T) { + span := newSpan("click") + span.Attributes = attributesFromMap(map[string]string{ + "event_type": "click", + "http.url": "http://localhost:1663", + }) + + trace := traces.NewTrace("trace", []traces.Span{span}) + + assert.Equal(t, trace.Spans()[0].Attributes.Get(traces.TracetestMetadataFieldType), "general") + + span2 := newSpan("documentLoad") + span2.Attributes = attributesFromMap(map[string]string{ + "http.url": "http://localhost:1663", + }) + + trace2 := traces.NewTrace("trace", []traces.Span{span2}) + + assert.Equal(t, trace2.Spans()[0].Attributes.Get(traces.TracetestMetadataFieldType), "general") + + span3 := newSpan("GET /api/v1/trace") + span3.Attributes = attributesFromMap(map[string]string{ + "http.url": "http://localhost:1663", + }) + + trace3 := traces.NewTrace("trace", []traces.Span{span3}) + + assert.Equal(t, trace3.Spans()[0].Attributes.Get(traces.TracetestMetadataFieldType), "http") +} + func attributesFromMap(input map[string]string) traces.Attributes { attributes := traces.NewAttributes() for key, value := range input {