Categorygithub.com/newrelic/go-agent
repository
3.41.0
Repository: https://github.com/newrelic/go-agent.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

Community Plus header

New Relic Go Agent GoDoc Go Report Card codecov

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.

Go is a compiled language, and doesn’t use a virtual machine. This means that setting up New Relic for your Golang app requires you to use our Go agent API and manually add New Relic methods to your source code. Our API provides exceptional flexibility and control over what gets instrumented.

Installation

Compatibility and Requirements

For the latest version of the agent, Go 1.22+ is required.

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

Installing and using the Go agent

To install the agent, follow the instructions in our GETTING_STARTED document or our GUIDE.

We recommend instrumenting your Go code to get the maximum benefits from the New Relic Go agent. But we make it easy to get great data in couple of ways:

  • Even without adding instrumentation, just importing the agent and creating an application will provide useful runtime information about your number of goroutines, garbage collection statistics, and memory and CPU usage.
  • You can use our many INTEGRATION packages for out-of-the box support for many popular Go web frameworks and libraries. We continue to add integration packages based on your feedback. You can weigh in on potential integrations by opening an Issue here in our New Relic Go agent GitHub project.

Upgrading

If you have already been using version 2.X of the agent and are upgrading to version 3.0, see our MIGRATION guide for details.

Getting Started

v3/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 v3/examples/server/main.go

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

Usage

Integration Packages

The following integration packages extend the base newrelic package to support the following frameworks and libraries. Frameworks and databases which don't have an integration package may still be instrumented using the newrelic package primitives.

Service Frameworks

ProjectIntegration Package
gin-gonic/ginv3/integrations/nrginInstrument inbound requests through the Gin framework
gofiber/fiberv3/integrations/nrfiberInstrument inbound requests through the Fiber framework
gorilla/muxv3/integrations/nrgorillaInstrument inbound requests through the Gorilla framework
google.golang.org/grpcv3/integrations/nrgrpcInstrument gRPC servers and clients
labstack/echov3/integrations/nrecho-v3Instrument inbound requests through version 3 of the Echo framework
labstack/echov3/integrations/nrecho-v4Instrument inbound requests through version 4 of the Echo framework
julienschmidt/httprouterv3/integrations/nrhttprouterInstrument inbound requests through the HttpRouter framework
micro/go-microv3/integrations/nrmicroInstrument servers, clients, publishers, and subscribers through the Micro framework
connectrpc/connect-gov3/integrations/nrconnectInstrument Connect servers and clients

Datastores

More information about instrumenting databases without an integration package using newrelic package primitives can be found here.

ProjectIntegration Package
lib/pqv3/integrations/nrpqInstrument PostgreSQL driver (pq driver for database/sql)
jackc/pgxv3/integrations/nrpgxInstrument PostgreSQL driver (pgx driver for database/sql)
jackc/pgx/v5v3/integrations/nrpgx5Instrument PostgreSQL driver (pgx/v5 driver for database/sql)
go-mssqldbv3/integrations/nrmssqlInstrument MS SQL driver
go-sql-driver/mysqlv3/integrations/nrmysqlInstrument MySQL driver
elastic/go-elasticsearchv3/integrations/nrelasticsearch-v7Instrument Elasticsearch datastore calls
database/sqlUse a supported database driver or builtin instrumentationInstrument database calls with SQL
jmoiron/sqlxUse a supported database driver or builtin instrumentationInstrument database calls with SQLx
go-redis/redisv3/integrations/nrredis-v7Instrument Redis 7 calls
go-redis/redisv3/integrations/nrredis-v8Instrument Redis 8 calls
redis/go-redisv3/integrations/nrredis-v9Instrument Redis 9 calls
mattn/go-sqlite3v3/integrations/nrsqlite3Instrument SQLite driver
snowflakedb/gosnowflakev3/integrations/nrsnowflakeInstrument Snowflake driver
mongodb/mongo-go-driverv3/integrations/nrmongoInstrument MongoDB calls

AI

ProjectIntegration Package
sashabaranov/go-openaiv3/integrations/nropenaiSend AI Monitoring Events with OpenAI
aws/aws-sdk-go-v2/tree/main/service/bedrockruntimev3/integrations/nrawsbedrockSend AI Monitoring Events with AWS Bedrock

Agent Logging

ProjectIntegration Package
sirupsen/logrusv3/integrations/nrlogrusSend agent log messages to Logrus
mgutz/logxiv3/integrations/nrlogxiSend agent log messages to Logxi
uber-go/zapv3/integrations/nrzapSend agent log messages to Zap
log/slogv3/integrations/nrslogSend agent log messages to log/slog
rs/zerologv3/integrations/nrzerologSend agent log messages to Zerolog

Logs in Context

ProjectIntegration Package
sirupsen/logrusv3/integrations/logcontext-v2/nrlogrusSend data collected from Logrus log messages to New Relic
logv3/integrations/logcontext-v2/logWriterSend data collected from the standard library logger log messages to New Relic
rs/zerologv3/integrations/logcontext-v2/zerologWriterSend data collected from zerolog log messages to New Relic

