From 881c4ef625a34be04ff57f27f362145a43fc4933 Mon Sep 17 00:00:00 2001 From: Vishal Raj Date: Wed, 7 Aug 2024 16:59:23 +0100 Subject: [PATCH 1/2] Add enrichment for span.destination.service.resource --- enrichments/trace/config/config.go | 14 ++- .../trace/internal/elastic/attributes.go | 17 +-- enrichments/trace/internal/elastic/span.go | 40 +++++++ .../trace/internal/elastic/span_test.go | 108 ++++++++++-------- 4 files changed, 117 insertions(+), 62 deletions(-) diff --git a/enrichments/trace/config/config.go b/enrichments/trace/config/config.go index 2dd72d3..32a566a 100644 --- a/enrichments/trace/config/config.go +++ b/enrichments/trace/config/config.go @@ -43,9 +43,10 @@ type ElasticTransactionConfig struct { // ElasticSpanConfig configures the enrichment attributes for the spans // which are NOT identified as elastic transaction. type ElasticSpanConfig struct { - Name AttributeConfig `mapstructure:"name"` - EventOutcome AttributeConfig `mapstructure:"event_outcome"` - ServiceTarget AttributeConfig `mapstructure:"service_target"` + Name AttributeConfig `mapstructure:"name"` + EventOutcome AttributeConfig `mapstructure:"event_outcome"` + ServiceTarget AttributeConfig `mapstructure:"service_target"` + DestinationService AttributeConfig `mapstructure:"destination_service"` } // AttributeConfig is the configuration options for each attribute. @@ -68,9 +69,10 @@ func Enabled() Config { EventOutcome: AttributeConfig{Enabled: true}, }, Span: ElasticSpanConfig{ - Name: AttributeConfig{Enabled: true}, - EventOutcome: AttributeConfig{Enabled: true}, - ServiceTarget: AttributeConfig{Enabled: true}, + Name: AttributeConfig{Enabled: true}, + EventOutcome: AttributeConfig{Enabled: true}, + ServiceTarget: AttributeConfig{Enabled: true}, + DestinationService: AttributeConfig{Enabled: true}, }, } } diff --git a/enrichments/trace/internal/elastic/attributes.go b/enrichments/trace/internal/elastic/attributes.go index ee4db48..c604e3f 100644 --- a/enrichments/trace/internal/elastic/attributes.go +++ b/enrichments/trace/internal/elastic/attributes.go @@ -23,12 +23,13 @@ const ( AttributeAgentVersion = "agent.version" // span attributes - AttributeTransactionRoot = "transaction.root" - AttributeTransactionName = "transaction.name" - AttributeTransactionType = "transaction.type" - AttributeTransactionResult = "transaction.result" - AttributeSpanName = "span.name" - AttributeEventOutcome = "event.outcome" - AttributeServiceTargetType = "service.target.type" - AttributeServiceTargetName = "service.target.name" + AttributeTransactionRoot = "transaction.root" + AttributeTransactionName = "transaction.name" + AttributeTransactionType = "transaction.type" + AttributeTransactionResult = "transaction.result" + AttributeSpanName = "span.name" + AttributeEventOutcome = "event.outcome" + AttributeServiceTargetType = "service.target.type" + AttributeServiceTargetName = "service.target.name" + AttributeSpanDestinationResource = "span.destination.service.resource" ) diff --git a/enrichments/trace/internal/elastic/span.go b/enrichments/trace/internal/elastic/span.go index e90c98c..efaea9a 100644 --- a/enrichments/trace/internal/elastic/span.go +++ b/enrichments/trace/internal/elastic/span.go @@ -191,6 +191,9 @@ func (s *spanEnrichmentContext) enrichSpan( if cfg.ServiceTarget.Enabled { s.setServiceTarget(span) } + if cfg.DestinationService.Enabled { + s.setDestinationService(span) + } } // normalizeAttributes sets any dependent attributes that @@ -300,6 +303,40 @@ func (s *spanEnrichmentContext) setServiceTarget(span ptrace.Span) { } } +func (s *spanEnrichmentContext) setDestinationService(span ptrace.Span) { + var destnResource string + if s.peerService != "" { + destnResource = s.peerService + } + + switch { + case s.isDB: + if destnResource == "" && s.dbType != "" { + destnResource = s.dbType + } + case s.isMessaging: + if destnResource == "" && s.messagingSystem != "" { + destnResource = s.messagingSystem + } + // For parity with apm-data, destn resource does not handle + // temporary destination flag. However, it is handled by + // service.target fields and we might want to do the same here. + if destnResource != "" && s.messagingDestinationName != "" { + destnResource += "/" + s.messagingDestinationName + } + case s.isRPC, s.isHTTP: + if destnResource == "" { + if res := getHostPort(s.urlFull, s.urlDomain, s.urlPort); res != "" { + destnResource = res + } + } + } + + if destnResource != "" { + span.Attributes().PutStr(AttributeSpanDestinationResource, destnResource) + } +} + func isTraceRoot(span ptrace.Span) bool { return span.ParentSpanID().IsEmpty() } @@ -319,6 +356,9 @@ func isElasticTransaction(span ptrace.Span) bool { return false } +// getHostPort derives the host:port value from url.* attributes. Unlike +// apm-data, the current code does NOT fallback to net.* or http.* +// attritubtes as most of these are now deprecated. func getHostPort(urlFull *url.URL, urlDomain string, urlPort int64) string { if urlFull != nil { return urlFull.Host diff --git a/enrichments/trace/internal/elastic/span_test.go b/enrichments/trace/internal/elastic/span_test.go index 412ea76..8c3848e 100644 --- a/enrichments/trace/internal/elastic/span_test.go +++ b/enrichments/trace/internal/elastic/span_test.go @@ -265,10 +265,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetName: "testsvc", - AttributeServiceTargetType: "", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetName: "testsvc", + AttributeServiceTargetType: "", + AttributeSpanDestinationResource: "testsvc", }, }, { @@ -285,10 +286,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "http", - AttributeServiceTargetName: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "http", + AttributeServiceTargetName: "testsvc", + AttributeSpanDestinationResource: "testsvc", }, }, { @@ -311,10 +313,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "http", - AttributeServiceTargetName: "www.foo.bar:443", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "http", + AttributeServiceTargetName: "www.foo.bar:443", + AttributeSpanDestinationResource: "testsvc", }, }, { @@ -335,10 +338,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "http", - AttributeServiceTargetName: "www.foo.bar:443", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "http", + AttributeServiceTargetName: "www.foo.bar:443", + AttributeSpanDestinationResource: "testsvc", }, }, { @@ -355,10 +359,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "grpc", - AttributeServiceTargetName: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "grpc", + AttributeServiceTargetName: "testsvc", + AttributeSpanDestinationResource: "testsvc", }, }, { @@ -372,10 +377,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "xmlrpc", - AttributeServiceTargetName: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "xmlrpc", + AttributeServiceTargetName: "testsvc", + AttributeSpanDestinationResource: "testsvc", }, }, { @@ -391,10 +397,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "external", - AttributeServiceTargetName: "service.Test", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "external", + AttributeServiceTargetName: "service.Test", + AttributeSpanDestinationResource: "testsvc", }, }, { @@ -408,10 +415,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "kafka", - AttributeServiceTargetName: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "kafka", + AttributeServiceTargetName: "testsvc", + AttributeSpanDestinationResource: "testsvc", }, }, { @@ -425,10 +433,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "messaging", - AttributeServiceTargetName: "t1", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "messaging", + AttributeServiceTargetName: "t1", + AttributeSpanDestinationResource: "testsvc/t1", }, }, { @@ -443,10 +452,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "messaging", - AttributeServiceTargetName: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "messaging", + AttributeServiceTargetName: "testsvc", + AttributeSpanDestinationResource: "testsvc/t1", }, }, { @@ -467,10 +477,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "elasticsearch", - AttributeServiceTargetName: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "elasticsearch", + AttributeServiceTargetName: "testsvc", + AttributeSpanDestinationResource: "testsvc", }, }, { @@ -496,10 +507,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "cassandra", - AttributeServiceTargetName: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "cassandra", + AttributeServiceTargetName: "testsvc", + AttributeSpanDestinationResource: "testsvc", }, }, } { From a5b6dd5e78b247897feb514d7f092a2ffbcec660 Mon Sep 17 00:00:00 2001 From: Vishal Raj Date: Fri, 9 Aug 2024 12:23:35 +0100 Subject: [PATCH 2/2] minor fixes: address review comments --- .../trace/internal/elastic/attributes.go | 18 +-- enrichments/trace/internal/elastic/span.go | 4 +- .../trace/internal/elastic/span_test.go | 120 +++++++++--------- 3 files changed, 71 insertions(+), 71 deletions(-) diff --git a/enrichments/trace/internal/elastic/attributes.go b/enrichments/trace/internal/elastic/attributes.go index 19a7e7c..890df26 100644 --- a/enrichments/trace/internal/elastic/attributes.go +++ b/enrichments/trace/internal/elastic/attributes.go @@ -27,13 +27,13 @@ const ( AttributeServiceFrameworkVersion = "service.framework.version" // span attributes - AttributeTransactionRoot = "transaction.root" - AttributeTransactionName = "transaction.name" - AttributeTransactionType = "transaction.type" - AttributeTransactionResult = "transaction.result" - AttributeSpanName = "span.name" - AttributeEventOutcome = "event.outcome" - AttributeServiceTargetType = "service.target.type" - AttributeServiceTargetName = "service.target.name" - AttributeSpanDestinationResource = "span.destination.service.resource" + AttributeTransactionRoot = "transaction.root" + AttributeTransactionName = "transaction.name" + AttributeTransactionType = "transaction.type" + AttributeTransactionResult = "transaction.result" + AttributeSpanName = "span.name" + AttributeEventOutcome = "event.outcome" + AttributeServiceTargetType = "service.target.type" + AttributeServiceTargetName = "service.target.name" + AttributeSpanDestinationServiceResource = "span.destination.service.resource" ) diff --git a/enrichments/trace/internal/elastic/span.go b/enrichments/trace/internal/elastic/span.go index efaea9a..efdaea8 100644 --- a/enrichments/trace/internal/elastic/span.go +++ b/enrichments/trace/internal/elastic/span.go @@ -333,7 +333,7 @@ func (s *spanEnrichmentContext) setDestinationService(span ptrace.Span) { } if destnResource != "" { - span.Attributes().PutStr(AttributeSpanDestinationResource, destnResource) + span.Attributes().PutStr(AttributeSpanDestinationServiceResource, destnResource) } } @@ -358,7 +358,7 @@ func isElasticTransaction(span ptrace.Span) bool { // getHostPort derives the host:port value from url.* attributes. Unlike // apm-data, the current code does NOT fallback to net.* or http.* -// attritubtes as most of these are now deprecated. +// attributes as most of these are now deprecated. func getHostPort(urlFull *url.URL, urlDomain string, urlPort int64) string { if urlFull != nil { return urlFull.Host diff --git a/enrichments/trace/internal/elastic/span_test.go b/enrichments/trace/internal/elastic/span_test.go index 8c3848e..27fecd9 100644 --- a/enrichments/trace/internal/elastic/span_test.go +++ b/enrichments/trace/internal/elastic/span_test.go @@ -265,11 +265,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetName: "testsvc", - AttributeServiceTargetType: "", - AttributeSpanDestinationResource: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetName: "testsvc", + AttributeServiceTargetType: "", + AttributeSpanDestinationServiceResource: "testsvc", }, }, { @@ -286,11 +286,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "http", - AttributeServiceTargetName: "testsvc", - AttributeSpanDestinationResource: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "http", + AttributeServiceTargetName: "testsvc", + AttributeSpanDestinationServiceResource: "testsvc", }, }, { @@ -313,11 +313,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "http", - AttributeServiceTargetName: "www.foo.bar:443", - AttributeSpanDestinationResource: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "http", + AttributeServiceTargetName: "www.foo.bar:443", + AttributeSpanDestinationServiceResource: "testsvc", }, }, { @@ -338,11 +338,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "http", - AttributeServiceTargetName: "www.foo.bar:443", - AttributeSpanDestinationResource: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "http", + AttributeServiceTargetName: "www.foo.bar:443", + AttributeSpanDestinationServiceResource: "testsvc", }, }, { @@ -359,11 +359,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "grpc", - AttributeServiceTargetName: "testsvc", - AttributeSpanDestinationResource: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "grpc", + AttributeServiceTargetName: "testsvc", + AttributeSpanDestinationServiceResource: "testsvc", }, }, { @@ -377,11 +377,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "xmlrpc", - AttributeServiceTargetName: "testsvc", - AttributeSpanDestinationResource: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "xmlrpc", + AttributeServiceTargetName: "testsvc", + AttributeSpanDestinationServiceResource: "testsvc", }, }, { @@ -397,11 +397,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "external", - AttributeServiceTargetName: "service.Test", - AttributeSpanDestinationResource: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "external", + AttributeServiceTargetName: "service.Test", + AttributeSpanDestinationServiceResource: "testsvc", }, }, { @@ -415,11 +415,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "kafka", - AttributeServiceTargetName: "testsvc", - AttributeSpanDestinationResource: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "kafka", + AttributeServiceTargetName: "testsvc", + AttributeSpanDestinationServiceResource: "testsvc", }, }, { @@ -433,11 +433,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "messaging", - AttributeServiceTargetName: "t1", - AttributeSpanDestinationResource: "testsvc/t1", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "messaging", + AttributeServiceTargetName: "t1", + AttributeSpanDestinationServiceResource: "testsvc/t1", }, }, { @@ -452,11 +452,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "messaging", - AttributeServiceTargetName: "testsvc", - AttributeSpanDestinationResource: "testsvc/t1", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "messaging", + AttributeServiceTargetName: "testsvc", + AttributeSpanDestinationServiceResource: "testsvc/t1", }, }, { @@ -477,11 +477,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "elasticsearch", - AttributeServiceTargetName: "testsvc", - AttributeSpanDestinationResource: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "elasticsearch", + AttributeServiceTargetName: "testsvc", + AttributeSpanDestinationServiceResource: "testsvc", }, }, { @@ -507,11 +507,11 @@ func TestElasticSpanEnrich(t *testing.T) { }(), config: config.Enabled().Span, enrichedAttrs: map[string]any{ - AttributeSpanName: "testspan", - AttributeEventOutcome: "success", - AttributeServiceTargetType: "cassandra", - AttributeServiceTargetName: "testsvc", - AttributeSpanDestinationResource: "testsvc", + AttributeSpanName: "testspan", + AttributeEventOutcome: "success", + AttributeServiceTargetType: "cassandra", + AttributeServiceTargetName: "testsvc", + AttributeSpanDestinationServiceResource: "testsvc", }, }, } {