New Relic Go Agent
Clone or download
astormnewrelic and GitHub Enterprise Fixing link (#263)
Latest commit 46d73e6 Aug 28, 2018
Permalink
Failed to load latest commit information.
_integrations Add Context functions (#200) Aug 27, 2018
examples Span Events (#240) Aug 27, 2018
internal Span Events (#240) Aug 27, 2018
.travis.yml Don't run golint on Go 1.6. Apr 27, 2018
CHANGELOG.md Fixing link (#263) Aug 28, 2018
CONTRIBUTING.md Fix broken markdown link Aug 5, 2016
GUIDE.md Address comments from docs team in GUIDE.md. (#261) Aug 28, 2018
LICENSE.txt Update license to address scope of usage Oct 9, 2017
README.md GoDoc badge (public GitHub PR request) (#194) Jan 12, 2018
application.go Implement RecordCustomMetric Oct 6, 2017
attributes.go Shortened attribute constant names for ease of use Aug 5, 2016
config.go Address comments from docs team in GUIDE.md. (#261) Aug 28, 2018
context.go Add Context functions (#200) Aug 27, 2018
context_stub.go Add Context functions (#200) Aug 27, 2018
datastore.go Move attributes and datastore to top level Aug 5, 2016
errors.go Add ErrorAttributes and Error types Oct 10, 2017
instrumentation.go Add Context functions (#200) Aug 27, 2018
internal_app.go Span Events (#240) Aug 27, 2018
internal_attributes_test.go Implement LASP (#202) Apr 27, 2018
internal_benchmark_test.go Implement Better CAT (#209) Aug 3, 2018
internal_config.go Implement LASP (#202) Apr 27, 2018
internal_config_test.go Span Events (#240) Aug 27, 2018
internal_cross_process_test.go Implement Better CAT (#209) Aug 3, 2018
internal_distributed_trace_test.go Span Events (#240) Aug 27, 2018
internal_errors_test.go Implement LASP (#202) Apr 27, 2018
internal_response_writer.go Split internal.go for convenience Aug 5, 2016
internal_slow_queries_test.go Make the segment functions operate by pointer, not by value. May 2, 2018
internal_synthetics_test.go Implement Better CAT (#209) Aug 3, 2018
internal_test.go Span Events (#240) Aug 27, 2018
internal_txn.go Span Events (#240) Aug 27, 2018
internal_txn_test.go Span Events (#240) Aug 27, 2018
log.go Improve logging documentation Jul 21, 2016
segments.go Make the segment functions operate by pointer, not by value. May 2, 2018
transaction.go Implement Better CAT (#209) Aug 3, 2018
version.go Changelog and version bump. (#262) Aug 28, 2018

README.md

New Relic Go Agent GoDoc

Description

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.

All pull requests will be reviewed by the New Relic product team. Any questions or issues should be directed to our support site or our community forum.

Requirements

Go 1.3+ is required, due to the use of http.Client's Timeout field.

Linux, OS X, and Windows (Vista, Server 2008 and later) are supported.

Getting Started

Here are the basic steps to instrumenting your application. For more information, see GUIDE.md.

Step 0: Installation

Installing the Go Agent is the same as installing any other Go library. The simplest way is to run:

go get github.com/newrelic/go-agent

Then import the github.com/newrelic/go-agent package in your application.

Step 1: Create a Config and an Application

In your main function or an init block:

config := newrelic.NewConfig("Your Application Name", "__YOUR_NEW_RELIC_LICENSE_KEY__")
app, err := newrelic.NewApplication(config)

more info, application.go, config.go

Step 2: Add Transactions

Transactions time requests and background tasks. Use WrapHandle and WrapHandleFunc to create transactions for requests handled by the http standard library package.

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

Alternatively, create transactions directly using the application's StartTransaction method:

txn := app.StartTransaction("myTxn", optionalResponseWriter, optionalRequest)
defer txn.End()

more info, transaction.go

Step 3: Instrument Segments

Segments show you where time in your transactions is being spent. At the beginning of important functions, add:

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

more info, segments.go

Runnable Example

examples/server/main.go is an example that will appear as "Example App" in your New Relic applications list. To run it:

env NEW_RELIC_LICENSE_KEY=__YOUR_NEW_RELIC_LICENSE_KEY__LICENSE__ \
    go run examples/server/main.go

Some endpoints exposed are http://localhost:8000/ and http://localhost:8000/notice_error

Basic Example

Before Instrumentation

package main

import (
	"io"
	"net/http"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, "hello, world")
}

func main() {
	http.HandleFunc("/", helloHandler)
	http.ListenAndServe(":8000", nil)
}

After Instrumentation

package main

import (
	"fmt"
	"io"
	"net/http"
	"os"

	"github.com/newrelic/go-agent"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, "hello, world")
}

func main() {
	// Create a config.  You need to provide the desired application name
	// and your New Relic license key.
	cfg := newrelic.NewConfig("Example App", "__YOUR_NEW_RELIC_LICENSE_KEY__")

	// Create an application.  This represents an application in the New
	// Relic UI.
	app, err := newrelic.NewApplication(cfg)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Wrap helloHandler.  The performance of this handler will be recorded.
	http.HandleFunc(newrelic.WrapHandleFunc(app, "/", helloHandler))
	http.ListenAndServe(":8000", nil)
}

Support

You can find more detailed documentation in the guide and on the New Relic Documentation site.

If you can't find what you're looking for there, reach out to us on our support site or our community forum and we'll be happy to help you.

Find a bug? Contact us via support.newrelic.com, or email support@newrelic.com.