gin/examples/new_relic/README.md
Jeremy Loy fece76d93f Add NewRelic middleware example. (#1526)
* Add NewRelic middleware example.

* Update go.mod

* Update main.go
2019-02-20 10:41:46 +08:00

1.0 KiB

The New Relic Go Agent provides a nice middleware for the stdlib handler signature. The following is an adaptation of that middleware for Gin.

const (
	// NewRelicTxnKey is the key used to retrieve the NewRelic Transaction from the context
	NewRelicTxnKey = "NewRelicTxnKey"
)

// NewRelicMonitoring is a middleware that starts a newrelic transaction, stores it in the context, then calls the next handler
func NewRelicMonitoring(app newrelic.Application) gin.HandlerFunc {
	return func(ctx *gin.Context) {
		txn := app.StartTransaction(ctx.Request.URL.Path, ctx.Writer, ctx.Request)
		defer txn.End()
		ctx.Set(NewRelicTxnKey, txn)
		ctx.Next()
	}
}

and in main.go or equivalent...

router := gin.Default()
cfg := newrelic.NewConfig(os.Getenv("APP_NAME"), os.Getenv("NEW_RELIC_API_KEY"))
app, err := newrelic.NewApplication(cfg)
if err != nil {
		log.Printf("failed to make new_relic app: %v", err)
} else {
		router.Use(adapters.NewRelicMonitoring(app))
}