Skip to content

Commit

Permalink
Set GenAi subtype for spans
Browse files Browse the repository at this point in the history
  • Loading branch information
xrmx committed Dec 3, 2024
1 parent 9daa1d2 commit 67ba8e1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
12 changes: 12 additions & 0 deletions enrichments/trace/internal/elastic/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ import (
"google.golang.org/grpc/codes"
)

// Here we are duplicating newere semconv attributes because since v1.26.0
// all deprecated symbols have been removed
const (
semconvAttributeGenAiSystem = "gen_ai.system"
)

// EnrichSpan adds Elastic specific attributes to the OTel span.
// These attributes are derived from the base attributes and appended to
// the span attributes. The enrichment logic is performed by categorizing
Expand Down Expand Up @@ -82,6 +88,7 @@ type spanEnrichmentContext struct {
isHTTP bool
isDB bool
messagingDestinationTemp bool
isGenAi bool
}

func (s *spanEnrichmentContext) Enrich(span ptrace.Span, cfg config.Config) {
Expand Down Expand Up @@ -171,6 +178,8 @@ func (s *spanEnrichmentContext) Enrich(span ptrace.Span, cfg config.Config) {
case semconv.AttributeDBSystem:
s.isDB = true
s.dbSystem = v.Str()
case semconvAttributeGenAiSystem:
s.isGenAi = true
}
return true
})
Expand Down Expand Up @@ -361,6 +370,9 @@ func (s *spanEnrichmentContext) setSpanTypeSubtype(span ptrace.Span) {
case s.isHTTP:
spanType = "external"
spanSubtype = "http"
case s.isGenAi:
spanType = "external"
spanSubtype = "genai"
default:
switch span.Kind() {
case ptrace.SpanKindInternal:
Expand Down
23 changes: 23 additions & 0 deletions enrichments/trace/internal/elastic/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
return &spanLinks
}(),
},

} {
t.Run(tc.name, func(t *testing.T) {
expectedSpan := ptrace.NewSpan()
Expand Down Expand Up @@ -914,6 +915,28 @@ func TestElasticSpanEnrich(t *testing.T) {
return &spanLinks
}(),
},
{
name: "external_type_genai",
input: func() ptrace.Span {
span := getElasticSpan()
span.SetName("testspan")
span.SetSpanID([8]byte{1})
span.Attributes().PutStr(semconvAttributeGenAiSystem, "openai")
return span
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanRepresentativeCount: float64(1),
AttributeSpanType: "external",
AttributeSpanSubtype: "genai",
AttributeSpanDurationUs: expectedDuration.Microseconds(),
AttributeEventOutcome: "success",
AttributeSuccessCount: int64(1),
},
},
} {
t.Run(tc.name, func(t *testing.T) {
expectedSpan := ptrace.NewSpan()
Expand Down

0 comments on commit 67ba8e1

Please sign in to comment.