Skip to content

Commit

Permalink
fix(exporter/sentry): treat spans with a kind of server or consumer a…
Browse files Browse the repository at this point in the history
…s root spans.

fix(exporter/sentry): only include parentSpanID if it is not empty.
  • Loading branch information
fourbytes committed Oct 2, 2021
1 parent 31cb599 commit 804a34f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
40 changes: 22 additions & 18 deletions exporter/sentryexporter/sentry_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s *SentryExporter) pushTraceData(_ context.Context, td pdata.Traces) error
//
// If the span is not a root span, we can either associate it with an existing
// transaction, or we can temporarily consider it an orphan span.
if isRootSpan(sentrySpan) {
if spanIsTransaction(otelSpan) {
transactionMap[sentrySpan.SpanID] = transactionFromSpan(sentrySpan)
idMap[sentrySpan.SpanID] = sentrySpan.SpanID
} else {
Expand All @@ -102,16 +102,16 @@ func (s *SentryExporter) pushTraceData(_ context.Context, td pdata.Traces) error
}
}

if len(transactionMap) == 0 {
return nil
}

// After the first pass through, we can't necessarily make the assumption we have not associated all
// the spans with a transaction. As such, we must classify the remaining spans as orphans or not.
orphanSpans := classifyAsOrphanSpans(maybeOrphanSpans, len(maybeOrphanSpans)+1, idMap, transactionMap)

transactions := generateTransactions(transactionMap, orphanSpans)

if len(transactions) == 0 {
return nil
}

events := append(transactions, exceptionEvents...)

s.transport.SendEvents(events)
Expand Down Expand Up @@ -249,15 +249,18 @@ func convertToSentrySpan(span pdata.Span, library pdata.InstrumentationLibrary,
tags["library_version"] = library.Version()

sentrySpan = &sentry.Span{
TraceID: span.TraceID().Bytes(),
SpanID: span.SpanID().Bytes(),
ParentSpanID: span.ParentSpanID().Bytes(),
Description: description,
Op: op,
Tags: tags,
StartTime: unixNanoToTime(span.StartTimestamp()),
EndTime: unixNanoToTime(span.EndTimestamp()),
Status: status,
TraceID: span.TraceID().Bytes(),
SpanID: span.SpanID().Bytes(),
Description: description,
Op: op,
Tags: tags,
StartTime: unixNanoToTime(span.StartTimestamp()),
EndTime: unixNanoToTime(span.EndTimestamp()),
Status: status,
}

if parentSpanID := span.ParentSpanID(); !parentSpanID.IsEmpty() {
sentrySpan.ParentSpanID = parentSpanID.Bytes()
}

return sentrySpan
Expand Down Expand Up @@ -366,10 +369,11 @@ func statusFromSpanStatus(spanStatus pdata.SpanStatus) (status sentry.SpanStatus
return canonicalCodes[code], spanStatus.Message()
}

// isRootSpan determines if a span is a root span.
// If parent span id is empty, then the span is a root span.
func isRootSpan(s *sentry.Span) bool {
return s.ParentSpanID == sentry.SpanID{}
// spanIsTransaction determines if a span should be sent to Sentry as a transaction.
// If parent span id is empty or the span kind allows remote parent spans, then the span is a root span.
func spanIsTransaction(s pdata.Span) bool {
kind := s.Kind()
return s.ParentSpanID() == pdata.SpanID{} || kind == pdata.SpanKindServer || kind == pdata.SpanKindConsumer
}

// transactionFromSpan converts a span to a transaction.
Expand Down
4 changes: 2 additions & 2 deletions exporter/sentryexporter/sentry_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func TestSpanToSentrySpan(t *testing.T) {

sentrySpan := convertToSentrySpan(testSpan, pdata.NewInstrumentationLibrary(), map[string]string{})
assert.NotNil(t, sentrySpan)
assert.True(t, isRootSpan(sentrySpan))
assert.True(t, spanIsTransaction(testSpan))
})

t.Run("with full span", func(t *testing.T) {
Expand Down Expand Up @@ -329,7 +329,7 @@ func TestSpanToSentrySpan(t *testing.T) {
actual := convertToSentrySpan(testSpan, library, resourceTags)

assert.NotNil(t, actual)
assert.False(t, isRootSpan(actual))
assert.False(t, spanIsTransaction(testSpan))

expected := &sentry.Span{
TraceID: TraceIDFromHex("01020304050607080807060504030201"),
Expand Down

0 comments on commit 804a34f

Please sign in to comment.