Skip to content

Commit

Permalink
Added function id into database/sql traces (start info's)
Browse files Browse the repository at this point in the history
Fix of breaking changes
  • Loading branch information
asmyasnikov committed Nov 9, 2023
1 parent 13c5744 commit 1add019
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 38 deletions.
16 changes: 10 additions & 6 deletions internal/xsql/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (c *conn) PrepareContext(ctx context.Context, query string) (_ driver.Stmt,
if c.currentTx != nil {
return c.currentTx.PrepareContext(ctx, query)
}
onDone := trace.DatabaseSQLOnConnPrepare(c.trace, &ctx, query)
onDone := trace.DatabaseSQLOnConnPrepare(c.trace, &ctx, stack.FunctionID(0), query)
defer func() {
onDone(finalErr)
}()
Expand All @@ -177,7 +177,9 @@ func (c *conn) execContext(ctx context.Context, query string, args []driver.Name
) {
var (
m = queryModeFromContext(ctx, c.defaultQueryMode)
onDone = trace.DatabaseSQLOnConnExec(c.trace, &ctx, query, m.String(), xcontext.IsIdempotent(ctx), c.sinceLastUsage())
onDone = trace.DatabaseSQLOnConnExec(
c.trace, &ctx, stack.FunctionID(0), query, m.String(), xcontext.IsIdempotent(ctx), c.sinceLastUsage(),
)
)

defer func() {
Expand Down Expand Up @@ -270,7 +272,7 @@ func (c *conn) queryContext(ctx context.Context, query string, args []driver.Nam
) {
m := queryModeFromContext(ctx, c.defaultQueryMode)
onDone := trace.DatabaseSQLOnConnQuery(
c.trace, &ctx, query, m.String(), xcontext.IsIdempotent(ctx), c.sinceLastUsage(),
c.trace, &ctx, stack.FunctionID(0), query, m.String(), xcontext.IsIdempotent(ctx), c.sinceLastUsage(),
)
defer func() {
c.lastUsage.Store(time.Now().Unix())
Expand Down Expand Up @@ -351,7 +353,7 @@ func (c *conn) queryContext(ctx context.Context, query string, args []driver.Nam
}

func (c *conn) Ping(ctx context.Context) (finalErr error) {
onDone := trace.DatabaseSQLOnConnPing(c.trace, &ctx)
onDone := trace.DatabaseSQLOnConnPing(c.trace, &ctx, stack.FunctionID(0))
defer func() {
onDone(finalErr)
}()
Expand All @@ -367,7 +369,9 @@ func (c *conn) Ping(ctx context.Context) (finalErr error) {
func (c *conn) Close() (finalErr error) {
if c.closed.CompareAndSwap(false, true) {
c.connector.detach(c)
onDone := trace.DatabaseSQLOnConnClose(c.trace)
onDone := trace.DatabaseSQLOnConnClose(
c.trace, &c.openConnCtx, stack.FunctionID(0),
)
defer func() {
onDone(finalErr)
}()
Expand Down Expand Up @@ -406,7 +410,7 @@ func (c *conn) ID() string {

func (c *conn) BeginTx(ctx context.Context, txOptions driver.TxOptions) (_ driver.Tx, finalErr error) {
var tx currentTx
onDone := trace.DatabaseSQLOnConnBegin(c.trace, &ctx)
onDone := trace.DatabaseSQLOnConnBegin(c.trace, &ctx, stack.FunctionID(0))
defer func() {
onDone(tx, finalErr)
}()
Expand Down
5 changes: 4 additions & 1 deletion internal/xsql/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
metaHeaders "github.com/ydb-platform/ydb-go-sdk/v3/internal/meta"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/meta"
Expand Down Expand Up @@ -295,7 +296,9 @@ func (c *Connector) detach(cc *conn) {

func (c *Connector) Connect(ctx context.Context) (_ driver.Conn, err error) {
var (
onDone = trace.DatabaseSQLOnConnectorConnect(c.trace, &ctx)
onDone = trace.DatabaseSQLOnConnectorConnect(
c.trace, &ctx, stack.FunctionID(0),
)
session table.ClosableSession
)
defer func() {
Expand Down
7 changes: 4 additions & 3 deletions internal/xsql/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql/driver"
"fmt"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/badconn"
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
Expand All @@ -29,7 +30,7 @@ var (
)

func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (_ driver.Rows, finalErr error) {
onDone := trace.DatabaseSQLOnStmtQuery(s.trace, &ctx, &s.prepareCtx, s.query)
onDone := trace.DatabaseSQLOnStmtQuery(s.trace, &ctx, stack.FunctionID(0), &s.prepareCtx, s.query)
defer func() {
onDone(finalErr)
}()
Expand All @@ -45,7 +46,7 @@ func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (_ dr
}

func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (_ driver.Result, finalErr error) {
onDone := trace.DatabaseSQLOnStmtExec(s.trace, &ctx, &s.prepareCtx, s.query)
onDone := trace.DatabaseSQLOnStmtExec(s.trace, &ctx, stack.FunctionID(0), &s.prepareCtx, s.query)
defer func() {
onDone(finalErr)
}()
Expand All @@ -65,7 +66,7 @@ func (s *stmt) NumInput() int {
}

func (s *stmt) Close() (finalErr error) {
onDone := trace.DatabaseSQLOnStmtClose(s.trace, &s.prepareCtx)
onDone := trace.DatabaseSQLOnStmtClose(s.trace, &s.prepareCtx, stack.FunctionID(0))
defer func() {
onDone(finalErr)
}()
Expand Down
11 changes: 6 additions & 5 deletions internal/xsql/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql/driver"
"fmt"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/badconn"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/isolation"
Expand Down Expand Up @@ -70,7 +71,7 @@ func (tx *tx) checkTxState() error {
}

func (tx *tx) Commit() (finalErr error) {
onDone := trace.DatabaseSQLOnTxCommit(tx.conn.trace, &tx.beginCtx, tx)
onDone := trace.DatabaseSQLOnTxCommit(tx.conn.trace, &tx.beginCtx, stack.FunctionID(0), tx)
defer func() {
onDone(finalErr)
}()
Expand All @@ -88,7 +89,7 @@ func (tx *tx) Commit() (finalErr error) {
}

func (tx *tx) Rollback() (finalErr error) {
onDone := trace.DatabaseSQLOnTxRollback(tx.conn.trace, &tx.beginCtx, tx)
onDone := trace.DatabaseSQLOnTxRollback(tx.conn.trace, &tx.beginCtx, stack.FunctionID(0), tx)
defer func() {
onDone(finalErr)
}()
Expand All @@ -108,7 +109,7 @@ func (tx *tx) Rollback() (finalErr error) {
func (tx *tx) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (
_ driver.Rows, finalErr error,
) {
onDone := trace.DatabaseSQLOnTxQuery(tx.conn.trace, &ctx, &tx.beginCtx, tx, query)
onDone := trace.DatabaseSQLOnTxQuery(tx.conn.trace, &ctx, stack.FunctionID(0), &tx.beginCtx, tx, query, true)
defer func() {
onDone(finalErr)
}()
Expand Down Expand Up @@ -146,7 +147,7 @@ func (tx *tx) QueryContext(ctx context.Context, query string, args []driver.Name
func (tx *tx) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (
_ driver.Result, finalErr error,
) {
onDone := trace.DatabaseSQLOnTxExec(tx.conn.trace, &ctx, &tx.beginCtx, tx, query)
onDone := trace.DatabaseSQLOnTxExec(tx.conn.trace, &ctx, stack.FunctionID(0), &tx.beginCtx, tx, query, true)
defer func() {
onDone(finalErr)
}()
Expand Down Expand Up @@ -176,7 +177,7 @@ func (tx *tx) ExecContext(ctx context.Context, query string, args []driver.Named
}

func (tx *tx) PrepareContext(ctx context.Context, query string) (_ driver.Stmt, finalErr error) {
onDone := trace.DatabaseSQLOnTxPrepare(tx.conn.trace, &ctx, &tx.beginCtx, tx, query)
onDone := trace.DatabaseSQLOnTxPrepare(tx.conn.trace, &ctx, stack.FunctionID(0), &tx.beginCtx, tx, query)
defer func() {
onDone(finalErr)
}()
Expand Down
16 changes: 11 additions & 5 deletions internal/xsql/tx_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"database/sql/driver"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/badconn"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
Expand All @@ -17,7 +19,7 @@ type txFake struct {
}

func (tx *txFake) PrepareContext(ctx context.Context, query string) (_ driver.Stmt, finalErr error) {
onDone := trace.DatabaseSQLOnTxPrepare(tx.conn.trace, &ctx, &tx.beginCtx, tx, query)
onDone := trace.DatabaseSQLOnTxPrepare(tx.conn.trace, &ctx, stack.FunctionID(0), &tx.beginCtx, tx, query)
defer func() {
onDone(finalErr)
}()
Expand Down Expand Up @@ -52,7 +54,7 @@ func (tx *txFake) ID() string {
}

func (tx *txFake) Commit() (err error) {
onDone := trace.DatabaseSQLOnTxCommit(tx.conn.trace, &tx.ctx, tx)
onDone := trace.DatabaseSQLOnTxCommit(tx.conn.trace, &tx.ctx, stack.FunctionID(0), tx)
defer func() {
onDone(err)
}()
Expand All @@ -66,7 +68,7 @@ func (tx *txFake) Commit() (err error) {
}

func (tx *txFake) Rollback() (err error) {
onDone := trace.DatabaseSQLOnTxRollback(tx.conn.trace, &tx.ctx, tx)
onDone := trace.DatabaseSQLOnTxRollback(tx.conn.trace, &tx.ctx, stack.FunctionID(0), tx)
defer func() {
onDone(err)
}()
Expand All @@ -82,7 +84,9 @@ func (tx *txFake) Rollback() (err error) {
func (tx *txFake) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (
rows driver.Rows, err error,
) {
onDone := trace.DatabaseSQLOnTxQuery(tx.conn.trace, &ctx, &tx.ctx, tx, query)
onDone := trace.DatabaseSQLOnTxQuery(
tx.conn.trace, &ctx, stack.FunctionID(0), &tx.ctx, tx, query, xcontext.IsIdempotent(ctx),
)
defer func() {
onDone(err)
}()
Expand All @@ -96,7 +100,9 @@ func (tx *txFake) QueryContext(ctx context.Context, query string, args []driver.
func (tx *txFake) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (
result driver.Result, err error,
) {
onDone := trace.DatabaseSQLOnTxExec(tx.conn.trace, &ctx, &tx.ctx, tx, query)
onDone := trace.DatabaseSQLOnTxExec(
tx.conn.trace, &ctx, stack.FunctionID(0), &tx.ctx, tx, query, xcontext.IsIdempotent(ctx),
)
defer func() {
onDone(err)
}()
Expand Down
32 changes: 30 additions & 2 deletions trace/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type (
// Warning: concurrent access to pointer on client side must be excluded.
// Safe replacement of context are provided only inside callback function
Context *context.Context
Call call
}
DatabaseSQLConnectorConnectDoneInfo struct {
Error error
Expand All @@ -51,6 +52,7 @@ type (
// Warning: concurrent access to pointer on client side must be excluded.
// Safe replacement of context are provided only inside callback function
Context *context.Context
Call call
}
DatabaseSQLConnPingDoneInfo struct {
Error error
Expand All @@ -61,6 +63,7 @@ type (
// Warning: concurrent access to pointer on client side must be excluded.
// Safe replacement of context are provided only inside callback function
Context *context.Context
Call call
Query string
}
DatabaseSQLConnPrepareDoneInfo struct {
Expand All @@ -72,15 +75,23 @@ type (
// Warning: concurrent access to pointer on client side must be excluded.
// Safe replacement of context are provided only inside callback function
Context *context.Context
Call call
TxContext *context.Context
Tx tableTransactionInfo
Query string
}
DatabaseSQLTxPrepareDoneInfo struct {
Error error
}
DatabaseSQLConnCloseStartInfo struct{}
DatabaseSQLConnCloseDoneInfo struct {
DatabaseSQLConnCloseStartInfo struct {
// Context make available context in trace callback function.
// Pointer to context provide replacement of context in trace callback function.
// Warning: concurrent access to pointer on client side must be excluded.
// Safe replacement of context are provided only inside callback function
Context *context.Context
Call call
}
DatabaseSQLConnCloseDoneInfo struct {
Error error
}
DatabaseSQLConnBeginStartInfo struct {
Expand All @@ -89,6 +100,7 @@ type (
// Warning: concurrent access to pointer on client side must be excluded.
// Safe replacement of context are provided only inside callback function
Context *context.Context
Call call
}
DatabaseSQLConnBeginDoneInfo struct {
Tx tableTransactionInfo
Expand All @@ -100,6 +112,7 @@ type (
// Warning: concurrent access to pointer on client side must be excluded.
// Safe replacement of context are provided only inside callback function
Context *context.Context
Call call
Query string
Mode string
Idempotent bool
Expand All @@ -114,6 +127,7 @@ type (
// Warning: concurrent access to pointer on client side must be excluded.
// Safe replacement of context are provided only inside callback function
Context *context.Context
Call call
Query string
Mode string
Idempotent bool
Expand Down Expand Up @@ -141,9 +155,13 @@ type (
// Warning: concurrent access to pointer on client side must be excluded.
// Safe replacement of context are provided only inside callback function
Context *context.Context
Call call
TxContext *context.Context
Tx tableTransactionInfo
Query string

// Deprecated: all transactions are idempotent
Idempotent bool
}
DatabaseSQLTxQueryDoneInfo struct {
Error error
Expand All @@ -154,9 +172,13 @@ type (
// Warning: concurrent access to pointer on client side must be excluded.
// Safe replacement of context are provided only inside callback function
Context *context.Context
Call call
TxContext *context.Context
Tx tableTransactionInfo
Query string

// Deprecated: all transactions are idempotent
Idempotent bool
}
DatabaseSQLTxExecDoneInfo struct {
Error error
Expand All @@ -167,6 +189,7 @@ type (
// Warning: concurrent access to pointer on client side must be excluded.
// Safe replacement of context are provided only inside callback function
TxContext *context.Context
Call call
Tx tableTransactionInfo
}
DatabaseSQLTxCommitDoneInfo struct {
Expand All @@ -178,13 +201,15 @@ type (
// Warning: concurrent access to pointer on client side must be excluded.
// Safe replacement of context are provided only inside callback function
TxContext *context.Context
Call call
Tx tableTransactionInfo
}
DatabaseSQLTxRollbackDoneInfo struct {
Error error
}
DatabaseSQLStmtCloseStartInfo struct {
StmtContext *context.Context
Call call
}
DatabaseSQLStmtCloseDoneInfo struct {
Error error
Expand All @@ -195,6 +220,7 @@ type (
// Warning: concurrent access to pointer on client side must be excluded.
// Safe replacement of context are provided only inside callback function
Context *context.Context
Call call
StmtContext *context.Context
Query string
}
Expand All @@ -207,6 +233,7 @@ type (
// Warning: concurrent access to pointer on client side must be excluded.
// Safe replacement of context are provided only inside callback function
Context *context.Context
Call call
StmtContext *context.Context
Query string
}
Expand All @@ -219,6 +246,7 @@ type (
// Warning: concurrent access to pointer on client side must be excluded.
// Safe replacement of context are provided only inside callback function
Context *context.Context
Call call
ID string
Idempotent bool
}
Expand Down
Loading

0 comments on commit 1add019

Please sign in to comment.