Skip to content

Commit

Permalink
Add txn root, txn name and span name attributes for trace enrichment (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lahsivjar authored Jul 29, 2024
1 parent 34105fb commit cadd9f7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
6 changes: 6 additions & 0 deletions enrichments/trace/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Config struct {
// ElasticTransactionConfig configures the enrichment attributes for the
// spans which are identified as elastic transaction.
type ElasticTransactionConfig struct {
Root AttributeConfig `mapstructure:"root"`
Name AttributeConfig `mapstructure:"name"`
Type AttributeConfig `mapstructure:"type"`
Result AttributeConfig `mapstructure:"result"`
EventOutcome AttributeConfig `mapstructure:"event_outcome"`
Expand All @@ -34,6 +36,7 @@ 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"`
}
Expand All @@ -47,11 +50,14 @@ type AttributeConfig struct {
func Enabled() Config {
return Config{
Transaction: ElasticTransactionConfig{
Root: AttributeConfig{Enabled: true},
Name: AttributeConfig{Enabled: true},
Type: AttributeConfig{Enabled: true},
Result: AttributeConfig{Enabled: true},
EventOutcome: AttributeConfig{Enabled: true},
},
Span: ElasticSpanConfig{
Name: AttributeConfig{Enabled: true},
EventOutcome: AttributeConfig{Enabled: true},
ServiceTarget: AttributeConfig{Enabled: true},
},
Expand Down
3 changes: 3 additions & 0 deletions enrichments/trace/internal/elastic/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
package elastic

const (
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"
Expand Down
9 changes: 9 additions & 0 deletions enrichments/trace/internal/elastic/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ func (s *spanEnrichmentContext) enrichTransaction(
span ptrace.Span,
cfg config.ElasticTransactionConfig,
) {
if cfg.Root.Enabled {
span.Attributes().PutBool(AttributeTransactionRoot, true)
}
if cfg.Name.Enabled {
span.Attributes().PutStr(AttributeTransactionName, span.Name())
}
if cfg.Type.Enabled {
s.setTxnType(span)
}
Expand All @@ -176,6 +182,9 @@ func (s *spanEnrichmentContext) enrichSpan(
span ptrace.Span,
cfg config.ElasticSpanConfig,
) {
if cfg.Name.Enabled {
span.Attributes().PutStr(AttributeSpanName, span.Name())
}
if cfg.EventOutcome.Enabled {
s.setEventOutcome(span)
}
Expand Down
51 changes: 51 additions & 0 deletions enrichments/trace/internal/elastic/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func TestElasticTransactionEnrich(t *testing.T) {
input: ptrace.NewSpan(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTransactionRoot: true,
AttributeTransactionName: "",
AttributeEventOutcome: "success",
AttributeTransactionResult: "Success",
AttributeTransactionType: "unknown",
Expand All @@ -57,11 +59,14 @@ func TestElasticTransactionEnrich(t *testing.T) {
name: "http_status_ok",
input: func() ptrace.Span {
span := ptrace.NewSpan()
span.SetName("testtxn")
span.Attributes().PutInt(semconv.AttributeHTTPStatusCode, http.StatusOK)
return span
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTransactionRoot: true,
AttributeTransactionName: "testtxn",
AttributeEventOutcome: "success",
AttributeTransactionResult: "HTTP 2xx",
AttributeTransactionType: "request",
Expand All @@ -71,6 +76,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
name: "http_status_1xx",
input: func() ptrace.Span {
span := ptrace.NewSpan()
span.SetName("testtxn")
// attributes should be preferred over span status for txn result
span.Status().SetCode(ptrace.StatusCodeOk)
span.Attributes().PutInt(
Expand All @@ -81,6 +87,8 @@ func TestElasticTransactionEnrich(t *testing.T) {
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTransactionRoot: true,
AttributeTransactionName: "testtxn",
AttributeEventOutcome: "success",
AttributeTransactionResult: "HTTP 1xx",
AttributeTransactionType: "request",
Expand All @@ -90,6 +98,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
name: "http_status_5xx",
input: func() ptrace.Span {
span := ptrace.NewSpan()
span.SetName("testtxn")
// span status code should take precedence over http status attributes
// for setting event.outcome
span.Status().SetCode(ptrace.StatusCodeOk)
Expand All @@ -99,6 +108,8 @@ func TestElasticTransactionEnrich(t *testing.T) {
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTransactionRoot: true,
AttributeTransactionName: "testtxn",
AttributeEventOutcome: "success",
AttributeTransactionResult: "HTTP 5xx",
AttributeTransactionType: "request",
Expand All @@ -108,6 +119,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
name: "grpc_status_ok",
input: func() ptrace.Span {
span := ptrace.NewSpan()
span.SetName("testtxn")
// attributes should be preferred over span status for txn result
span.Status().SetCode(ptrace.StatusCodeOk)
span.Attributes().PutInt(
Expand All @@ -118,6 +130,8 @@ func TestElasticTransactionEnrich(t *testing.T) {
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTransactionRoot: true,
AttributeTransactionName: "testtxn",
AttributeEventOutcome: "success",
AttributeTransactionResult: "OK",
AttributeTransactionType: "request",
Expand All @@ -127,6 +141,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
name: "grpc_status_internal_error",
input: func() ptrace.Span {
span := ptrace.NewSpan()
span.SetName("testtxn")
// attributes should be preferred over span status for txn result
span.Status().SetCode(ptrace.StatusCodeOk)
span.Attributes().PutInt(
Expand All @@ -137,6 +152,8 @@ func TestElasticTransactionEnrich(t *testing.T) {
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTransactionRoot: true,
AttributeTransactionName: "testtxn",
AttributeEventOutcome: "success",
AttributeTransactionResult: "Internal",
AttributeTransactionType: "request",
Expand All @@ -146,11 +163,14 @@ func TestElasticTransactionEnrich(t *testing.T) {
name: "span_status_ok",
input: func() ptrace.Span {
span := ptrace.NewSpan()
span.SetName("testtxn")
span.Status().SetCode(ptrace.StatusCodeOk)
return span
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTransactionRoot: true,
AttributeTransactionName: "testtxn",
AttributeEventOutcome: "success",
AttributeTransactionResult: "Success",
AttributeTransactionType: "unknown",
Expand All @@ -160,11 +180,14 @@ func TestElasticTransactionEnrich(t *testing.T) {
name: "span_status_error",
input: func() ptrace.Span {
span := ptrace.NewSpan()
span.SetName("testtxn")
span.Status().SetCode(ptrace.StatusCodeError)
return span
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTransactionRoot: true,
AttributeTransactionName: "testtxn",
AttributeEventOutcome: "failure",
AttributeTransactionResult: "Error",
AttributeTransactionType: "unknown",
Expand All @@ -174,11 +197,14 @@ func TestElasticTransactionEnrich(t *testing.T) {
name: "messaging_type_kafka",
input: func() ptrace.Span {
span := ptrace.NewSpan()
span.SetName("testtxn")
span.Attributes().PutStr(semconv.AttributeMessagingSystem, "kafka")
return span
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTransactionRoot: true,
AttributeTransactionName: "testtxn",
AttributeEventOutcome: "success",
AttributeTransactionResult: "Success",
AttributeTransactionType: "messaging",
Expand Down Expand Up @@ -220,6 +246,7 @@ func TestElasticSpanEnrich(t *testing.T) {
input: getElasticSpan(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeSpanName: "",
AttributeEventOutcome: "success",
},
},
Expand All @@ -232,11 +259,13 @@ func TestElasticSpanEnrich(t *testing.T) {
name: "peer_service",
input: func() ptrace.Span {
span := getElasticSpan()
span.SetName("testspan")
span.Attributes().PutStr(semconv.AttributePeerService, "testsvc")
return span
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeEventOutcome: "success",
AttributeServiceTargetName: "testsvc",
},
Expand All @@ -245,6 +274,7 @@ func TestElasticSpanEnrich(t *testing.T) {
name: "http_span_basic",
input: func() ptrace.Span {
span := getElasticSpan()
span.SetName("testspan")
span.Attributes().PutStr(semconv.AttributePeerService, "testsvc")
span.Attributes().PutInt(
semconv.AttributeHTTPResponseStatusCode,
Expand All @@ -254,6 +284,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeEventOutcome: "success",
AttributeServiceTargetType: "http",
AttributeServiceTargetName: "testsvc",
Expand All @@ -263,6 +294,7 @@ func TestElasticSpanEnrich(t *testing.T) {
name: "http_span_full_url",
input: func() ptrace.Span {
span := getElasticSpan()
span.SetName("testspan")
// peer.service should be ignored if more specific deductions
// can be made about the service target.
span.Attributes().PutStr(semconv.AttributePeerService, "testsvc")
Expand All @@ -278,6 +310,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeEventOutcome: "success",
AttributeServiceTargetType: "http",
AttributeServiceTargetName: "www.foo.bar:443",
Expand All @@ -287,6 +320,7 @@ func TestElasticSpanEnrich(t *testing.T) {
name: "http_span_no_full_url",
input: func() ptrace.Span {
span := getElasticSpan()
span.SetName("testspan")
// peer.service should be ignored if more specific deductions
// can be made about the service target.
span.Attributes().PutStr(semconv.AttributePeerService, "testsvc")
Expand All @@ -300,6 +334,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeEventOutcome: "success",
AttributeServiceTargetType: "http",
AttributeServiceTargetName: "www.foo.bar:443",
Expand All @@ -309,6 +344,7 @@ func TestElasticSpanEnrich(t *testing.T) {
name: "rpc_span_grpc",
input: func() ptrace.Span {
span := getElasticSpan()
span.SetName("testspan")
span.Attributes().PutStr(semconv.AttributePeerService, "testsvc")
span.Attributes().PutInt(
semconv.AttributeRPCGRPCStatusCode,
Expand All @@ -318,6 +354,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeEventOutcome: "success",
AttributeServiceTargetType: "grpc",
AttributeServiceTargetName: "testsvc",
Expand All @@ -327,12 +364,14 @@ func TestElasticSpanEnrich(t *testing.T) {
name: "rpc_span_system",
input: func() ptrace.Span {
span := getElasticSpan()
span.SetName("testspan")
span.Attributes().PutStr(semconv.AttributePeerService, "testsvc")
span.Attributes().PutStr(semconv.AttributeRPCSystem, "xmlrpc")
return span
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeEventOutcome: "success",
AttributeServiceTargetType: "xmlrpc",
AttributeServiceTargetName: "testsvc",
Expand All @@ -342,6 +381,7 @@ func TestElasticSpanEnrich(t *testing.T) {
name: "rpc_span_service",
input: func() ptrace.Span {
span := getElasticSpan()
span.SetName("testspan")
// peer.service should be ignored if more specific deductions
// can be made about the service target.
span.Attributes().PutStr(semconv.AttributePeerService, "testsvc")
Expand All @@ -350,6 +390,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeEventOutcome: "success",
AttributeServiceTargetType: "external",
AttributeServiceTargetName: "service.Test",
Expand All @@ -359,12 +400,14 @@ func TestElasticSpanEnrich(t *testing.T) {
name: "messaging_basic",
input: func() ptrace.Span {
span := getElasticSpan()
span.SetName("testspan")
span.Attributes().PutStr(semconv.AttributePeerService, "testsvc")
span.Attributes().PutStr(semconv.AttributeMessagingSystem, "kafka")
return span
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeEventOutcome: "success",
AttributeServiceTargetType: "kafka",
AttributeServiceTargetName: "testsvc",
Expand All @@ -374,12 +417,14 @@ func TestElasticSpanEnrich(t *testing.T) {
name: "messaging_destination",
input: func() ptrace.Span {
span := getElasticSpan()
span.SetName("testspan")
span.Attributes().PutStr(semconv.AttributePeerService, "testsvc")
span.Attributes().PutStr(semconv.AttributeMessagingDestinationName, "t1")
return span
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeEventOutcome: "success",
AttributeServiceTargetType: "messaging",
AttributeServiceTargetName: "t1",
Expand All @@ -389,13 +434,15 @@ func TestElasticSpanEnrich(t *testing.T) {
name: "messaging_temp_destination",
input: func() ptrace.Span {
span := getElasticSpan()
span.SetName("testspan")
span.Attributes().PutStr(semconv.AttributePeerService, "testsvc")
span.Attributes().PutBool(semconv.AttributeMessagingDestinationTemporary, true)
span.Attributes().PutStr(semconv.AttributeMessagingDestinationName, "t1")
return span
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeEventOutcome: "success",
AttributeServiceTargetType: "messaging",
AttributeServiceTargetName: "testsvc",
Expand All @@ -405,6 +452,7 @@ func TestElasticSpanEnrich(t *testing.T) {
name: "db_over_http",
input: func() ptrace.Span {
span := getElasticSpan()
span.SetName("testspan")
span.Attributes().PutStr(semconv.AttributePeerService, "testsvc")
span.Attributes().PutStr(
semconv.AttributeURLFull,
Expand All @@ -418,6 +466,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeEventOutcome: "success",
AttributeServiceTargetType: "elasticsearch",
AttributeServiceTargetName: "testsvc",
Expand All @@ -427,6 +476,7 @@ func TestElasticSpanEnrich(t *testing.T) {
name: "db_over_rpc",
input: func() ptrace.Span {
span := getElasticSpan()
span.SetName("testspan")
span.Attributes().PutStr(semconv.AttributePeerService, "testsvc")
span.Attributes().PutStr(
semconv.AttributeRPCSystem,
Expand All @@ -445,6 +495,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeEventOutcome: "success",
AttributeServiceTargetType: "cassandra",
AttributeServiceTargetName: "testsvc",
Expand Down

0 comments on commit cadd9f7

Please sign in to comment.