How to Setup New Relic for a Golang Service?

Sumit Kandoi
3 min readJun 20, 2021

First of all we need to understand What is New Relic and why do we need it?

Answer : There is one interesting article on medium which really helps you understanding New Relic definition.

Now if we have a golang based service and we want to setup New Relic for it. How can we do this ?

We can use New Relic Go Agent for it. The New Relic Go Agent allows you to monitor your Go applications with New Relic. It helps you track transactions, outbound requests, database calls, and other parts of your Go application’s behavior and provides a running overview of garbage collection, goroutine activity, and memory use.

Step 0: Installation

The New Relic Go agent is a Go library. It has two dependencies on gRPC libraries — see go.mod. Install the Go agent the same way you would install any other Go library. The simplest way is to run:

go get github.com/newrelic/go-agent

Then import the package in your application:

import "github.com/newrelic/go-agent/v3/newrelic"

Step 1: Create an Application

In your main function, or an init block, create an Application using ConfigOptions. Available configurations are listed here. Application is the starting point for all instrumentation.

func main() {
// Create an Application:
app, err := newrelic.NewApplication(
// Name your application
newrelic.ConfigAppName("Your Application Name"),
// Fill in your New Relic license key
newrelic.ConfigLicense("__YOUR_NEW_RELIC_LICENSE_KEY__"),
// Add logging:
newrelic.ConfigDebugLogger(os.Stdout),
// Optional: add additional changes to your configuration via a config function:
func(cfg *newrelic.Config) {
cfg.CustomInsightsEvents.Enabled = false
},
)
// If an application could not be created then err will reveal why.
if err != nil {
fmt.Println("unable to create New Relic Application", err)
}
// Now use the app to instrument everything!
}

Now start your application, and within minutes it will appear in the New Relic UI. Your application in New Relic won’t contain much data (until we complete the steps below!), but you will already be able to see a Go runtime page that shows goroutine counts, garbage collection, memory, and CPU usage.

Step 2: Instrument Requests Using Transactions

Transactions are used to time inbound requests and background tasks. Use them to see your application's throughput and response time. The instrumentation strategy depends on the framework you're using:

Standard HTTP Library

If you are using the standard library http package, use WrapHandle and WrapHandleFunc. As an example, the following code:

http.HandleFunc("/users", usersHandler)

Can be instrumented like this:

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

Step 3: Instrument Segments

Segments show you where the time in your transactions is being spent. There are four types of segments: Segment, ExternalSegment, DatastoreSegment, and MessageProducerSegment.

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.

Creating a segment requires access to the transaction. You can pass the transaction around your functions inside a context.Context (preferred), or as an explicit transaction parameter of the function. Functions FromContext and NewContext make it easy to store and retrieve the transaction from a context.

You may not even need to add the transaction to the context: WrapHandle and WrapHandleFunc add the transaction to the request’s context automatically.

func instrumentMe(ctx context.Context) {
txn := newrelic.FromContext(ctx)
segment := txn.StartSegment("instrumentMe")
time.Sleep(1 * time.Second)
segment.End()
}
func myHandler(w http.ResponseWriter, r *http.Request) {
instrumentMe(r.Context())
}
func main() {
app, _ := newrelic.NewApplication(
newrelic.ConfigAppName("appName"),
newrelic.ConfigLicense("__license__"),
)
http.HandleFunc(newrelic.WrapHandleFunc(app, "/handler", myHandler))
}

Happy Coding !!!

--

--

Sumit Kandoi

I am a Senior Software Engineer @Gojek by profession involved in building highly scalable apps.