Skip to content

dapings/opentracing-gorm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

opentracing gorm

OpenTracing for GORM

Install

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

Usage

  1. Call otgorm.AddGORMCallbacks(db) with an *gorm.DB instance.
  2. Clone db db = otgorm.WithContext(ctx, db) with a span.

Example

package demo

import (
    "context"
    "testing"

    "github.com/jinzhu/gorm"
    otgorm "github.com/dapings/opentracing-gorm"
    "github.com/opentracing/opentracing-go"
)

var db *gorm.DB

func init() {
    db = initDB()
}

func initDB() *gorm.DB {
    liteDB, err := gorm.Open("sqlite3", ":memory:")
    if err != nil {
        panic(err)
    }
    // register callbacks must be called for a root instance of gorm.DB
    otgorm.AddGORMCallbacks(liteDB)
    return liteDB
}

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

    // clone db with proper context
    clonedDB := otgorm.WithContext(ctx, db)

    // sql query
    var p struct{}
    clonedDB.First(&p, 1)
}

func TestDemo((t *testing.T)) {
    Handler(context.Background())
}

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