Skip to content

Commit

Permalink
Add span.{type, subtype} enrichment (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahsivjar authored Aug 13, 2024
1 parent 38bc56e commit 25c1f83
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
2 changes: 2 additions & 0 deletions enrichments/trace/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type ElasticTransactionConfig struct {
type ElasticSpanConfig struct {
Name AttributeConfig `mapstructure:"name"`
ProcessorEvent AttributeConfig `mapstructure:"processor_event"`
TypeSubtype AttributeConfig `mapstructure:"type_subtype"`
DurationUs AttributeConfig `mapstructure:"duration_us"`
EventOutcome AttributeConfig `mapstructure:"event_outcome"`
ServiceTarget AttributeConfig `mapstructure:"service_target"`
Expand Down Expand Up @@ -90,6 +91,7 @@ func Enabled() Config {
Span: ElasticSpanConfig{
Name: AttributeConfig{Enabled: true},
ProcessorEvent: AttributeConfig{Enabled: true},
TypeSubtype: AttributeConfig{Enabled: true},
DurationUs: AttributeConfig{Enabled: true},
EventOutcome: AttributeConfig{Enabled: true},
ServiceTarget: AttributeConfig{Enabled: true},
Expand Down
2 changes: 2 additions & 0 deletions enrichments/trace/internal/elastic/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const (
AttributeTransactionDurationUs = "transaction.duration.us"
AttributeTransactionResult = "transaction.result"
AttributeSpanName = "span.name"
AttributeSpanType = "span.type"
AttributeSpanSubtype = "span.subtype"
AttributeEventOutcome = "event.outcome"
AttributeSuccessCount = "event.success_count"
AttributeServiceTargetType = "service.target.type"
Expand Down
47 changes: 41 additions & 6 deletions enrichments/trace/internal/elastic/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type spanEnrichmentContext struct {
rpcService string
grpcStatus string
dbName string
dbType string
dbSystem string
messagingSystem string
messagingDestinationName string

Expand Down Expand Up @@ -142,7 +142,7 @@ func (s *spanEnrichmentContext) Enrich(span ptrace.Span, cfg config.Config) {
s.dbName = v.Str()
case semconv.AttributeDBSystem:
s.isDB = true
s.dbType = v.Str()
s.dbSystem = v.Str()
}
return true
})
Expand Down Expand Up @@ -197,6 +197,9 @@ func (s *spanEnrichmentContext) enrichSpan(
if cfg.ProcessorEvent.Enabled {
span.Attributes().PutStr(AttributeProcessorEvent, "span")
}
if cfg.TypeSubtype.Enabled {
s.setSpanTypeSubtype(span)
}
if cfg.EventOutcome.Enabled {
s.setEventOutcome(span)
}
Expand Down Expand Up @@ -277,6 +280,38 @@ func (s *spanEnrichmentContext) setEventOutcome(span ptrace.Span) {
span.Attributes().PutInt(AttributeSuccessCount, int64(successCount))
}

func (s *spanEnrichmentContext) setSpanTypeSubtype(span ptrace.Span) {
var spanType, spanSubtype string

switch {
case s.isDB:
spanType = "db"
spanSubtype = s.dbSystem
case s.isMessaging:
spanType = "messaging"
spanSubtype = s.messagingSystem
case s.isRPC:
spanType = "external"
spanSubtype = s.rpcSystem
case s.isHTTP:
spanType = "external"
spanSubtype = "http"
default:
switch span.Kind() {
case ptrace.SpanKindInternal:
spanType = "app"
spanSubtype = "internal"
default:
spanType = "unknown"
}
}

span.Attributes().PutStr(AttributeSpanType, spanType)
if spanSubtype != "" {
span.Attributes().PutStr(AttributeSpanSubtype, spanSubtype)
}
}

func (s *spanEnrichmentContext) setServiceTarget(span ptrace.Span) {
var targetType, targetName string

Expand All @@ -287,8 +322,8 @@ func (s *spanEnrichmentContext) setServiceTarget(span ptrace.Span) {
switch {
case s.isDB:
targetType = "db"
if s.dbType != "" {
targetType = s.dbType
if s.dbSystem != "" {
targetType = s.dbSystem
}
if s.dbName != "" {
targetName = s.dbName
Expand Down Expand Up @@ -330,8 +365,8 @@ func (s *spanEnrichmentContext) setDestinationService(span ptrace.Span) {

switch {
case s.isDB:
if destnResource == "" && s.dbType != "" {
destnResource = s.dbType
if destnResource == "" && s.dbSystem != "" {
destnResource = s.dbSystem
}
case s.isMessaging:
if destnResource == "" && s.messagingSystem != "" {
Expand Down
40 changes: 40 additions & 0 deletions enrichments/trace/internal/elastic/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ func TestElasticSpanEnrich(t *testing.T) {
enrichedAttrs: map[string]any{
AttributeSpanName: "",
AttributeProcessorEvent: "span",
AttributeSpanType: "unknown",
AttributeSpanDurationUs: int64(0),
AttributeEventOutcome: "success",
AttributeSuccessCount: int64(1),
Expand All @@ -326,6 +327,25 @@ func TestElasticSpanEnrich(t *testing.T) {
input: getElasticSpan(),
enrichedAttrs: map[string]any{},
},
{
name: "internal_span",
input: func() ptrace.Span {
span := ptrace.NewSpan()
span.SetParentSpanID([8]byte{1})
span.SetKind(ptrace.SpanKindInternal)
return span
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeSpanName: "",
AttributeProcessorEvent: "span",
AttributeSpanType: "app",
AttributeSpanSubtype: "internal",
AttributeSpanDurationUs: int64(0),
AttributeEventOutcome: "success",
AttributeSuccessCount: int64(1),
},
},
{
name: "peer_service",
input: func() ptrace.Span {
Expand All @@ -338,6 +358,7 @@ func TestElasticSpanEnrich(t *testing.T) {
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanType: "unknown",
AttributeSpanDurationUs: expectedDuration.Microseconds(),
AttributeEventOutcome: "success",
AttributeSuccessCount: int64(1),
Expand All @@ -362,6 +383,8 @@ func TestElasticSpanEnrich(t *testing.T) {
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanType: "external",
AttributeSpanSubtype: "http",
AttributeSpanDurationUs: expectedDuration.Microseconds(),
AttributeEventOutcome: "success",
AttributeSuccessCount: int64(1),
Expand Down Expand Up @@ -392,6 +415,8 @@ func TestElasticSpanEnrich(t *testing.T) {
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanType: "external",
AttributeSpanSubtype: "http",
AttributeSpanDurationUs: expectedDuration.Microseconds(),
AttributeEventOutcome: "success",
AttributeSuccessCount: int64(1),
Expand Down Expand Up @@ -420,6 +445,8 @@ func TestElasticSpanEnrich(t *testing.T) {
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanType: "external",
AttributeSpanSubtype: "http",
AttributeSpanDurationUs: expectedDuration.Microseconds(),
AttributeEventOutcome: "success",
AttributeSuccessCount: int64(1),
Expand All @@ -444,6 +471,8 @@ func TestElasticSpanEnrich(t *testing.T) {
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanType: "external",
AttributeSpanSubtype: "grpc",
AttributeSpanDurationUs: expectedDuration.Microseconds(),
AttributeEventOutcome: "success",
AttributeSuccessCount: int64(1),
Expand All @@ -465,6 +494,8 @@ func TestElasticSpanEnrich(t *testing.T) {
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanType: "external",
AttributeSpanSubtype: "xmlrpc",
AttributeSpanDurationUs: expectedDuration.Microseconds(),
AttributeEventOutcome: "success",
AttributeSuccessCount: int64(1),
Expand All @@ -488,6 +519,7 @@ func TestElasticSpanEnrich(t *testing.T) {
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanType: "external",
AttributeSpanDurationUs: expectedDuration.Microseconds(),
AttributeEventOutcome: "success",
AttributeSuccessCount: int64(1),
Expand All @@ -509,6 +541,8 @@ func TestElasticSpanEnrich(t *testing.T) {
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanType: "messaging",
AttributeSpanSubtype: "kafka",
AttributeSpanDurationUs: expectedDuration.Microseconds(),
AttributeEventOutcome: "success",
AttributeSuccessCount: int64(1),
Expand All @@ -530,6 +564,7 @@ func TestElasticSpanEnrich(t *testing.T) {
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanType: "messaging",
AttributeSpanDurationUs: expectedDuration.Microseconds(),
AttributeEventOutcome: "success",
AttributeSuccessCount: int64(1),
Expand All @@ -552,6 +587,7 @@ func TestElasticSpanEnrich(t *testing.T) {
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanType: "messaging",
AttributeSpanDurationUs: expectedDuration.Microseconds(),
AttributeEventOutcome: "success",
AttributeSuccessCount: int64(1),
Expand Down Expand Up @@ -580,6 +616,8 @@ func TestElasticSpanEnrich(t *testing.T) {
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanType: "db",
AttributeSpanSubtype: "elasticsearch",
AttributeSpanDurationUs: expectedDuration.Microseconds(),
AttributeEventOutcome: "success",
AttributeSuccessCount: int64(1),
Expand Down Expand Up @@ -613,6 +651,8 @@ func TestElasticSpanEnrich(t *testing.T) {
enrichedAttrs: map[string]any{
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanType: "db",
AttributeSpanSubtype: "cassandra",
AttributeSpanDurationUs: expectedDuration.Microseconds(),
AttributeEventOutcome: "success",
AttributeSuccessCount: int64(1),
Expand Down

0 comments on commit 25c1f83

Please sign in to comment.