Skip to content
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

feat(tracing): Add a method to get transaction for given span: GetTransaction() #558

Merged
merged 7 commits into from Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions tracing.go
Expand Up @@ -241,6 +241,26 @@ func (s *Span) IsTransaction() bool {
return s.isTransaction
}

// GetTransaction returns the transaction that contains this span.
//
// For transaction spans it returns itself. For spans that were created manually
// the method returns "nil".
func (s *Span) GetTransaction() *Span {
spanRecorder := s.spanRecorder()
if spanRecorder == nil {
// This probably means that the Span was created manually (not via
// StartTransaction/StartSpan or StartChild).
// Return "nil" to indicate that it's not a normal situation.
return nil
}
recorderRoot := spanRecorder.root()
if recorderRoot == nil {
// Same as above: manually created Span.
return nil
}
return recorderRoot
}

// TODO(tracing): maybe add shortcuts to get/set transaction name. Right now the
// transaction name is in the Scope, as it has existed there historically, prior
// to tracing.
Expand Down
34 changes: 34 additions & 0 deletions tracing_test.go
Expand Up @@ -764,6 +764,7 @@ func TestParseTraceParentContext(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
traceParentContext, valid := ParseTraceParentContext([]byte(tt.sentryTrace))

Expand All @@ -776,3 +777,36 @@ func TestParseTraceParentContext(t *testing.T) {
})
}
}

func TestGetTransactionWithProperTransactionsSpans(t *testing.T) {
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
})
transaction := StartTransaction(ctx, "transaction")
child1 := transaction.StartChild("child1")
child2 := transaction.StartChild("child2")
grandchild := child1.StartChild("grandchild")

assertEqual(t, transaction.GetTransaction(), transaction)
assertEqual(t, child1.GetTransaction(), transaction)
assertEqual(t, child2.GetTransaction(), transaction)
assertEqual(t, grandchild.GetTransaction(), transaction)

// Another transaction, unrelated to the first one
anotherTransaction := StartTransaction(ctx, "another transaction")

assertNotEqual(t, transaction, anotherTransaction)
assertEqual(t, anotherTransaction.GetTransaction(), anotherTransaction)
}

func TestGetTransactionReturnsNilOnManuallyCreatedSpans(t *testing.T) {
span1 := Span{}
if span1.GetTransaction() != nil {
t.Errorf("GetTransaction() should return nil on manually created Spans")
}

span2 := Span{isTransaction: true}
if span2.GetTransaction() != nil {
t.Errorf("GetTransaction() should return nil on manually created Spans")
}
}