Skip to content

Commit 09192ee

Browse files
committed
fixed tracing over database/sql
1 parent 33ae952 commit 09192ee

File tree

2 files changed

+78
-9
lines changed

2 files changed

+78
-9
lines changed

internal/xsql/conn.go

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
1616
internalTable "github.com/ydb-platform/ydb-go-sdk/v3/internal/table"
1717
"github.com/ydb-platform/ydb-go-sdk/v3/internal/table/config"
18+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
1819
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1920
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xslices"
2021
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/conn"
@@ -37,6 +38,7 @@ type (
3738

3839
cc conn.Conn
3940
currentTx *txWrapper
41+
ctx context.Context //nolint:containedctx
4042

4143
connector *Connector
4244
lastUsage xsync.LastUsage
@@ -60,10 +62,13 @@ func (c *connWrapper) CheckNamedValue(value *driver.NamedValue) error {
6062
return nil
6163
}
6264

63-
func (c *connWrapper) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
64-
if c.currentTx != nil {
65-
return nil, xerrors.WithStackTrace(xerrors.AlreadyHasTx(c.currentTx.ID()))
66-
}
65+
func BeginTx(ctx context.Context, c *connWrapper, opts driver.TxOptions) (_ driver.Tx, finalErr error) {
66+
onDone := trace.DatabaseSQLOnConnBegin(c.connector.trace, &ctx,
67+
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql.BeginTx"),
68+
)
69+
defer func() {
70+
onDone(c.currentTx, finalErr)
71+
}()
6772

6873
tx, err := c.cc.BeginTx(ctx, opts)
6974
if err != nil {
@@ -79,7 +84,22 @@ func (c *connWrapper) BeginTx(ctx context.Context, opts driver.TxOptions) (drive
7984
return c.currentTx, nil
8085
}
8186

82-
func (c *connWrapper) Close() error {
87+
func (c *connWrapper) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
88+
if c.currentTx != nil {
89+
return nil, xerrors.WithStackTrace(xerrors.AlreadyHasTx(c.currentTx.ID()))
90+
}
91+
92+
return BeginTx(ctx, c, opts)
93+
}
94+
95+
func Close(c *connWrapper) (finalErr error) {
96+
onDone := trace.DatabaseSQLOnConnClose(c.connector.Trace(), &c.ctx,
97+
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql.Close"),
98+
)
99+
defer func() {
100+
onDone(finalErr)
101+
}()
102+
83103
err := c.cc.Close()
84104
if err != nil {
85105
return xerrors.WithStackTrace(err)
@@ -88,6 +108,10 @@ func (c *connWrapper) Close() error {
88108
return nil
89109
}
90110

111+
func (c *connWrapper) Close() (finalErr error) {
112+
return Close(c)
113+
}
114+
91115
func (c *connWrapper) Connector() *Connector {
92116
return c.connector
93117
}
@@ -139,9 +163,9 @@ func (c *connWrapper) Prepare(string) (driver.Stmt, error) {
139163
return nil, errDeprecated
140164
}
141165

142-
func (c *connWrapper) PrepareContext(ctx context.Context, sql string) (_ driver.Stmt, finalErr error) {
166+
func PrepareContext(ctx context.Context, c *connWrapper, sql string) (_ driver.Stmt, finalErr error) {
143167
onDone := trace.DatabaseSQLOnConnPrepare(c.connector.Trace(), &ctx,
144-
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql.(*connWrapper).PrepareContext"),
168+
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql.PrepareContext"),
145169
sql,
146170
)
147171
defer func() {
@@ -160,6 +184,10 @@ func (c *connWrapper) PrepareContext(ctx context.Context, sql string) (_ driver.
160184
}, nil
161185
}
162186

187+
func (c *connWrapper) PrepareContext(ctx context.Context, sql string) (_ driver.Stmt, finalErr error) {
188+
return PrepareContext(ctx, c, sql)
189+
}
190+
163191
func (c *connWrapper) LastUsage() time.Time {
164192
return c.lastUsage.Get()
165193
}
@@ -178,7 +206,17 @@ func (c *connWrapper) toYdb(sql string, args ...driver.NamedValue) (yql string,
178206
return yql, &params, nil
179207
}
180208

181-
func (c *connWrapper) QueryContext(ctx context.Context, sql string, args []driver.NamedValue) (driver.Rows, error) {
209+
func QueryContext(ctx context.Context, c *connWrapper, sql string, args []driver.NamedValue) (
210+
_ driver.Rows, finalErr error,
211+
) {
212+
onDone := trace.DatabaseSQLOnConnQuery(c.connector.Trace(), &ctx,
213+
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql.QueryContext"),
214+
sql, c.connector.processor.String(), xcontext.IsIdempotent(ctx), c.connector.clock.Since(c.lastUsage.Get()),
215+
)
216+
defer func() {
217+
onDone(finalErr)
218+
}()
219+
182220
done := c.lastUsage.Start()
183221
defer done()
184222

@@ -203,7 +241,21 @@ func (c *connWrapper) QueryContext(ctx context.Context, sql string, args []drive
203241
return c.cc.Query(ctx, sql, params)
204242
}
205243

206-
func (c *connWrapper) ExecContext(ctx context.Context, sql string, args []driver.NamedValue) (driver.Result, error) {
244+
func (c *connWrapper) QueryContext(ctx context.Context, sql string, args []driver.NamedValue) (driver.Rows, error) {
245+
return QueryContext(ctx, c, sql, args)
246+
}
247+
248+
func ExecContext(ctx context.Context, c *connWrapper, sql string, args []driver.NamedValue) (
249+
_ driver.Result, finalErr error,
250+
) {
251+
onDone := trace.DatabaseSQLOnConnExec(c.connector.Trace(), &ctx,
252+
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql.ExecContext"),
253+
sql, c.connector.processor.String(), xcontext.IsIdempotent(ctx), c.connector.clock.Since(c.lastUsage.Get()),
254+
)
255+
defer func() {
256+
onDone(finalErr)
257+
}()
258+
207259
done := c.lastUsage.Start()
208260
defer done()
209261

@@ -219,6 +271,10 @@ func (c *connWrapper) ExecContext(ctx context.Context, sql string, args []driver
219271
return c.cc.Exec(ctx, sql, params)
220272
}
221273

274+
func (c *connWrapper) ExecContext(ctx context.Context, sql string, args []driver.NamedValue) (driver.Result, error) {
275+
return ExecContext(ctx, c, sql, args)
276+
}
277+
222278
func (c *connWrapper) GetDatabaseName() string {
223279
return c.connector.Name()
224280
}

internal/xsql/connector.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ type (
6363
}
6464
)
6565

66+
func (e Engine) String() string {
67+
switch e {
68+
case TABLE_SERVICE:
69+
return "TABLE_SERVICE"
70+
case QUERY_SERVICE:
71+
return "QUERY_SERVICE"
72+
default:
73+
return "UNKNOWN"
74+
}
75+
}
76+
6677
func (c *Connector) RetryBudget() budget.Budget {
6778
return c.retryBudget
6879
}
@@ -130,6 +141,7 @@ func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
130141
c.conns.Delete(id)
131142
}))...,
132143
),
144+
ctx: ctx,
133145
connector: c,
134146
lastUsage: xsync.NewLastUsage(xsync.WithClock(c.Clock())),
135147
}
@@ -153,6 +165,7 @@ func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
153165
c.conns.Delete(id)
154166
}))...,
155167
),
168+
ctx: ctx,
156169
connector: c,
157170
lastUsage: xsync.NewLastUsage(xsync.WithClock(c.Clock())),
158171
}

0 commit comments

Comments
 (0)