Skip to content

Commit

Permalink
Refactor grabbing service names from spans and resources
Browse files Browse the repository at this point in the history
Signed-off-by: John <dorman@overlooked.us>
  • Loading branch information
boostchicken authored and jpkrohling committed Jan 18, 2022
1 parent 3d1ecd7 commit 529f54e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
25 changes: 17 additions & 8 deletions internal/coreinternal/processor/filterspan/filterspan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 "<nil-service-name>"
}
return "<nil-service-name>", 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 "<nil-service-name>", found
}

return service.StringVal()
return service.AsString(), found
}
13 changes: 9 additions & 4 deletions internal/coreinternal/processor/filterspan/filterspan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()), "<nil-service-name>")

name, found := serviceNameForResource(td.ResourceSpans().At(0).Resource())
require.Equal(t, name, "<nil-service-name>")
require.False(t, found)
td = testdata.GenerateTracesOneSpan()
resource := td.ResourceSpans().At(0).Resource()
require.Equal(t, serviceNameForResource(resource), "<nil-service-name>")
name, found = serviceNameForResource(resource)
require.Equal(t, name, "<nil-service-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)
}

0 comments on commit 529f54e

Please sign in to comment.