Skip to content

Commit

Permalink
Merge pull request #755 from nr-swilloughby/txnname
Browse files Browse the repository at this point in the history
added Name() transaction name getter function
  • Loading branch information
nr-swilloughby authored Jul 31, 2023
2 parents fbdf0f2 + e35b5ed commit a61a47a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions v3/newrelic/internal_txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,12 @@ func (txn *txn) SetName(name string) error {
return nil
}

func (txn *txn) GetName() string {
txn.Lock()
defer txn.Unlock()
return txn.Name
}

func (txn *txn) Ignore() error {
txn.Lock()
defer txn.Unlock()
Expand Down
20 changes: 20 additions & 0 deletions v3/newrelic/internal_txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,26 @@ func TestNilTransaction(t *testing.T) {
}
}

func TestGetName(t *testing.T) {
replyfn := func(reply *internal.ConnectReply) {
reply.SetSampleEverything()
reply.EntityGUID = "entities-are-guid"
reply.TraceIDGenerator = internal.NewTraceIDGenerator(12345)
}
cfgfn := func(cfg *Config) {
cfg.AppName = "app-name"
cfg.DistributedTracer.Enabled = true
}
app := testApp(replyfn, cfgfn, t)
txn := app.StartTransaction("hello")
defer txn.End()
txn.Ignore()
txn.SetName("hello世界")
if theName := txn.Name(); theName != "hello世界" {
t.Error(theName)
}
}

func TestEmptyTransaction(t *testing.T) {
txn := &Transaction{}

Expand Down
13 changes: 13 additions & 0 deletions v3/newrelic/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ func (txn *Transaction) SetName(name string) {
txn.thread.logAPIError(txn.thread.SetName(name), "set transaction name", nil)
}

// Name returns the name currently set for the transaction, as, e.g. by a call to SetName.
// If unable to do so (such as due to a nil transaction pointer), the empty string is returned.
func (txn *Transaction) Name() string {
// This is called Name rather than GetName to be consistent with the prevailing naming
// conventions for the Go language, even though the underlying internal call must be called
// something else (like GetName) because there's already a Name struct member.

if txn == nil || txn.thread == nil {
return ""
}
return txn.thread.GetName()
}

// NoticeError records an error. The Transaction saves the first five
// errors. For more control over the recorded error fields, see the
// newrelic.Error type.
Expand Down

0 comments on commit a61a47a

Please sign in to comment.