Instrument Go transactions

Monitor your New Relic for Go application or microservice by creating transactions associated with specific app server activities, such as HTTP responses or background tasks. You can then view your app's performance in the New Relic UI, including the APM Transactions page.

Identify web and non-web transactions

Unlike many other languages, Go applications run from a compiled, native binary file. This means that setting up New Relic for your Golang app requires you to manually add New Relic methods to your source code.

In New Relic APM, transactions are identified as web transactions or non-web transactions.

  • When you instrument or wrap a transaction that has an HTTP request and response writer, New Relic treats it as a web transaction.
  • When you instrument or wrap a transaction that does not have HTTP data, New Relic treats it as a non-web transaction.

Segments are the functions and calls that make up a transaction. You can also use New Relic for Go to add segment-level detail to your transactions.

Monitor a transaction

Each transaction must be used in a single goroutine. A transaction cannot be used across multiple goroutines.

Do not use brackets [suffix] at the end of your transaction name. New Relic automatically strips brackets from the name. Instead, use parentheses (suffix) or other symbols if needed.

To monitor a transaction: Place the following code immediately after the start of the function you want to monitor. For example:

txn := app.StartTransaction("transaction_name", nil, nil)
defer txn.End()

In this code example:

  • app refers to the variable assigned during the New Relic configuration process. For more information, see the Go agent installation procedures.
  • The two nil parameters make this a non-web transaction.
  • The defer statement defers the segment ending until the function closes.

To monitor a web transaction, use an HTTP response writer and request in place of the nil parameters:

txn := app.StartTransaction("transaction_name", w http.ResponseWriter, r *http.Request)
defer txn.End()
Transaction example

Here is a simple before-and-after example for creating a transaction called checkout_transaction:


func checkout() {
// function code here


func checkout() {
txn := app.StartTransaction("checkout_transaction", nil, nil)
defer txn.End()
// function code here

Monitor a transaction by wrapping an HTTP handler

If you are using the standard HTTP library package, you can create transactions by wrapping HTTP requests, as an alternative to instrumenting a function's code.

Here is a before-and-after example of an HTTP handler being wrapped:


http.HandleFunc("/users", usersHandler)


This automatically starts and ends a transaction with the request and response writer.

http.HandleFunc(newrelic.WrapHandleFunc(app, "/users", usersHandler))

To access the transaction in your handler, use type assertion on the response writer passed to the handler. For example:

func myHandler(w http.ResponseWriter, r *http.Request) {
    if txn, ok := w.(newrelic.Transaction); ok {
        txn.NoticeError(errors.New("my error message"))

Additional transaction methods

The transaction object has several optional methods that can be used for controlling transaction behavior, such as NoticeError, AddAttribute, and Ignore. For a list of transaction methods, see the New Relic for Go transaction methods on GitHub.

For more help

Recommendations for learning more: