diff --git a/v3/newrelic/internal_txn.go b/v3/newrelic/internal_txn.go index 25468ddd8..8adf35d50 100644 --- a/v3/newrelic/internal_txn.go +++ b/v3/newrelic/internal_txn.go @@ -54,6 +54,14 @@ type thread struct { thread *tracingThread } +func (thd *thread) IsEnded() bool { + txn := thd.txn + txn.Lock() + defer txn.Unlock() + + return txn.finished +} + func (txn *txn) markStart(now time.Time) { txn.Start = now // The mainThread is considered active now. diff --git a/v3/newrelic/internal_txn_test.go b/v3/newrelic/internal_txn_test.go index 04d37bc28..5aca377a3 100644 --- a/v3/newrelic/internal_txn_test.go +++ b/v3/newrelic/internal_txn_test.go @@ -1018,3 +1018,32 @@ func TestPanicNilRecovery(t *testing.T) { }, }) } + +func TestIsEndedInternal(t *testing.T) { + tests := []struct { + name string + txn *txn + expected bool + }{ + { + name: "finished transaction", + txn: &txn{finished: true}, + expected: true, + }, + { + name: "unfinished transaction", + txn: &txn{finished: false}, + expected: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + thread := &thread{txn: tt.txn} + result := thread.IsEnded() + if result != tt.expected { + t.Errorf("IsEnded() = %v; want %v", result, tt.expected) + } + }) + } +} diff --git a/v3/newrelic/transaction.go b/v3/newrelic/transaction.go index 7d2f8ff76..d4f06466f 100644 --- a/v3/newrelic/transaction.go +++ b/v3/newrelic/transaction.go @@ -243,10 +243,10 @@ func (txn *Transaction) SetWebRequestHTTP(r *http.Request) { // If the transaction is nil, the thread is nil, or the transaction is finished, it returns true. // Otherwise, it returns thread.finished value. func (txn *Transaction) IsEnded() bool { - if txn == nil || txn.thread == nil || txn.thread.txn == nil { + if nilTransaction(txn) { return true } - return txn.thread.txn.finished + return txn.thread.IsEnded() } func transport(r *http.Request) TransportType {