Skip to content

Set GenAi subtype for spans that have the gen_ai.system attribute #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 44 additions & 35 deletions enrichments/trace/internal/elastic/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import (
"github.com/elastic/opentelemetry-lib/enrichments/trace/config"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
semconv "go.opentelemetry.io/collector/semconv/v1.25.0"
semconv25 "go.opentelemetry.io/collector/semconv/v1.25.0"
semconv27 "go.opentelemetry.io/collector/semconv/v1.27.0"
Comment on lines +36 to +37

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and so it begins... 😛

tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
"google.golang.org/grpc/codes"
)
Expand Down Expand Up @@ -68,6 +69,7 @@ type spanEnrichmentContext struct {
dbSystem string
messagingSystem string
messagingDestinationName string
genAiSystem string

serverPort int64
urlPort int64
Expand All @@ -82,6 +84,7 @@ type spanEnrichmentContext struct {
isHTTP bool
isDB bool
messagingDestinationTemp bool
isGenAi bool
}

func (s *spanEnrichmentContext) Enrich(span ptrace.Span, cfg config.Config) {
Expand All @@ -91,86 +94,89 @@ func (s *spanEnrichmentContext) Enrich(span ptrace.Span, cfg config.Config) {
// Extract information from span attributes.
span.Attributes().Range(func(k string, v pcommon.Value) bool {
switch k {
case semconv.AttributePeerService:
case semconv25.AttributePeerService:
s.peerService = v.Str()
case semconv.AttributeServerAddress:
case semconv25.AttributeServerAddress:
s.serverAddress = v.Str()
case semconv.AttributeServerPort:
case semconv25.AttributeServerPort:
s.serverPort = v.Int()
case semconv.AttributeNetPeerName:
case semconv25.AttributeNetPeerName:
if s.serverAddress == "" {
// net.peer.name is deprecated, so has lower priority
// only set when not already set with server.address
// and allowed to be overridden by server.address.
s.serverAddress = v.Str()
}
case semconv.AttributeNetPeerPort:
case semconv25.AttributeNetPeerPort:
if s.serverPort == 0 {
// net.peer.port is deprecated, so has lower priority
// only set when not already set with server.port and
// allowed to be overridden by server.port.
s.serverPort = v.Int()
}
case semconv.AttributeMessagingDestinationName:
case semconv25.AttributeMessagingDestinationName:
s.isMessaging = true
s.messagingDestinationName = v.Str()
case semconv.AttributeMessagingOperation:
case semconv25.AttributeMessagingOperation:
s.isMessaging = true
case semconv.AttributeMessagingSystem:
case semconv25.AttributeMessagingSystem:
s.isMessaging = true
s.messagingSystem = v.Str()
case semconv.AttributeMessagingDestinationTemporary:
case semconv25.AttributeMessagingDestinationTemporary:
s.isMessaging = true
s.messagingDestinationTemp = true
case semconv.AttributeHTTPStatusCode,
semconv.AttributeHTTPResponseStatusCode:
case semconv25.AttributeHTTPStatusCode,
semconv25.AttributeHTTPResponseStatusCode:
s.isHTTP = true
s.httpStatusCode = v.Int()
case semconv.AttributeHTTPMethod,
semconv.AttributeHTTPRequestMethod,
semconv.AttributeHTTPTarget,
semconv.AttributeHTTPScheme,
semconv.AttributeHTTPFlavor,
semconv.AttributeNetHostName:
case semconv25.AttributeHTTPMethod,
semconv25.AttributeHTTPRequestMethod,
semconv25.AttributeHTTPTarget,
semconv25.AttributeHTTPScheme,
semconv25.AttributeHTTPFlavor,
semconv25.AttributeNetHostName:
s.isHTTP = true
case semconv.AttributeURLFull,
semconv.AttributeHTTPURL:
case semconv25.AttributeURLFull,
semconv25.AttributeHTTPURL:
s.isHTTP = true
// ignoring error as if parse fails then we don't want the url anyway
s.urlFull, _ = url.Parse(v.Str())
case semconv.AttributeURLScheme:
case semconv25.AttributeURLScheme:
s.isHTTP = true
s.urlScheme = v.Str()
case semconv.AttributeURLDomain:
case semconv25.AttributeURLDomain:
s.isHTTP = true
s.urlDomain = v.Str()
case semconv.AttributeURLPort:
case semconv25.AttributeURLPort:
s.isHTTP = true
s.urlPort = v.Int()
case semconv.AttributeURLPath:
case semconv25.AttributeURLPath:
s.isHTTP = true
s.urlPath = v.Str()
case semconv.AttributeURLQuery:
case semconv25.AttributeURLQuery:
s.isHTTP = true
s.urlQuery = v.Str()
case semconv.AttributeRPCGRPCStatusCode:
case semconv25.AttributeRPCGRPCStatusCode:
s.isRPC = true
s.grpcStatus = codes.Code(v.Int()).String()
case semconv.AttributeRPCSystem:
case semconv25.AttributeRPCSystem:
s.isRPC = true
s.rpcSystem = v.Str()
case semconv.AttributeRPCService:
case semconv25.AttributeRPCService:
s.isRPC = true
s.rpcService = v.Str()
case semconv.AttributeDBStatement,
semconv.AttributeDBUser:
case semconv25.AttributeDBStatement,
semconv25.AttributeDBUser:
s.isDB = true
case semconv.AttributeDBName:
case semconv25.AttributeDBName:
s.isDB = true
s.dbName = v.Str()
case semconv.AttributeDBSystem:
case semconv25.AttributeDBSystem:
s.isDB = true
s.dbSystem = v.Str()
case semconv27.AttributeGenAiSystem:
s.isGenAi = true
s.genAiSystem = v.Str()
}
return true
})
Expand Down Expand Up @@ -361,6 +367,9 @@ func (s *spanEnrichmentContext) setSpanTypeSubtype(span ptrace.Span) {
case s.isHTTP:
spanType = "external"
spanSubtype = "http"
case s.isGenAi:
spanType = "genai"
spanSubtype = s.genAiSystem
default:
switch span.Kind() {
case ptrace.SpanKindInternal:
Expand Down Expand Up @@ -504,11 +513,11 @@ func (s *spanEventEnrichmentContext) enrich(
if s.exception {
se.Attributes().Range(func(k string, v pcommon.Value) bool {
switch k {
case semconv.AttributeExceptionEscaped:
case semconv25.AttributeExceptionEscaped:
s.exceptionEscaped = v.Bool()
case semconv.AttributeExceptionType:
case semconv25.AttributeExceptionType:
s.exceptionType = v.Str()
case semconv.AttributeExceptionMessage:
case semconv25.AttributeExceptionMessage:
s.exceptionMessage = v.Str()
}
return true
Expand Down
Loading
Loading