Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ func (drv) OpenConnector(name string) (driver.Connector, error) {
return &connector{name: name}, nil
}

// Connector returns a [driver.Connector] for the given connection
// parameters.
//
// The tracer may be nil.
func Connector(sqliteURI string, connInitFunc ConnInitFunc, tracer sqliteh.Tracer) driver.Connector {
return &connector{
name: sqliteURI,
Expand All @@ -128,8 +132,10 @@ func Connector(sqliteURI string, connInitFunc ConnInitFunc, tracer sqliteh.Trace
}

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

type connector struct {
name string
tracer sqliteh.Tracer
tracer sqliteh.Tracer // or nil
makeLogger func() ConnLogger // or nil
connInitFunc ConnInitFunc
}
Expand Down Expand Up @@ -193,7 +199,7 @@ const (
type conn struct {
db sqliteh.DB
id sqliteh.TraceConnID
tracer sqliteh.Tracer
tracer sqliteh.Tracer // or nil if unused
logger ConnLogger
stmts map[string]*stmt // persisted statements
txState txState
Expand All @@ -216,6 +222,9 @@ func (c *conn) Close() error {
delete(c.stmts, q)
}
err := reserr(c.db, "Conn.Close", "", c.db.Close())
if c.tracer != nil {
c.tracer.Close(c.id, err)
}
return err
}

Expand Down
1 change: 1 addition & 0 deletions sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ func (t *queryTracer) Commit(_ sqliteh.TraceConnID, _ error) {
}
func (t *queryTracer) Rollback(_ sqliteh.TraceConnID, _ error) {
}
func (t *queryTracer) Close(_ sqliteh.TraceConnID, _ error) {}

func TestTraceQuery(t *testing.T) {
tracer := &queryTracer{
Expand Down
3 changes: 3 additions & 0 deletions sqliteh/sqliteh.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,4 +909,7 @@ type Tracer interface {

// Rollback is called by the driver to report the end of a Tx.
Rollback(id TraceConnID, err error)

// Close is called when the SQLite connection is closed.
Close(id TraceConnID, err error)
}
10 changes: 10 additions & 0 deletions sqlstats/sqlstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Tracer struct {
TxCommitError *expvar.Map
TxRollback *expvar.Map
TxTotalSeconds *expvar.Map
ConnCloses *expvar.Int

curTxs sync.Map // TraceConnID -> *connStats

Expand Down Expand Up @@ -58,6 +59,9 @@ func (t *Tracer) Reset() {
if t.TxTotalSeconds != nil {
t.TxTotalSeconds.Init()
}
if t.ConnCloses != nil {
t.ConnCloses.Set(0)
}
t.curTxs.Range(func(key, value any) bool {
t.curTxs.Delete(key)
return true
Expand Down Expand Up @@ -376,3 +380,9 @@ func (t *Tracer) Handle(w http.ResponseWriter, r *http.Request) {
}
fmt.Fprintf(w, "</table></body></html>")
}

func (t *Tracer) Close(id sqliteh.TraceConnID, err error) {
if t.ConnCloses != nil {
t.ConnCloses.Add(1)
}
}
Loading