Skip to content

Commit 3a1aa40

Browse files
committed
sqlstats: add Close method to Tracer
So users can count how often the conns close. Updates tailscale/corp#33577 Signed-off-by: Brad Fitzpatrick <[email protected]>
1 parent 35d6745 commit 3a1aa40

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

sqlite.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ func (drv) OpenConnector(name string) (driver.Connector, error) {
119119
return &connector{name: name}, nil
120120
}
121121

122+
// Connector returns a [driver.Connector] for the given connection
123+
// parameters.
124+
//
125+
// The tracer may be nil.
122126
func Connector(sqliteURI string, connInitFunc ConnInitFunc, tracer sqliteh.Tracer) driver.Connector {
123127
return &connector{
124128
name: sqliteURI,
@@ -128,8 +132,10 @@ func Connector(sqliteURI string, connInitFunc ConnInitFunc, tracer sqliteh.Trace
128132
}
129133

130134
// ConnectorWithLogger returns a [driver.Connector] for the given connection
131-
// parameters. makeLogger is used to create a [ConnLogger] when [Connect] is
135+
// parameters. makeLogger, if non-nil, is used to create a [ConnLogger] when [Connect] is
132136
// called.
137+
//
138+
// The tracer may also be nil.
133139
func ConnectorWithLogger(sqliteURI string, connInitFunc ConnInitFunc, tracer sqliteh.Tracer, makeLogger func() ConnLogger) driver.Connector {
134140
return &connector{
135141
name: sqliteURI,
@@ -141,7 +147,7 @@ func ConnectorWithLogger(sqliteURI string, connInitFunc ConnInitFunc, tracer sql
141147

142148
type connector struct {
143149
name string
144-
tracer sqliteh.Tracer
150+
tracer sqliteh.Tracer // or nil
145151
makeLogger func() ConnLogger // or nil
146152
connInitFunc ConnInitFunc
147153
}
@@ -193,7 +199,7 @@ const (
193199
type conn struct {
194200
db sqliteh.DB
195201
id sqliteh.TraceConnID
196-
tracer sqliteh.Tracer
202+
tracer sqliteh.Tracer // or nil if unused
197203
logger ConnLogger
198204
stmts map[string]*stmt // persisted statements
199205
txState txState
@@ -216,6 +222,9 @@ func (c *conn) Close() error {
216222
delete(c.stmts, q)
217223
}
218224
err := reserr(c.db, "Conn.Close", "", c.db.Close())
225+
if c.tracer != nil {
226+
c.tracer.Close(c.id, err)
227+
}
219228
return err
220229
}
221230

sqlite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ func (t *queryTracer) Commit(_ sqliteh.TraceConnID, _ error) {
634634
}
635635
func (t *queryTracer) Rollback(_ sqliteh.TraceConnID, _ error) {
636636
}
637+
func (t *queryTracer) Close(_ sqliteh.TraceConnID, _ error) {}
637638

638639
func TestTraceQuery(t *testing.T) {
639640
tracer := &queryTracer{

sqliteh/sqliteh.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,4 +909,7 @@ type Tracer interface {
909909

910910
// Rollback is called by the driver to report the end of a Tx.
911911
Rollback(id TraceConnID, err error)
912+
913+
// Close is called when the SQLite connection is closed.
914+
Close(id TraceConnID, err error)
912915
}

sqlstats/sqlstats.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Tracer struct {
2828
TxCommitError *expvar.Map
2929
TxRollback *expvar.Map
3030
TxTotalSeconds *expvar.Map
31+
ConnCloses *expvar.Int
3132

3233
curTxs sync.Map // TraceConnID -> *connStats
3334

@@ -58,6 +59,9 @@ func (t *Tracer) Reset() {
5859
if t.TxTotalSeconds != nil {
5960
t.TxTotalSeconds.Init()
6061
}
62+
if t.ConnCloses != nil {
63+
t.ConnCloses.Set(0)
64+
}
6165
t.curTxs.Range(func(key, value any) bool {
6266
t.curTxs.Delete(key)
6367
return true
@@ -376,3 +380,9 @@ func (t *Tracer) Handle(w http.ResponseWriter, r *http.Request) {
376380
}
377381
fmt.Fprintf(w, "</table></body></html>")
378382
}
383+
384+
func (t *Tracer) Close(id sqliteh.TraceConnID, err error) {
385+
if t.ConnCloses != nil {
386+
t.ConnCloses.Add(1)
387+
}
388+
}

0 commit comments

Comments
 (0)