浏览代码

Add NewRelic middleware example. (#1526)

* Add NewRelic middleware example.

* Update go.mod

* Update main.go
Jeremy Loy 6 年之前
父节点
当前提交
fece76d93f
共有 3 个文件被更改,包括 73 次插入0 次删除
  1. 30 0
      examples/new_relic/README.md
  2. 42 0
      examples/new_relic/main.go
  3. 1 0
      go.mod

+ 30 - 0
examples/new_relic/README.md

@@ -0,0 +1,30 @@
+The [New Relic Go Agent](https://github.com/newrelic/go-agent) provides a nice middleware for the stdlib handler signature. 
+The following is an adaptation of that middleware for Gin.
+
+```golang
+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...
+```golang
+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))
+}
+ ```

+ 42 - 0
examples/new_relic/main.go

@@ -0,0 +1,42 @@
+package main
+
+import (
+	"log"
+	"net/http"
+	"os"
+
+	"github.com/gin-gonic/gin"
+	"github.com/newrelic/go-agent"
+)
+
+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()
+	}
+}
+
+func main() {
+	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(NewRelicMonitoring(app))
+	}
+
+	router.GET("/", func(c *gin.Context) {
+		c.String(http.StatusOK, "Hello World!\n")
+	})
+	router.Run()
+}

+ 1 - 0
go.mod

@@ -24,6 +24,7 @@ exclude (
 	github.com/gin-gonic/autotls v0.0.0-20190119125636-0b5f4fc15768
 	github.com/gin-gonic/autotls v0.0.0-20190119125636-0b5f4fc15768
 	github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15
 	github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15
 	github.com/manucorporat/stats v0.0.0-20180402194714-3ba42d56d227
 	github.com/manucorporat/stats v0.0.0-20180402194714-3ba42d56d227
+	github.com/newrelic/go-agent v2.5.0+incompatible
 	github.com/thinkerou/favicon v0.1.0
 	github.com/thinkerou/favicon v0.1.0
 	golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b
 	golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b
 	golang.org/x/lint v0.0.0-20181217174547-8f45f776aaf1
 	golang.org/x/lint v0.0.0-20181217174547-8f45f776aaf1