The ability to trace asynchronous segments with the NewGoroutine
API is available as of Go agent version 2.6.0. If you do not have version 2.6.0 or higher, update your Go agent to the latest version.
NewGoroutine
The Transaction.NewGoroutine() Transaction
method allows transactions to create segments in multiple goroutines.
NewGoroutine
returns a new reference to the Transaction
. This must be called any time you are passing the Transaction
to another goroutine which makes segments. Each segment-creating goroutine must have its own Transaction
reference. It does not matter if you call this before or after the other goroutine has started.
All Transaction
methods can be used in any Transaction
reference. The Transaction
will end when End()
is called in any goroutine.
Example passing a new Transaction
reference directly to another goroutine:
go func(txn *newrelic.Transaction) { defer txn.StartSegment("async").End() time.Sleep(100 * time.Millisecond)}(txn.NewGoroutine())
Example passing a new Transaction
reference on a channel to another goroutine:
ch := make(chan *newrelic.Transaction)go func() { txn := <-ch defer txn.StartSegment("async").End() time.Sleep(100 * time.Millisecond)}()ch <- txn.NewGoroutine()
For more help
If you need more help, check out these support and learning resources:
- Browse the Explorers Hub to get help from the community and join in discussions.
- Find answers on our sites and learn how to use our support portal.
- Run New Relic Diagnostics, our troubleshooting tool for Linux, Windows, and macOS.
- Review New Relic's data security and licenses documentation.