sql

package module
v0.0.0-...-1db7dc6 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2023 License: Apache-2.0 Imports: 10 Imported by: 2

README

Go2Sky with database/sql

Installation

go get -u github.com/SkyAPM/go2sky-plugins/sql

Usage

import (
	sqlPlugin "github.com/SkyAPM/go2sky-plugins/sql"

	"github.com/SkyAPM/go2sky"
	"github.com/SkyAPM/go2sky/reporter"
	_ "github.com/go-sql-driver/mysql"
)

// init reporter
re, err := reporter.NewLogReporter()
defer re.Close()

// init tracer
tracer, err := go2sky.NewTracer("service-name", go2sky.WithReporter(re))
if err != nil {
    log.Fatalf("init tracer error: %v", err)
}

// use sql plugin to open db with tracer
db, err := sqlPlugin.Open("mysql", dsn, tracer,
    sqlPlugin.WithSqlDBType(sqlPlugin.MYSQL),
    sqlPlugin.WithQueryReport(),
    sqlPlugin.WithParamReport(),
    sqlPlugin.WithPeerAddr("127.0.0.1:3306"),
)
if err != nil {
	log.Fatalf("open db error: %v \n", err)
}

// use db handler as usual.

Documentation

Overview

Package sql is a plugin that can be used to trace database/sql.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupportedOp = errors.New("operation unsupported by the underlying driver")

ErrUnsupportedOp operation unsupported by the underlying driver

Functions

This section is empty.

Types

type Conn

type Conn struct {
	*sql.Conn
	// contains filtered or unexported fields
}

Conn wrap sql.Conn and support trace

func (*Conn) BeginTx

func (c *Conn) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

BeginTx support trace

func (*Conn) ExecContext

func (c *Conn) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext support trace

func (*Conn) PingContext

func (c *Conn) PingContext(ctx context.Context) error

PingContext support trace

func (*Conn) PrepareContext

func (c *Conn) PrepareContext(ctx context.Context, query string) (*Stmt, error)

PrepareContext support trace

func (*Conn) QueryContext

func (c *Conn) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext support trace

func (*Conn) QueryRowContext

func (c *Conn) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRowContext support trace

type DB

type DB struct {
	*sql.DB
	// contains filtered or unexported fields
}

DB wrap sql.DB and support trace

func Open

func Open(driverName, dataSourceName string, tracer *go2sky.Tracer, opts ...Option) (*DB, error)

Open support trace

func OpenDB

func OpenDB(c driver.Connector, tracer *go2sky.Tracer, opts ...Option) *DB

OpenDB support trace

func (*DB) BeginTx

func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

BeginTx support trace

func (*DB) Conn

func (db *DB) Conn(ctx context.Context) (*Conn, error)

Conn support trace

func (*DB) ExecContext

func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext support trace

func (*DB) PingContext

func (db *DB) PingContext(ctx context.Context) error

PingContext support trace

func (*DB) PrepareContext

func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error)

PrepareContext support trace

func (*DB) QueryContext

func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext support trace

func (*DB) QueryRowContext

func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRowContext support trace

type DBType

type DBType string

DBType database type

const (
	// UNKNOWN unknown database
	UNKNOWN DBType = "unknown"
	// MYSQL mysql
	MYSQL DBType = "mysql"
	// IPV4 others database type
	IPV4 DBType = "others"
)

type Option

type Option func(*options)

Option set plugin option

func WithParamReport

func WithParamReport() Option

WithParamReport if set, the parameters of the sql would be collected

func WithPeerAddr

func WithPeerAddr(addr string) Option

WithPeerAddr set the peer address to report

func WithQueryReport

func WithQueryReport() Option

WithQueryReport if set, the sql would be collected

func WithSQLDBType

func WithSQLDBType(t DBType) Option

WithSQLDBType set dbType option, dbType is used for parsing dsn string to peer address and setting componentID, if DB type is not support in DBType list, please use WithPeerAddr to set peer address manually

type Stmt

type Stmt struct {
	*sql.Stmt
	// contains filtered or unexported fields
}

Stmt wrap sql.Stmt and support trace

func (*Stmt) ExecContext

func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error)

ExecContext support trace

func (*Stmt) QueryContext

func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*sql.Rows, error)

QueryContext support trace

func (*Stmt) QueryRowContext

func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *sql.Row

QueryRowContext support trace

type Tx

type Tx struct {
	*sql.Tx
	// contains filtered or unexported fields
}

Tx wrap sql.Tx and support trace

func (*Tx) Commit

func (tx *Tx) Commit() (err error)

Commit support trace

func (*Tx) Exec

func (tx *Tx) Exec(query string, args ...interface{}) (sql.Result, error)

Exec support trace

func (*Tx) ExecContext

func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext support trace

func (*Tx) Prepare

func (tx *Tx) Prepare(query string) (*Stmt, error)

Prepare support trace

func (*Tx) PrepareContext

func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error)

PrepareContext support trace

func (*Tx) Query

func (tx *Tx) Query(query string, args ...interface{}) (*sql.Rows, error)

Query support trace

func (*Tx) QueryContext

func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext support trace

func (*Tx) QueryRow

func (tx *Tx) QueryRow(query string, args ...interface{}) *sql.Row

QueryRow support trace

func (*Tx) QueryRowContext

func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRowContext support trace

func (*Tx) Rollback

func (tx *Tx) Rollback() (err error)

Rollback support trace

func (*Tx) StmtContext

func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt

StmtContext support trace

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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