Skip to content

Commit

Permalink
Add specific type and subtype for otlp gen_ai spans (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
xrmx authored Dec 18, 2024
1 parent d584bfa commit c7b04dc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
16 changes: 15 additions & 1 deletion input/otlp/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const (
attributeStackTrace = "code.stacktrace" // semconv 1.24 or later
attributeDataStreamDataset = "data_stream.dataset"
attributeDataStreamNamespace = "data_stream.namespace"
attributeGenAiSystem = "gen_ai.system"
)

// ConsumeTracesResult contains the number of rejected spans and error message for partial success response.
Expand Down Expand Up @@ -615,14 +616,18 @@ func TranslateSpan(spanKind ptrace.SpanKind, attributes pcommon.Map, event *mode
rpcService string
)

var (
genAiSystem string
)

var http modelpb.HTTP
var httpRequest modelpb.HTTPRequest
var httpResponse modelpb.HTTPResponse
var message modelpb.Message
var db modelpb.DB
var destinationService modelpb.DestinationService
var serviceTarget modelpb.ServiceTarget
var isHTTP, isDatabase, isRPC, isMessaging bool
var isHTTP, isDatabase, isRPC, isMessaging, isGenAi bool
var samplerType, samplerParam pcommon.Value
attributes.Range(func(kDots string, v pcommon.Value) bool {
if isJaeger {
Expand Down Expand Up @@ -812,6 +817,11 @@ func TranslateSpan(spanKind ptrace.SpanKind, attributes pcommon.Map, event *mode
// stacktrace is expected to be large thus un-truncated value is needed
event.Code.Stacktrace = v.Str()

// gen_ai.*
case attributeGenAiSystem:
genAiSystem = stringval
isGenAi = true

// miscellaneous
case "span.kind": // filter out
case semconv.AttributePeerService:
Expand Down Expand Up @@ -991,6 +1001,10 @@ func TranslateSpan(spanKind ptrace.SpanKind, attributes pcommon.Map, event *mode
destinationService.Resource = resource
}
}
case isGenAi:
event.Span.Type = "genai"
event.Span.Subtype = genAiSystem
serviceTarget.Type = event.Span.Type
default:
// Only set event.Span.Type if not already set
if event.Span.Type == "" {
Expand Down
10 changes: 10 additions & 0 deletions input/otlp/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,16 @@ func TestSpanEventsDataStream(t *testing.T) {
}
}

func TestGenAiSpan(t *testing.T) {
event := transformSpanWithAttributes(t, map[string]interface{}{
"gen_ai.system": "openai",
})

assert.Equal(t, "genai", event.Span.Type)
assert.Equal(t, "openai", event.Span.Subtype)
assert.Equal(t, "", event.Span.Action)
}

func transformTransactionWithAttributes(t *testing.T, attrs map[string]interface{}, configFns ...func(ptrace.Span)) *modelpb.APMEvent {
traces, spans := newTracesSpans()
otelSpan := spans.Spans().AppendEmpty()
Expand Down

0 comments on commit c7b04dc

Please sign in to comment.