AWS

ProjectIntegration Package
aws/aws-sdk-gov3/integrations/nrawssdk-v1Instrument outbound calls made using Go AWS SDK
aws/aws-sdk-go-v2v3/integrations/nrawssdk-v2Instrument outbound calls made using Go AWS SDK v2
aws/aws-lambda-gov3/integrations/nrlambdaInstrument AWS Lambda applications

GraphQL

ProjectIntegration Package
graph-gophers/graphql-gov3/integrations/nrgraphgophersInstrument inbound requests using graph-gophers/graphql-go
graphql-go/graphqlv3/integrations/nrgraphqlgoInstrument inbound requests using graphql-go/graphql

Misc

ProjectIntegration Package
pkg/errorsv3/integrations/nrpkgerrorsWrap pkg/errors errors to improve stack traces and error class information
openzipkin/b3-propagationv3/integrations/nrb3Add B3 headers to outgoing requests
nats-io/nats.gov3/integrations/nrnatsInstrument publishers and subscribers using the NATS client
nats-io/stan.gov3/integrations/nrstanInstrument publishers and subscribers using the NATS streaming client

These integration packages must be imported along with the newrelic package, as shown in this nrgin example.

Alternatives

If you are already using another open source solution to gather telemetry data, you may find it easier to use one of our open source exporters to send this data to New Relic:

Go Agent Development

This section describes the suggested workflow for developers contributing to the Go Agent.

The Makefile

At the root directory is a Makefile that contains several helpful targets for building containers and running tests.

Docker

Docker is a useful development tool that allows for the quick, easy and repeatable creation of virtual environments.

Docker Compose builds on the functionality provided by Docker to control the orchestration of multi-container environments.

The development workflow described in this section relies heavily Docker. The containers that are generated will create a volume mounted to the current directory on the local host, allowing the modification of code and tests on the local host to be propagated automatically to the running container without having to re-build or copy manually.

Targets

TargetDescription
devenv-imageBuild the dev image(s) specified under the docker compose dev profile.
dev-shellRun docker compose with the dev profile. Open a shell in the running container.
dev-stopTerminate the running containers under the docker compose dev profile.
test-services-startSpin up agent and other containers.
test-services-stopTerminate agent and other containers.
core-testRun a specified core test.
core-suiteRun all core tests.
integration-testRun a specified integration test.
integration-suiteRun all integration tests.
tidyReplace github.com New Relic Go Agent Module with the local copy; run go mod tidy.
infoDisplay information about the running Go environment, integration tests, and core tests.

Usage

Build and run the agent container and database container(s). Execute a shell into the agent container environment:

make dev-shell

Terminate the container(s) started by dev-shell:

make dev-stop

Spin up services for testing. Does not create a shell into the container. May specify an optional PROFILE argument to start another container not covered by the default test profile: Note: When running the integration-suite against test-services containers, all accessory PROFILEs must be specified or the tests will fail. This may require editing the docker-compose.yml file. It is recommended to instead use dev-shell for this kind of use case, as the dev profile will start every integration container by default.

make test-services-start <PROFILE=profile-name>

Terminate the containers started by the test-services-start target:

make test-services-stop <PROFILE=profile-name>

Run a core test. May specify optional COVERAGE argument to generate a coverprofile:

make core-test TEST=test-name <COVERAGE=1>

Run all core tests:

make core-suite <COVERAGE=1>

Run an integration test. May specify optional COVERAGE argument to generate a coverprofile:

make integration-test TEST=test-name <COVERAGE=1>

Run all integration tests:

make integration-suite <COVERAGE=1>

Example (dev-shell)

An example development workflow where the developer is testing changes to utilization functionality and the nrpgx5 integration might look like the following:

From the top-level checkout of the go-agent, start the containers and open a shell into the agent container:

local:go-agent user$ make dev-shell
[+] Building 0/0
...Docker build output...
[+] Building 1/1 docker.io/library/go-agent-devenv                                   0.0s
 ✔ Service devenv  Built                                                             0.8s
docker compose --profile dev up --pull missing --remove-orphans -d
[+] Running 2/2
 ✔ Container go-agent-postgres-1  Started                                            0.2s
 ✔ Container go-agent-devenv-1    Started                                            0.2s
docker compose exec -it devenv bash -c "bash"
root@5a832b6fcba3:/usr/src/app/go-agent#

Testing the utilization package:

root@5a832b6fcba3:/usr/src/app/go-agent# make core-test TEST=internal/utilization

# TEST=internal/utilization, COVERAGE=
go: downloading packages...
PASS
ok      github.com/newrelic/go-agent/v3/internal/utilization    0.116s
# TEST=internal/utilization
root@5a832b6fcba3:/usr/src/app/go-agent#

Testing the nrpgx5 integration:

root@5a832b6fcba3:/usr/src/app/go-agent# make integration-test TEST=nrpgx5

