Skip to content
Myr

Add NewRelic middleware example. (#1526)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Add NewRelic middleware example. * Update go.mod * Update main.go

authored by Jeremy Loy · committed by 田欧 · parent 5846ceb · Browse files · 3 files+73 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+73 added−0 removed

  • other:+73 added−0 removed

3 files+73 added−0 removed

added:examples/new_relic/main.go+42 added
1package main23import (4 "log"5 "net/http"6 "os"78 "github.com/gin-gonic/gin"9 "github.com/newrelic/go-agent"10)1112const (13 // NewRelicTxnKey is the key used to retrieve the NewRelic Transaction from the context14 NewRelicTxnKey = "NewRelicTxnKey"15)1617// NewRelicMonitoring is a middleware that starts a newrelic transaction, stores it in the context, then calls the next handler18func NewRelicMonitoring(app newrelic.Application) gin.HandlerFunc {19 return func(ctx *gin.Context) {20 txn := app.StartTransaction(ctx.Request.URL.Path, ctx.Writer, ctx.Request)21 defer txn.End()22 ctx.Set(NewRelicTxnKey, txn)23 ctx.Next()24 }25}2627func main() {28 router := gin.Default()2930 cfg := newrelic.NewConfig(os.Getenv("APP_NAME"), os.Getenv("NEW_RELIC_API_KEY"))31 app, err := newrelic.NewApplication(cfg)32 if err != nil {33 log.Printf("failed to make new_relic app: %v", err)34 } else {35 router.Use(NewRelicMonitoring(app))36 }3738 router.GET("/", func(c *gin.Context) {39 c.String(http.StatusOK, "Hello World!\n")40 })41 router.Run()42}
added:examples/new_relic/README.md+30 added
modified:go.mod+1 added
22 unmodified lines
23 github.com/dustin/go-broadcast v0.0.0-20171205050544-f664265f5a6624 github.com/gin-gonic/autotls v0.0.0-20190119125636-0b5f4fc1576825 github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e1526 github.com/manucorporat/stats v0.0.0-20180402194714-3ba42d56d22727 github.com/newrelic/go-agent v2.5.0+incompatible28 github.com/thinkerou/favicon v0.1.029 golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b30 golang.org/x/lint v0.0.0-20181217174547-8f45f776aaf131 google.golang.org/grpc v1.18.0
1 unmodified line