Instrument Go transactions

Monitor your Go application or microservice by creating transactions associated with specific app server activities, such as background tasks or HTTP responses.

For general information about New Relic's Go monitoring and how to get it, see New Relic for Go.

Overview of Go monitoring

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.

A transaction refers to an interaction with an app server, such as an HTTP request, that is monitored by New Relic. Transactions in the New Relic UI are separated into web transactions and non-web transactions. When you instrument or wrap a transaction that has an HTTP request and response writer, that transaction will be treated as a web transaction. Transactions that don't have HTTP data will be treated as non-web transactions. For more on how transactions are handled in the New Relic UI, see Transactions page.

Segments are optional subcomponents of transactions. Segments can be used to break down and time specific functions in transactions.

Create a transaction

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

To create a transaction:

Place the following code immediately after the start of the function you want to monitor:

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

In this code, app refers to the variable assigned during the New Relic configuration process. See the install process for more info.

The defer statement defers the segment ending until the function closes.

The two nil parameters would make this transaction a non-web transaction. To create a web transaction, you would 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:

Before:

func checkout() {
// function code here
}

After:

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

Create 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:

Before:

http.HandleFunc("/users", usersHandler)

After:

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

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

To access the transaction in your handler, use type assertion on the response writer passed to the handler. Here is an example of this:

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

Additional documentation resources include:

Join the discussion about Go monitoring in the New Relic Online Technical Community! The Technical Community is a public platform to discuss and troubleshoot your New Relic toolset.

If you need additional help, get support at support.newrelic.com.