This example demonstrates how to integrate New Relic with a Gin web application.
- Go 1.16 or later
- A New Relic account and API key
- Clone the repository and navigate to the
new_relic
directory:
git clone https://github.com/your-repo/gin-examples.git
cd gin-examples/new_relic
- Install the dependencies:
go mod tidy
- Set the environment variables for your New Relic application:
export NEW_RELIC_APP_NAME="YourAppName"
export NEW_RELIC_LICENSE_KEY="YourNewRelicLicenseKey"
To run the application, use the following command:
go run main.go
The application will start a web server on http://localhost:8080
. You can access it in your browser to see the "Hello World!" message.
The main components of the application are:
- Gin Router: The web framework used to handle HTTP requests.
- New Relic Application: The New Relic agent used to monitor the application.
package main
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/newrelic/go-agent/v3/integrations/nrgin"
newrelic "github.com/newrelic/go-agent/v3/newrelic"
)
func main() {
router := gin.Default()
app, err := newrelic.NewApplication(
newrelic.ConfigAppName("MyApp"),
newrelic.ConfigFromEnvironment(),
)
if err != nil {
log.Fatalf("failed to make new_relic app: %v", err)
}
router.Use(nrgin.Middleware(app))
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello World!\n")
})
router.Run()
}
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please see the CONTRIBUTING file for more information.