sqltrace

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2022 License: Apache-2.0 Imports: 18 Imported by: 0

README

sqltrace

A low-code intrusion SQL tracing library, suitable for any relational database (Sqlite3, MySQL, Oracle, SQL Server, PostgreSQL, TiDB, etc.) and ORM libraries for various relational database (gorm, xorm, sqlx, etc.)

😜installation

go get -u github.com/chenquan/sqltrace

👏how to use

native

package main

import (
	"context"
	"database/sql"
	"fmt"

	"github.com/chenquan/sqltrace"
	"github.com/mattn/go-sqlite3"
	_ "github.com/mattn/go-sqlite3"
	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/trace"
)

func main() {
	// Create a sqlite3 driver with link tracking
	driver := sqltrace.NewDriver(sqltrace.Config{
		Name:           "sqlite3_trace",
		DataSourceName: "sqlite3",
		Endpoint:       "http://localhost:14268/api/traces",
		Sampler:        1,
		Batcher:        "jaeger",
	}, &sqlite3.SQLiteDriver{})
	defer sqltrace.StopAgent()

	// register new driver
	sql.Register("sqlite3_trace", driver)

	// open database
	db, err := sql.Open("sqlite3_trace", "identifier.sqlite")
	if err != nil {
		panic(err)
	}

	tracer := otel.GetTracerProvider().Tracer("sqlite3_trace")
	ctx, span := tracer.Start(context.Background(),
		"test",
		trace.WithSpanKind(trace.SpanKindClient),
	)
	defer span.End()

	db.ExecContext(ctx, `CREATE TABLE t
(
    age  integer,
    name TEXT
)`)
	db.ExecContext(ctx, "insert into t values (?,?)", 1, "chenquan")

	// transaction
	tx, err := db.BeginTx(ctx, nil)
	stmt, err := tx.PrepareContext(ctx, "select age+1 as age,name from t where age = ?;")
	stmt.QueryContext(ctx, 1)
	tx.Commit()

	rows, err := db.QueryContext(ctx, "select  age+1 as age,name from t;")
	for rows.Next() {
		var age int
		var name string
		err := rows.Scan(&age, &name)
		if err != nil {
			fmt.Println(err)
		}

		fmt.Println(age, name)
	}
}

gorm

package main

