Skip to content

Commit 4e5ae05

Browse files
authored
Merge pull request #889 from ydb-platform/fix
Added function id into database/sql traces (start info's) + Fix of breaking changes
2 parents 13c5744 + 1add019 commit 4e5ae05

File tree

7 files changed

+100
-38
lines changed

7 files changed

+100
-38
lines changed

internal/xsql/conn.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (c *conn) PrepareContext(ctx context.Context, query string) (_ driver.Stmt,
152152
if c.currentTx != nil {
153153
return c.currentTx.PrepareContext(ctx, query)
154154
}
155-
onDone := trace.DatabaseSQLOnConnPrepare(c.trace, &ctx, query)
155+
onDone := trace.DatabaseSQLOnConnPrepare(c.trace, &ctx, stack.FunctionID(0), query)
156156
defer func() {
157157
onDone(finalErr)
158158
}()
@@ -177,7 +177,9 @@ func (c *conn) execContext(ctx context.Context, query string, args []driver.Name
177177
) {
178178
var (
179179
m = queryModeFromContext(ctx, c.defaultQueryMode)
180-
onDone = trace.DatabaseSQLOnConnExec(c.trace, &ctx, query, m.String(), xcontext.IsIdempotent(ctx), c.sinceLastUsage())
180+
onDone = trace.DatabaseSQLOnConnExec(
181+
c.trace, &ctx, stack.FunctionID(0), query, m.String(), xcontext.IsIdempotent(ctx), c.sinceLastUsage(),
182+
)
181183
)
182184

183185
defer func() {
@@ -270,7 +272,7 @@ func (c *conn) queryContext(ctx context.Context, query string, args []driver.Nam
270272
) {
271273
m := queryModeFromContext(ctx, c.defaultQueryMode)
272274
onDone := trace.DatabaseSQLOnConnQuery(
273-
c.trace, &ctx, query, m.String(), xcontext.IsIdempotent(ctx), c.sinceLastUsage(),
275+
c.trace, &ctx, stack.FunctionID(0), query, m.String(), xcontext.IsIdempotent(ctx), c.sinceLastUsage(),
274276
)
275277
defer func() {
276278
c.lastUsage.Store(time.Now().Unix())
@@ -351,7 +353,7 @@ func (c *conn) queryContext(ctx context.Context, query string, args []driver.Nam
351353
}
352354

353355
func (c *conn) Ping(ctx context.Context) (finalErr error) {
354-
onDone := trace.DatabaseSQLOnConnPing(c.trace, &ctx)
356+
onDone := trace.DatabaseSQLOnConnPing(c.trace, &ctx, stack.FunctionID(0))
355357
defer func() {
356358
onDone(finalErr)
357359
}()
@@ -367,7 +369,9 @@ func (c *conn) Ping(ctx context.Context) (finalErr error) {
367369
func (c *conn) Close() (finalErr error) {
368370
if c.closed.CompareAndSwap(false, true) {
369371
c.connector.detach(c)
370-
onDone := trace.DatabaseSQLOnConnClose(c.trace)
372+
onDone := trace.DatabaseSQLOnConnClose(
373+
c.trace, &c.openConnCtx, stack.FunctionID(0),
374+
)
371375
defer func() {
372376
onDone(finalErr)
373377
}()
@@ -406,7 +410,7 @@ func (c *conn) ID() string {
406410

407411
func (c *conn) BeginTx(ctx context.Context, txOptions driver.TxOptions) (_ driver.Tx, finalErr error) {
408412
var tx currentTx
409-
onDone := trace.DatabaseSQLOnConnBegin(c.trace, &ctx)
413+
onDone := trace.DatabaseSQLOnConnBegin(c.trace, &ctx, stack.FunctionID(0))
410414
defer func() {
411415
onDone(tx, finalErr)
412416
}()

internal/xsql/connector.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
1313
metaHeaders "github.com/ydb-platform/ydb-go-sdk/v3/internal/meta"
14+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
1415
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
1516
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1617
"github.com/ydb-platform/ydb-go-sdk/v3/meta"
@@ -295,7 +296,9 @@ func (c *Connector) detach(cc *conn) {
295296

296297
func (c *Connector) Connect(ctx context.Context) (_ driver.Conn, err error) {
297298
var (
298-
onDone = trace.DatabaseSQLOnConnectorConnect(c.trace, &ctx)
299+
onDone = trace.DatabaseSQLOnConnectorConnect(
300+
c.trace, &ctx, stack.FunctionID(0),
301+
)
299302
session table.ClosableSession
300303
)
301304
defer func() {

internal/xsql/stmt.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"database/sql/driver"
66
"fmt"
77

8+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
89
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
910
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/badconn"
1011
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
@@ -29,7 +30,7 @@ var (
2930
)
3031

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

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

6768
func (s *stmt) Close() (finalErr error) {
68-
onDone := trace.DatabaseSQLOnStmtClose(s.trace, &s.prepareCtx)
69+
onDone := trace.DatabaseSQLOnStmtClose(s.trace, &s.prepareCtx, stack.FunctionID(0))
6970
defer func() {
7071
onDone(finalErr)
7172
}()

internal/xsql/tx.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"database/sql/driver"
66
"fmt"
77

8+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
89
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
910
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/badconn"
1011
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/isolation"
@@ -70,7 +71,7 @@ func (tx *tx) checkTxState() error {
7071
}
7172

7273
func (tx *tx) Commit() (finalErr error) {
73-
onDone := trace.DatabaseSQLOnTxCommit(tx.conn.trace, &tx.beginCtx, tx)
74+
onDone := trace.DatabaseSQLOnTxCommit(tx.conn.trace, &tx.beginCtx, stack.FunctionID(0), tx)
7475
defer func() {
7576
onDone(finalErr)
7677
}()
@@ -88,7 +89,7 @@ func (tx *tx) Commit() (finalErr error) {
8889
}
8990

9091
func (tx *tx) Rollback() (finalErr error) {
91-
onDone := trace.DatabaseSQLOnTxRollback(tx.conn.trace, &tx.beginCtx, tx)
92+
onDone := trace.DatabaseSQLOnTxRollback(tx.conn.trace, &tx.beginCtx, stack.FunctionID(0), tx)
9293
defer func() {
9394
onDone(finalErr)
9495
}()
@@ -108,7 +109,7 @@ func (tx *tx) Rollback() (finalErr error) {
108109
func (tx *tx) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (
109110
_ driver.Rows, finalErr error,
110111
) {
111-
onDone := trace.DatabaseSQLOnTxQuery(tx.conn.trace, &ctx, &tx.beginCtx, tx, query)
112+
onDone := trace.DatabaseSQLOnTxQuery(tx.conn.trace, &ctx, stack.FunctionID(0), &tx.beginCtx, tx, query, true)
112113
defer func() {
113114
onDone(finalErr)
114115
}()
@@ -146,7 +147,7 @@ func (tx *tx) QueryContext(ctx context.Context, query string, args []driver.Name
146147
func (tx *tx) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (
147148
_ driver.Result, finalErr error,
148149
) {
149-
onDone := trace.DatabaseSQLOnTxExec(tx.conn.trace, &ctx, &tx.beginCtx, tx, query)
150+
onDone := trace.DatabaseSQLOnTxExec(tx.conn.trace, &ctx, stack.FunctionID(0), &tx.beginCtx, tx, query, true)
150151
defer func() {
151152
onDone(finalErr)
152153
}()
@@ -176,7 +177,7 @@ func (tx *tx) ExecContext(ctx context.Context, query string, args []driver.Named
176177
}
177178

178179
func (tx *tx) PrepareContext(ctx context.Context, query string) (_ driver.Stmt, finalErr error) {
179-
onDone := trace.DatabaseSQLOnTxPrepare(tx.conn.trace, &ctx, &tx.beginCtx, tx, query)
180+
onDone := trace.DatabaseSQLOnTxPrepare(tx.conn.trace, &ctx, stack.FunctionID(0), &tx.beginCtx, tx, query)
180181
defer func() {
181182
onDone(finalErr)
182183
}()

internal/xsql/tx_fake.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"database/sql/driver"
66

7+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
8+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
79
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
810
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/badconn"
911
"github.com/ydb-platform/ydb-go-sdk/v3/table"
@@ -17,7 +19,7 @@ type txFake struct {
1719
}
1820

1921
func (tx *txFake) PrepareContext(ctx context.Context, query string) (_ driver.Stmt, finalErr error) {
20-
onDone := trace.DatabaseSQLOnTxPrepare(tx.conn.trace, &ctx, &tx.beginCtx, tx, query)
22+
onDone := trace.DatabaseSQLOnTxPrepare(tx.conn.trace, &ctx, stack.FunctionID(0), &tx.beginCtx, tx, query)
2123
defer func() {
2224
onDone(finalErr)
2325
}()
@@ -52,7 +54,7 @@ func (tx *txFake) ID() string {
5254
}
5355

5456
func (tx *txFake) Commit() (err error) {
55-
onDone := trace.DatabaseSQLOnTxCommit(tx.conn.trace, &tx.ctx, tx)
57+
onDone := trace.DatabaseSQLOnTxCommit(tx.conn.trace, &tx.ctx, stack.FunctionID(0), tx)
5658
defer func() {
5759
onDone(err)
5860
}()
@@ -66,7 +68,7 @@ func (tx *txFake) Commit() (err error) {
6668
}
6769

6870
func (tx *txFake) Rollback() (err error) {
69-
onDone := trace.DatabaseSQLOnTxRollback(tx.conn.trace, &tx.ctx, tx)
71+
onDone := trace.DatabaseSQLOnTxRollback(tx.conn.trace, &tx.ctx, stack.FunctionID(0), tx)
7072
defer func() {
7173
onDone(err)
7274
}()
@@ -82,7 +84,9 @@ func (tx *txFake) Rollback() (err error) {
8284
func (tx *txFake) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (
8385
rows driver.Rows, err error,
8486
) {
85-
onDone := trace.DatabaseSQLOnTxQuery(tx.conn.trace, &ctx, &tx.ctx, tx, query)
87+
onDone := trace.DatabaseSQLOnTxQuery(
88+
tx.conn.trace, &ctx, stack.FunctionID(0), &tx.ctx, tx, query, xcontext.IsIdempotent(ctx),
89+
)
8690
defer func() {
8791
onDone(err)
8892
}()
@@ -96,7 +100,9 @@ func (tx *txFake) QueryContext(ctx context.Context, query string, args []driver.
96100
func (tx *txFake) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (
97101
result driver.Result, err error,
98102
) {
99-
onDone := trace.DatabaseSQLOnTxExec(tx.conn.trace, &ctx, &tx.ctx, tx, query)
103+
onDone := trace.DatabaseSQLOnTxExec(
104+
tx.conn.trace, &ctx, stack.FunctionID(0), &tx.ctx, tx, query, xcontext.IsIdempotent(ctx),
105+
)
100106
defer func() {
101107
onDone(err)
102108
}()

trace/sql.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type (
4040
// Warning: concurrent access to pointer on client side must be excluded.
4141
// Safe replacement of context are provided only inside callback function
4242
Context *context.Context
43+
Call call
4344
}
4445
DatabaseSQLConnectorConnectDoneInfo struct {
4546
Error error
@@ -51,6 +52,7 @@ type (
5152
// Warning: concurrent access to pointer on client side must be excluded.
5253
// Safe replacement of context are provided only inside callback function
5354
Context *context.Context
55+
Call call
5456
}
5557
DatabaseSQLConnPingDoneInfo struct {
5658
Error error
@@ -61,6 +63,7 @@ type (
6163
// Warning: concurrent access to pointer on client side must be excluded.
6264
// Safe replacement of context are provided only inside callback function
6365
Context *context.Context
66+
Call call
6467
Query string
6568
}
6669
DatabaseSQLConnPrepareDoneInfo struct {
@@ -72,15 +75,23 @@ type (
7275
// Warning: concurrent access to pointer on client side must be excluded.
7376
// Safe replacement of context are provided only inside callback function
7477
Context *context.Context
78+
Call call
7579
TxContext *context.Context
7680
Tx tableTransactionInfo
7781
Query string
7882
}
7983
DatabaseSQLTxPrepareDoneInfo struct {
8084
Error error
8185
}
82-
DatabaseSQLConnCloseStartInfo struct{}
83-
DatabaseSQLConnCloseDoneInfo struct {
86+
DatabaseSQLConnCloseStartInfo struct {
87+
// Context make available context in trace callback function.
88+
// Pointer to context provide replacement of context in trace callback function.
89+
// Warning: concurrent access to pointer on client side must be excluded.
90+
// Safe replacement of context are provided only inside callback function
91+
Context *context.Context
92+
Call call
93+
}
94+
DatabaseSQLConnCloseDoneInfo struct {
8495
Error error
8596
}
8697
DatabaseSQLConnBeginStartInfo struct {
@@ -89,6 +100,7 @@ type (
89100
// Warning: concurrent access to pointer on client side must be excluded.
90101
// Safe replacement of context are provided only inside callback function
91102
Context *context.Context
103+
Call call
92104
}
93105
DatabaseSQLConnBeginDoneInfo struct {
94106
Tx tableTransactionInfo
@@ -100,6 +112,7 @@ type (
100112
// Warning: concurrent access to pointer on client side must be excluded.
101113
// Safe replacement of context are provided only inside callback function
102114
Context *context.Context
115+
Call call
103116
Query string
104117
Mode string
105118
Idempotent bool
@@ -114,6 +127,7 @@ type (
114127
// Warning: concurrent access to pointer on client side must be excluded.
115128
// Safe replacement of context are provided only inside callback function
116129
Context *context.Context
130+
Call call
117131
Query string
118132
Mode string
119133
Idempotent bool
@@ -141,9 +155,13 @@ type (
141155
// Warning: concurrent access to pointer on client side must be excluded.
142156
// Safe replacement of context are provided only inside callback function
143157
Context *context.Context
158+
Call call
144159
TxContext *context.Context
145160
Tx tableTransactionInfo
146161
Query string
162+
163+
// Deprecated: all transactions are idempotent
164+
Idempotent bool
147165
}
148166
DatabaseSQLTxQueryDoneInfo struct {
149167
Error error
@@ -154,9 +172,13 @@ type (
154172
// Warning: concurrent access to pointer on client side must be excluded.
155173
// Safe replacement of context are provided only inside callback function
156174
Context *context.Context
175+
Call call
157176
TxContext *context.Context
158177
Tx tableTransactionInfo
159178
Query string
179+
180+
// Deprecated: all transactions are idempotent
181+
Idempotent bool
160182
}
161183
DatabaseSQLTxExecDoneInfo struct {
162184
Error error
@@ -167,6 +189,7 @@ type (
167189
// Warning: concurrent access to pointer on client side must be excluded.
168190
// Safe replacement of context are provided only inside callback function
169191
TxContext *context.Context
192+
Call call
170193
Tx tableTransactionInfo
171194
}
172195
DatabaseSQLTxCommitDoneInfo struct {
@@ -178,13 +201,15 @@ type (
178201
// Warning: concurrent access to pointer on client side must be excluded.
179202
// Safe replacement of context are provided only inside callback function
180203
TxContext *context.Context
204+
Call call
181205
Tx tableTransactionInfo
182206
}
183207
DatabaseSQLTxRollbackDoneInfo struct {
184208
Error error
185209
}
186210
DatabaseSQLStmtCloseStartInfo struct {
187211
StmtContext *context.Context
212+
Call call
188213
}
189214
DatabaseSQLStmtCloseDoneInfo struct {
190215
Error error
@@ -195,6 +220,7 @@ type (
195220
// Warning: concurrent access to pointer on client side must be excluded.
196221
// Safe replacement of context are provided only inside callback function
197222
Context *context.Context
223+
Call call
198224
StmtContext *context.Context
199225
Query string
200226
}
@@ -207,6 +233,7 @@ type (
207233
// Warning: concurrent access to pointer on client side must be excluded.
208234
// Safe replacement of context are provided only inside callback function
209235
Context *context.Context
236+
Call call
210237
StmtContext *context.Context
211238
Query string
212239
}
@@ -219,6 +246,7 @@ type (
219246
// Warning: concurrent access to pointer on client side must be excluded.
220247
// Safe replacement of context are provided only inside callback function
221248
Context *context.Context
249+
Call call
222250
ID string
223251
Idempotent bool
224252
}

0 commit comments

Comments
 (0)