Go Gin
Prerequisites
New Relic Go agent requires Golang 1.17+
Installation
Following are the steps to install the New Relic agent and connect it with CubeAPM. If New Relic agent is already installed, you can jump to step 4.
-
Install dependencies
go get github.com/newrelic/go-agent/v3/newrelic \
github.com/newrelic/go-agent/v3/integrations/nrgin -
Add the highlighted lines below to your project's
main.go
file:package main
import "github.com/newrelic/go-agent/v3/integrations/nrgin"
import "github.com/newrelic/go-agent/v3/newrelic"
func main() {
// initialize newrelic
app, err := newrelic.NewApplication(
newrelic.ConfigFromEnvironment(),
)
// Create Gin router
router := gin.Default()
router.Use(nrgin.Middleware(app))
}infoFor more information, refer to the NewRelic Go documentation.
-
Configure the agent.
- Environment Variables
- Code
# Use `newrelic.ConfigFromEnvironment()` as shown in step 2 to load these environment variables.
NEW_RELIC_APP_NAME=<app_name>
NEW_RELIC_LICENSE_KEY=ABC4567890ABC4567890ABC4567890ABC4567890func main() {
// initialize newrelic
app, err := newrelic.NewApplication(
newrelic.ConfigAppName("<app_name>"),
newrelic.ConfigLicense("ABC4567890ABC4567890ABC4567890ABC4567890"),
)
} -
Set up a proxy for New Relic agent.
New Relic agent has a limitation: it only send data over HTTPS on port 443. However, CubeAPM receives data on port 3130 over HTTP. To bridge this gap, you need to set up a proxy (load balancer or HTTP reverse proxy) that:
- Accepts HTTPS traffic on port 443 from the New Relic agent
- Forwards this traffic to your CubeAPM server on port 3130 over HTTP
For example, you can set up an AWS Application Load Balancer (ALB) with the following configuration:
-
Listener Configuration:
- Protocol: HTTPS
- Port: 443
- Domain name:
cubeapm-newrelic.yourdomain.com
(example domain for your load balancer) - SSL/TLS Certificate: Certificate for
cubeapm-newrelic.yourdomain.com
or\*.yourdomain.com
(from ACM or imported)
-
Target Group Configuration:
- Protocol: HTTP
- Port: 3130
- Target: Your CubeAPM server IP or DNS name (e.g.,
cubeapm-server.yourdomain.com
) - Health check path:
/health
-
Security Groups:
-
Load Balancer Security Group:
- Inbound rule: Allow HTTPS (port 443) from your application servers' IPs or security groups
- Outbound rule: Allow HTTP (port 3130) to the CubeAPM server's IP or security group
-
CubeAPM Server Security Group:
- Inbound rule: Allow HTTP (port 3130) from the load balancer's security group
- Outbound rule: Allow all traffic (or as per your security requirements)
-
The New Relic agent will connect to the load balancer's domain name (
cubeapm-newrelic.yourdomain.com
in this example), which will then forward the traffic to your CubeAPM server on port 3130.infoFor more details on proxy setup options, refer to the New Relic integration documentation.
-
Tell the agent to connect with CubeAPM instead of New Relic:
- Environment Variables
- Code
NEW_RELIC_APP_NAME=<app_name>
NEW_RELIC_LICENSE_KEY=ABC4567890ABC4567890ABC4567890ABC4567890
# Use your load balancer's domain name here
NEW_RELIC_HOST=<cubeapm-newrelic.yourdomain.com>func main() {
// initialize newrelic
app, err := newrelic.NewApplication(
newrelic.ConfigAppName("<app_name>"),
newrelic.ConfigLicense("ABC4567890ABC4567890ABC4567890ABC4567890"),
func(c *newrelic.Config) {
c.Host = "<cubeapm-newrelic.yourdomain.com>" // Use your load balancer's domain name here
},
)
}
Sample App
A working example with multiple instrumentations is available at https://github.com/cubeapm/sample_app_go_gin/tree/newrelic
Troubleshooting
The following can be used for debugging:
- Environment Variables
- Code
# Print New Relic agent logs on screen
NEW_RELIC_LOG=stdout
# Set New Relic agent log level to debug if needed to see detailed logs
NEW_RELIC_LOG_LEVEL=debug
app, err := newrelic.NewApplication(
newrelic.ConfigDebugLogger(os.Stdout),
)