Skip to content

Latest commit

 

History

History
51 lines (36 loc) · 1.06 KB

README.md

File metadata and controls

51 lines (36 loc) · 1.06 KB

opentracing gorm

OpenTracing instrumentation for GORM V2.

Install

go get -u github.com/lhypj/opentracing-gorm

Usage

  1. Call otgorm.AddGormCallbacks(db) with an instance of your *gorm.DB.
  2. Clone db db = otgorm.SetSpanToGorm(ctx, db) with a span.

Example:

var gDB *gorm.DB

func init() {
    gDB = initDB()
}

func initDB() *gorm.DB {
    instance, err := gorm.Open(mysql.New(mysql.Config{DSN: dsn}), &gorm.Config{})
    if err != nil {
        panic(err)
    }
    // register callbacks must be called for a root instance of your gorm.DB
    otgorm.AddGormCallbacks(db)
    return db
}

func Handler(ctx context.Context) {
    span, ctx := opentracing.StartSpanFromContext(ctx, "handler")
    defer span.Finish()

    // clone db with proper context
    db := otgorm.SetSpanToGorm(ctx, gDB)

    // sql query
    db.First
}

Call to the Handler function would create sql span with table name, sql method and sql statement as a child of handler span.

License

MIT