From 529f54e84e260e79b1817a42b635b97ee358f32c Mon Sep 17 00:00:00 2001 From: John Date: Mon, 6 Dec 2021 05:27:51 -0800 Subject: [PATCH] Refactor grabbing service names from spans and resources Signed-off-by: John --- .../processor/filterspan/filterspan.go | 25 +++++++++++++------ .../processor/filterspan/filterspan_test.go | 13 +++++++--- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/internal/coreinternal/processor/filterspan/filterspan.go b/internal/coreinternal/processor/filterspan/filterspan.go index a1ad71596af0..3c283494e1c9 100644 --- a/internal/coreinternal/processor/filterspan/filterspan.go +++ b/internal/coreinternal/processor/filterspan/filterspan.go @@ -112,7 +112,10 @@ func (mp *propertiesMatcher) MatchSpan(span pdata.Span, resource pdata.Resource, // If a set of properties was not in the mp, all spans are considered to match on that property if mp.serviceFilters != nil { // Check resource and spans for service.name - serviceName := serviceNameForResource(resource, span) + serviceName, found := serviceNameForResource(resource) + if !found { + serviceName, found = serviceNameForSpan(span) + } if !mp.serviceFilters.Matches(serviceName) { return false } @@ -125,15 +128,21 @@ func (mp *propertiesMatcher) MatchSpan(span pdata.Span, resource pdata.Resource, return mp.PropertiesMatcher.Match(span.Attributes(), resource, library) } -// serviceNameForResource gets the service name for a specified Resource and its associated Span. -func serviceNameForResource(resource pdata.Resource, span pdata.Span) string { +// serviceNameForResource gets the service name for a specified Resource +func serviceNameForResource(resource pdata.Resource) (string, bool) { service, found := resource.Attributes().Get(conventions.AttributeServiceName) if !found { - service, found = span.Attributes().Get(conventions.AttributeServiceName) - if !found { - return "" - } + return "", found + } + return service.AsString(), found +} + +// serviceNameForSpan gets the service name for a span +func serviceNameForSpan(span pdata.Span) (string, bool) { + service, found := span.Attributes().Get(conventions.AttributeServiceName) + if !found { + return "", found } - return service.StringVal() + return service.AsString(), found } diff --git a/internal/coreinternal/processor/filterspan/filterspan_test.go b/internal/coreinternal/processor/filterspan/filterspan_test.go index 7a3826d319e2..cd281834b497 100644 --- a/internal/coreinternal/processor/filterspan/filterspan_test.go +++ b/internal/coreinternal/processor/filterspan/filterspan_test.go @@ -246,12 +246,17 @@ func TestSpan_Matching_True(t *testing.T) { func TestServiceNameForResource(t *testing.T) { td := testdata.GenerateTracesOneSpanNoResource() - require.Equal(t, serviceNameForResource(td.ResourceSpans().At(0).Resource()), "") - + name, found := serviceNameForResource(td.ResourceSpans().At(0).Resource()) + require.Equal(t, name, "") + require.False(t, found) td = testdata.GenerateTracesOneSpan() resource := td.ResourceSpans().At(0).Resource() - require.Equal(t, serviceNameForResource(resource), "") + name, found = serviceNameForResource(resource) + require.Equal(t, name, "") + require.False(t, found) resource.Attributes().InsertString(conventions.AttributeServiceName, "test-service") - require.Equal(t, serviceNameForResource(resource), "test-service") + name, found = serviceNameForResource(resource) + require.Equal(t, name, "test-service") + require.True(t, found) }