otgorm

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SQLSpanOpName = "sql"
)

Functions

func AddGORMCallbacks

func AddGORMCallbacks(db *gorm.DB)

AddGORMCallbacks adds callbacks for tracing, should call SetSpanToGORM to make them work.

func SetSpanToGORM

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

SetSpanToGORM sets span to gorm settings, returns cloned DB.

func WithContext

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

WithContext 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