import (
	"context"
	"database/sql"
	"fmt"

	"github.com/chenquan/sqltrace"
	"github.com/mattn/go-sqlite3"
	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/trace"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

func main() {
	// Create a sqlite3 driver with link tracking
	driver := sqltrace.NewDriver(sqltrace.Config{
		Name:           "sqlite3_trace",
		DataSourceName: "sqlite3",
		Endpoint:       "http://localhost:14268/api/traces",
		Sampler:        1,
		Batcher:        "jaeger",
	}, &sqlite3.SQLiteDriver{})
	defer sqltrace.StopAgent()

	// register new driver
	sql.Register("sqlite3_trace", driver)

	db, err := gorm.Open(sqlite.Dialector{
		DriverName: "sqlite3_trace",
		DSN:        "identifier.sqlite",
	}, &gorm.Config{})
	if err != nil {
		panic(err)
	}
	tracer := otel.GetTracerProvider().Tracer("sqlite3_trace")
	ctx, span := tracer.Start(context.Background(),
		"test",
		trace.WithSpanKind(trace.SpanKindClient),
	)
	defer span.End()

	type T struct {
		Age  int    `db:"age"`
		Name string `db:"name"`
	}
	db = db.WithContext(ctx)
	db.Migrator().AutoMigrate(&T{})
	db.Create(&T{
		Age:  1,
		Name: "chenquan",
	})

	rows, err := db.Raw("select  age+1 as age,name from ts;").Rows()
	if err != nil {
		panic(err)
	}

	for rows.Next() {
		var age int
		var name string
		err := rows.Scan(&age, &name)
		if err != nil {
			fmt.Println(err)
		}

		fmt.Println(age, name)
	}
	db.Commit()
}


⭐star

If you like or are using this project to learn or start your solution, please give it a star⭐. Thanks!

Documentation

Index

Constants

View Source
const (
	Jaeger = "jaeger"
	Zipkin = "zipkin"
)

Variables

View Source
var (
	ErrUnknownExporter = errors.New("unknown exporter error")
)

Functions

func NewDriver

func NewDriver(c Config, d driver.Driver) driver.Driver

func StartAgent

func StartAgent(c Config)

StartAgent starts a opentelemetry agent.

func StopAgent

func StopAgent()

Types

type Config

type Config struct {
	Name           string
	DataSourceName string
	Endpoint       string
	Sampler        float64
	Batcher        string
}

Config represents a tracing configuration.

type Hook

type Hook struct {
	// contains filtered or unexported fields
}

func NewTraceHook

func NewTraceHook(c Config) *Hook

func (*Hook) AfterBeginTx

func (h *Hook) AfterBeginTx(ctx context.Context, _ driver.TxOptions, dd driver.Tx, err error) (context.Context, driver.Tx, error)

func (*Hook) AfterCommit

func (h *Hook) AfterCommit(ctx context.Context, err error) (context.Context, error)

func (*Hook) AfterConnect

func (h *Hook) AfterConnect(ctx context.Context, dc driver.Conn, err error) (context.Context, driver.Conn, error)

func (*Hook) AfterExecContext

func (h *Hook) AfterExecContext(ctx context.Context, _ string, _ []driver.NamedValue, r driver.Result, err error) (context.Context, driver.Result, error)

func (*Hook) AfterPrepareContext

func (h *Hook) AfterPrepareContext(ctx context.Context, _ string, s driver.Stmt, err error) (context.Context, driver.Stmt, error)

func (*Hook) AfterQueryContext

func (h *Hook) AfterQueryContext(ctx context.Context, _ string, _ []driver.NamedValue, rows driver.Rows, err error) (context.Context, driver.Rows, error)

func (*Hook) AfterRollback

func (h *Hook) AfterRollback(ctx context.Context, err error) (context.Context, error)

func (*Hook) AfterStmtExecContext

func (h *Hook) AfterStmtExecContext(ctx context.Context, _ string, _ []driver.NamedValue, r driver.Result, err error) (context.Context, driver.Result, error)

func (*Hook) AfterStmtQueryContext

func (h *Hook) AfterStmtQueryContext(ctx context.Context, _ string, _ []driver.NamedValue, rows driver.Rows, err error) (context.Context, driver.Rows, error)

func (*Hook) BeforeBeginTx

func (h *Hook) BeforeBeginTx(ctx context.Context, opts driver.TxOptions, err error) (context.Context, driver.TxOptions, error)

func (*Hook) BeforeCommit

func (h *Hook) BeforeCommit(ctx context.Context, err error) (context.Context, error)

func (*Hook) BeforeConnect

func (h *Hook) BeforeConnect(ctx context.Context, err error) (context.Context, error)

func (*Hook) BeforeExecContext

func (h *Hook) BeforeExecContext(ctx context.Context, query string, args []driver.NamedValue, err error) (context.Context, string, []driver.NamedValue, error)

func (*Hook) BeforePrepareContext

func (h *Hook) BeforePrepareContext(ctx context.Context, query string, err error) (context.Context, string, error)

func (*Hook) BeforeQueryContext

func (h *Hook) BeforeQueryContext(ctx context.Context, query string, args []driver.NamedValue, err error) (context.Context, string, []driver.NamedValue, error)

func (*Hook) BeforeRollback

func (h *Hook) BeforeRollback(ctx context.Context, err error) (context.Context, error)

func (*Hook) BeforeStmtExecContext

func (h *Hook) BeforeStmtExecContext(ctx context.Context, query string, args []driver.NamedValue, err error) (context.Context, []driver.NamedValue, error)

func (*Hook) BeforeStmtQueryContext

func (h *Hook) BeforeStmtQueryContext(ctx context.Context, query string, args []driver.NamedValue, err error) (context.Context, []driver.NamedValue, error)

Jump to

Keyboard shortcuts

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