Skip to content

Commit

Permalink
feat(tracing): Add a method to get transaction for given span: GetTra…
Browse files Browse the repository at this point in the history
…nsaction() (#558)



Co-authored-by: Michi Hoffmann <cleptric@users.noreply.github.com>
  • Loading branch information
tonyo and cleptric committed Jan 31, 2023
1 parent d362c36 commit 4e5ede9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
- Add Span.IsTransaction() ([#543](https://github.com/getsentry/sentry-go/pull/543))
- Add TraceParentContext structure ([#545](https://github.com/getsentry/sentry-go/pull/545))
- Add a new SpanOption: `SpanSampled` ([#546](https://github.com/getsentry/sentry-go/pull/546))
- Add `Span.GetTransaction()` method ([#558](https://github.com/getsentry/sentry-go/pull/558))

## 0.17.0

Expand Down
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")
}
}

0 comments on commit 4e5ede9

Please sign in to comment.