github.com/rotblauer/buffalo@v0.7.1-0.20170112214545-7aa55ef80dd3/middleware/new_relic.go (about)

     1  package middleware
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/gobuffalo/buffalo"
     7  	newrelic "github.com/newrelic/go-agent"
     8  )
     9  
    10  // NewRelic returns a piece of buffalo.Middleware that can
    11  // be used to report requests to NewRelic. You must pass in your
    12  // NewRelic key and a name for your application. If the key
    13  // passed in is blank, i.e. loading from an ENV, then the middleware
    14  // is skipped and the chain continues on like normal. Useful
    15  // for development.
    16  func NewRelic(key, name string) buffalo.MiddlewareFunc {
    17  	mf := func(next buffalo.Handler) buffalo.Handler {
    18  		return next
    19  	}
    20  	if key == "" {
    21  		return mf
    22  	}
    23  	fmt.Printf("Setting up New Relic %s\n", key)
    24  	config := newrelic.NewConfig(name, key)
    25  	app, err := newrelic.NewApplication(config)
    26  	if err != nil {
    27  		return mf
    28  	}
    29  
    30  	return func(next buffalo.Handler) buffalo.Handler {
    31  		return func(c buffalo.Context) error {
    32  			tx := app.StartTransaction(c.Request().URL.String(), c.Response(), c.Request())
    33  			defer tx.End()
    34  			return next(c)
    35  		}
    36  	}
    37  }