nrgorm

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2017 License: MIT Imports: 4 Imported by: 0

README

NrGorm

Build Status GoDoc Go project version license

Example

Simple app
var db *gorm.DB
var nrapp newrelic.Application

func main() {
	db = initDB()
	defer db.Close()

	nrapp = initNrApp()

	http.HandleFunc("/", handler)
	http.ListenAndServve(":8080", nil)
}

func initNrApp() newrelic.Application {
	cfg := newrelic.NewConfig("Example App", "__YOUR_NEW_RELIC_LICENSE_KEY__")

	app, err := newrelic.NewApplication(cfg)
	if err != nil {
		panic(err)
	}

	return app
}

func initDB() *gorm.DB {
	db, err := gorm.Open("postgres", "postgres://postgres:@localhost/maindb?sslmode=disable")
	if err != nil {
		panic(err)
	}

	// Register callback funcs to *gorm.DB
	nrgorm.RegsiterCallbacks(db, "maindb")

	return db
}

func handler(w http.ResponseWriter, r *http.Request) {
	txn := nrapp.StartTransaction("/", w, r)
	defer txn.End()

	// Set newrelic.Transaction to *gorm.DB
	db = nrgorm.Wrap(txn, db)

	// do something...
}
With Gin Web Framework and nrgin
var db *gorm.DB

func WrapDB() gin.HandlerFunc {
	return func(c *gin.Context) {
		db = nrgorm.Wrap(db, nrgin.Transcation(c))
	}
}

func main() {
	r := gin.Default()
	db = initDB()
	r.Use(nrgin.Middleware(initNrApp()))
	r.Use(WrapDB())
	r.GET("/", func(c *gin.Context) {
		// do something...
	})
	r.Run() // listen and serve on 0.0.0.0:8080
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterCallbacks

func RegisterCallbacks(db *gorm.DB, dbName string)

RegisterCallbacks register before and after callbacks of each operations to gorm.DB object

func Wrap

func Wrap(txn newrelic.Transaction, db *gorm.DB) *gorm.DB

Wrap sets newrelic transaction instance to gorm.DB object

func Wrapped

func Wrapped(db *gorm.DB) bool

Wrapped returns true if given gorm.DB object has been set newrelic transaction

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL