• /
  • Log in

Instrument Go segments

With New Relic for Go, you can monitor the specific segments of a transaction in a Go application to get more detail about specific functions or code blocks.

Measure time for functions and code blocks

Segments are the specific parts of a transaction in an application. By instrumenting segments, you can measure the time taken by functions and code blocks, such as external calls, datastore calls, adding messages to queues, and background tasks.

Example: You have a transaction associated with a checkout process, which processes both shipping information and credit card information. You could instrument your application to break that transaction up into two pieces: one segment for shipping and one segment for payment.

Block-of-code segments

Once you instrument a transaction, you are ready to instrument one or more segments in that transaction.

To instrument an arbitrary block of code as a segment, use the following pattern, and include txn as the variable name set for the transaction:

segment := newrelic.Segment{}
segment.Name = "mySegmentName"
segment.StartTime = txn.StartSegmentNow()
// ... code you want to time here ...
segment.End()

StartSegment is a convenient helper. It creates a segment and starts it:

segment := txn.StartSegment("mySegmentName")
// ... code you want to time here ...
segment.End()

Function segments

Instrumenting a function as a segment is essentially the same as instrumenting an arbitrary block of code as a segment. The main difference is that, because a function has a discrete ending, you can use Go's defer statement.

To instrument a function as a segment, add the following code at the start of the function, and include txn as the variable name set for the transaction:

defer txn.StartSegment("mySegmentName").End()

Nest segments

Segments can be nested. The segment being ended must be the most recently started segment.

Here's an example of a segment starting and ending inside another segment:

s1 := txn.StartSegment("outerSegment")
s2 := txn.StartSegment("innerSegment")
// s2 must end before s1
s2.End()
s1.End()

A zero value segment may safely be ended. Therefore, the following code is safe even if the conditional fails:

var s newrelic.Segment
if recordSegment {
s.StartTime = txn.StartSegmentNow(),
}
// ... code you wish to time here ...
s.End()

Datastore segments

You can instrument Go application datastore calls. Datastore segments appear in the APM Transactions breakdown table and Databases tab of the Transactions page in New Relic.

If you are using a MySQL, PostgreSQL, or SQLite database driver, the easiest way to add Datastore segments is to use our pre-built integration packages. Otherwise, you can manually create Datastore segments for each database call.

External segments

You can instrument Go application calls to external services, such as web services, resources in the cloud, and any other network calls. External segments appear in the APM Transactions breakdown table and the External services page in New Relic.

There are two ways to instrument external segments:

Message producer segments

You can instrument Go application calls that add messages to queuing systems like RabbitMQ and Kafka. Message producer segments appear in the APM Transactions breakdown in New Relic.

There is only one way to instrument message producer segments:

For more help

If you need more help, check out these support and learning resources:

Create issueEdit page
Copyright © 2021 New Relic Inc.