---
title: Instrument Go transactions
source: https://docs.newrelic.com/docs/apm/agents/go-agent/instrumentation/instrument-go-transactions
---

Monitor your [New Relic for Go](https://docs.newrelic.com/docs/agents/go-agent/get-started/new-relic-go) application or microservice by creating [transactions](https://docs.newrelic.com/docs/apm/transactions/intro-transactions/transactions-new-relic-apm) 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, including the [APM **Transactions** page](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/transactions-page).

## Identify web and non-web transactions [#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.

In APM, transactions are identified as [web transactions](https://docs.newrelic.com/docs/apm/transactions/intro-transactions/transactions-new-relic-apm) or [non-web transactions](https://docs.newrelic.com/docs/apm/transactions/intro-transactions/monitor-background-processes-other-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](https://docs.newrelic.com/docs/agents/go-agent/get-started/instrument-go-segments).

## Monitor a transaction [#go-txn]

> #### ⚠️ IMPORTANT
>
> 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:

```go
txn := app.StartTransaction("transaction_name")
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](https://docs.newrelic.com/docs/agents/go-agent/installation/install-new-relic-go).
-   The [`defer`](https://gobyexample.com/defer) statement defers the segment ending until the function closes.

To monitor a web transaction, call the `Transaction.SetWebRequest` and optionally the `Transaction.SetWebResponse` APIs:

```go
txn := app.StartTransaction("transaction_name")
defer txn.End()

// req is a *http.Request, this marks the transaction as a web transaction
txn.SetWebRequestHTTP(req)

// writer is a http.ResponseWriter, use the returned writer in place of the original
writer = txn.SetWebResponse(writer)
writer.WriteHeader(500)
```

**Transaction example**

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

Before:

````go
func checkout() {
    // function code here
}
```

After:

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

````

## Monitor a transaction with multiple goroutines [#goroutines]

To monitor a transaction across multiple goroutines, use `Transaction.NewGoroutine()`. The `NewGoroutine` method returns a new reference to the Transaction, which is required by each segment-creating goroutine. It does not matter if you call `NewGoroutine` before or after the other goroutine starts.

All `Transaction` methods can be used in any `Transaction` reference. The `Transaction`will end when `End()` is called in any goroutine.

**Multiple goroutines example**

The below example passes a new `Transaction` reference directly to another goroutine:

````go
go func(txn *newrelic.Transaction) {
    defer txn.StartSegment("async").End()
    time.Sleep(100 * time.Millisecond)
}(txn.NewGoroutine())
```

The below example passes a new `Transaction` reference on a channel to another goroutine:

```go
ch := make(chan *newrelic.Transaction)
go func() {
    txn := <-ch
    defer txn.StartSegment("async").End()
    time.Sleep(100 * time.Millisecond)
}()
ch <- txn.NewGoroutine()
```

````

## Monitor a transaction by wrapping an HTTP handler [#http-handler-txns]

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](https://docs.newrelic.com/docs/agents/go-agent/get-started/new-relic-go-monitor-transactions#go-txn).

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

Before:

```go
http.HandleFunc("/users", usersHandler)
```

After:

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

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

To access the transaction in your handler, use the `newrelic.FromContext` API. Note this will only work for Go versions 1.7 and newer. For example:

```go
func myHandler(w http.ResponseWriter, r *http.Request) {
    txn := newrelic.FromContext(r.Context())
    txn.NoticeError(errors.New("my error message"))
}
```

## Monitor errors [#go-error-methods]

New Relic for Go captures errors in three different ways:

| If you want to...                                                                                                                       | Use this                                                                                                                                                                                                                                                                                                                                         |
| --------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Track errors and report any combination of message, class, and attributes                                                               | [`Transaction.NoticeError()`](https://github.com/newrelic/go-agent/blob/master/GUIDE.md#noticeerror)                                                                                                                                                                                                                                             |
| Track expected errors and report any combination of message, class, and attributes without triggering alerts or affecting error metrics | [`Transaction.NoticeExpectedError()`](https://github.com/newrelic/go-agent/blob/master/GUIDE.md#noticeerror)                                                                                                                                                                                                                                     |
| Report panics                                                                                                                           | Transactions ended with `defer` automatically record panics. [See the New Relic for Go GitHub documentation for more information](https://github.com/newrelic/go-agent/blob/master/GUIDE.md#panics). As of version 3.0.0, this feature must be specifically enabled by setting the `Config.ErrorCollector.RecordPanics` configuration to `true`. |
| Report error response codes                                                                                                             | Transactions automatically record errors above 400 and below 100. [See the New Relic for Go GitHub documentation for more information](https://github.com/newrelic/go-agent/blob/master/GUIDE.md#error-response-codes).                                                                                                                          |

## View logs for your APM and infrastructure data [#logs-context]

You can also bring your logs and application's data together to make troubleshooting easier and faster. With [logs in context](https://docs.newrelic.com/docs/logs/logs-context/configure-logs-context-go/), you can see log messages related to your errors and traces directly in your app's UI.

1.  From the **Errors** page, click on a trace to go to the [**Error details** page](https://docs.newrelic.com/docs/apm/apm-ui-pages/error-analytics/errors-page-find-fix-verify-problems).
2.  From the error details page, click **See logs**.
3.  To view details related to an individual log message, click directly on the message.

You can also see logs in context of your [infrastructure data](https://docs.newrelic.com/docs/logs/forward-logs/forward-your-logs-using-infrastructure-agent/), such as Kubernetes clusters. No need to switch to another UI.

## Additional transaction methods [#go-transactions-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 Godoc](https://godoc.org/github.com/newrelic/go-agent/v3/newrelic#Transaction).
