otgorm

package module
v0.0.0-...-516a807 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2020 License: MIT Imports: 6 Imported by: 0

README

opentracing gorm

OpenTracing instrumentation for GORM.

Install

go get -u github.com/smacker/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 {
    db, err := gorm.Open("sqlite3", ":memory:")
    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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddGormCallbacks

func AddGormCallbacks(db *gorm.DB)

AddGormCallbacks adds callbacks for tracing, you should call SetSpanToGorm to make them work

func WithContext

func WithContext(ctx context.Context, db *gorm.DB) *gorm.DB

SetSpanToGorm sets span to gorm settings, returns cloned DB

Types

This section is empty.

Jump to

Keyboard shortcuts

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