# TEST=nrpgx5, COVERAGE=
go: downloading packages...
PASS
ok      github.com/newrelic/go-agent/v3/integrations/nrpgx5     1.100s
# TEST=nrpgx5
root@5a832b6fcba3:/usr/src/app/go-agent#

Make the necessary code changes, and test using the steps above until satisfied.

Once done with development, terminate the running containers:

root@5a832b6fcba3:/usr/src/app/go-agent# exit
exit
local:go-agent user$ make dev-stop
docker compose --profile dev stop
[+] Stopping 2/2
 ✔ Container go-agent-devenv-1    Stopped                                           10.1s
 ✔ Container go-agent-postgres-1  Stopped                                            0.1s
local:go-agent user$

Example (test-services)

The test-services-* targets are designed to allow developers to run tests without needing to execute a shell into the agent container. Re-using the scenario described above in dev-shell, this workflow might look like the following:

From the top-level checkout of the go-agent:

local:go-agent user$ make test-services-start PROFILE=nrpgx5
[+] Pulling 16/16
 ✔ go Skipped - No image to be pulled                                                0.0s
 ✔ postgres Pulled                                                                   7.0s
...Docker build output...
 ✔ Service go                     Built                                              0.9s
 ✔ Container nr-go                Healthy                                            1.1s
 ✔ Container go-agent-postgres-1  Healthy                                            1.1s
local:go-agent user$

Note: the specification of PROFILE=nrpgx5. This ensures that the postgres container is started alongside the agent container in order to test the nrpgx5 integration. This is not necessary if you do not intend to run postgres tests.

Testing the utilization package:

local:go-agent user$ docker exec -e TEST=internal/utilization nr-go make core-test

# TEST=internal/utilization, COVERAGE=
go: downloading packages...
PASS
ok      github.com/newrelic/go-agent/v3/internal/utilization    0.105s
# TEST=internal/utilization
local:go-agent user$

Let's quickly break down the above docker command:

  1. docker exec executes a command in a running container.
  2. -e TEST=internal/utilization passes the TEST argument as an environment variable.
  3. nr-go is the name of the container where the command will be executed.
  4. make core-test is the command to be executed.

The same logic applies to running integration tests:

local:go-agent user$ docker exec -e TEST=nrpgx5 nr-go make integration-test

# TEST=nrpgx5, COVERAGE=
go: downloading packages...
PASS
ok      github.com/newrelic/go-agent/v3/integrations/nrpgx5     1.095s
# TEST=nrpgx5
local:go-agent user$

Like the dev-shell target, the test-services agent container is volume-mounted to the local working directory containing the agent checkout. Code and test changes can be made and will automatically propagate to the container environment.

To terminate the running containers:

local:go-agent user$ make test-services-stop PROFILE=nrpgx5
[+] Stopping 2/2
 ✔ Container go-agent-postgres-1  Stopped                                            0.1s
 ✔ Container nr-go                Stopped                                           10.1s
local:go-agent user$

Adding new tests or moving existing tests

When adding or moving tests, any changes must be reflected in the corresponding integration-tests.mk / core-tests.mk files.

core-tests.mk contains all tests under the v3/newrelic and v3/internal directories and subdirectories.

integration-tests.mk contains all tests under the v3/integrations directory and subdirectories.

Development with different Go Versions

Docker Compose uses the latest Go Version for the agent container by default. At the time of writing, this is 1.24. This value can be changed by exporting a GO_VERSION environment variable or top-level .env file containing the GO_VERSION definition.

Example using Go 1.23:

export GO_VERSION=1.23

Then start docker services as normal.

Support

Should you need assistance with New Relic products, you are in good hands with several support channels.

If the issue has been confirmed as a bug or is a Feature request, please file a Github issue.

Privacy

At New Relic we take your privacy and the security of your information seriously, and are committed to protecting your information. We must emphasize the importance of not sharing personal data in public forums, and ask all users to scrub logs and diagnostic information for sensitive information, whether personal, proprietary, or otherwise.

We define "Personal Data" as any information relating to an identified or identifiable individual, including, for example, your name, phone number, post code or zip code, Device ID, IP address and email address.

For more information, review New Relic’s General Data Privacy Notice.

Contribute

We encourage your contributions to improve the Go Agent! Keep in mind when you submit your pull request, you'll need to sign the CLA via the click-through using CLA-Assistant. You only have to sign the CLA one time per project.

If you have any questions, or to execute our corporate CLA, required if your contribution is on behalf of a company, please drop us an email at [email protected].

A note about vulnerabilities

As noted in our security policy, New Relic is committed to the privacy and security of our customers and their data. We believe that providing coordinated disclosure by security researchers and engaging with the security community are important means to achieve our security goals.

If you believe you have found a security vulnerability in this project or any of New Relic's products or websites, we welcome and greatly appreciate you reporting it to New Relic through HackerOne.

If you would like to contribute to this project, please review these guidelines.

To all contributors, we thank you! Without your contribution, this project would not be what it is today. We also host a community project page dedicated to the Go Agent.

License

The New Relic Go agent is licensed under the Apache 2.0 License.