Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use slog in fsql #4464

Merged
merged 7 commits into from
Jul 23, 2024
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
3 changes: 1 addition & 2 deletions internal/backends/hana/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"log/slog"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"

"github.com/FerretDB/FerretDB/internal/backends"
"github.com/FerretDB/FerretDB/internal/util/fsql"
Expand Down Expand Up @@ -51,7 +50,7 @@
return nil, err
}

hdb := fsql.WrapDB(db, "hana", zap.L())
hdb := fsql.WrapDB(db, "hana", params.L)

Check warning on line 53 in internal/backends/hana/backend.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/hana/backend.go#L53

Added line #L53 was not covered by tests
hdb.BatchSize = params.BatchSize

return backends.BackendContract(&backend{
Expand Down
3 changes: 1 addition & 2 deletions internal/backends/mysql/metadata/pool/opendb.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"time"

_ "github.com/go-sql-driver/mysql" // register database/sql driver
"go.uber.org/zap"

"github.com/FerretDB/FerretDB/internal/util/fsql"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
Expand Down Expand Up @@ -66,5 +65,5 @@ func openDB(uri string, l *slog.Logger, sp *state.Provider) (*fsql.DB, error) {
}
}

return fsql.WrapDB(db, "", zap.L()), nil
return fsql.WrapDB(db, "", l), nil
}
3 changes: 1 addition & 2 deletions internal/backends/sqlite/metadata/pool/opendb.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"database/sql"
"log/slog"

"go.uber.org/zap"
_ "modernc.org/sqlite" // register database/sql driver

"github.com/FerretDB/FerretDB/internal/util/fsql"
Expand Down Expand Up @@ -73,5 +72,5 @@ func openDB(name, uri string, memory bool, l *slog.Logger, sp *state.Provider) (
}
}

return fsql.WrapDB(db, name, zap.L()), nil
return fsql.WrapDB(db, name, l), nil
}
41 changes: 20 additions & 21 deletions internal/util/fsql/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ package fsql
import (
"context"
"database/sql"
"fmt"
"log/slog"
"time"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"

"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/logging"
"github.com/FerretDB/FerretDB/internal/util/resource"
)

Expand All @@ -35,7 +37,7 @@ type DB struct {
*metricsCollector

sqlDB *sql.DB
l *zap.Logger
l *slog.Logger
token *resource.Token
BatchSize int
}
Expand All @@ -44,15 +46,15 @@ type DB struct {
//
// Name is used for metric label values, etc.
// Logger (that will be named) is used for query logging.
func WrapDB(db *sql.DB, name string, l *zap.Logger) *DB {
func WrapDB(db *sql.DB, name string, l *slog.Logger) *DB {
if db == nil {
return nil
}

res := &DB{
metricsCollector: newMetricsCollector(name, db.Stats),
sqlDB: db,
l: l.Named(name),
l: logging.WithName(l, name),
token: resource.NewToken(),
}

Expand All @@ -76,13 +78,13 @@ func (db *DB) Ping(ctx context.Context) error {
func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error) {
start := time.Now()

fields := []any{zap.Any("args", args)}
db.l.Sugar().With(fields...).Debugf(">>> %s", query)
fields := []any{slog.Any("args", args)}
chilagrow marked this conversation as resolved.
Show resolved Hide resolved
db.l.With(fields...).DebugContext(ctx, fmt.Sprintf(">>> %s", query))

rows, err := db.sqlDB.QueryContext(ctx, query, args...)

fields = append(fields, zap.Duration("time", time.Since(start)), zap.Error(err))
db.l.Sugar().With(fields...).Debugf("<<< %s", query)
fields = append(fields, slog.Duration("time", time.Since(start)), logging.Error(err))
db.l.With(fields...).DebugContext(ctx, fmt.Sprintf("<<< %s", query))

return wrapRows(rows), err
}
Expand All @@ -91,13 +93,13 @@ func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*Row
func (db *DB) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row {
start := time.Now()

fields := []any{zap.Any("args", args)}
db.l.Sugar().With(fields...).Debugf(">>> %s", query)
fields := []any{slog.Any("args", args)}
db.l.With(fields...).DebugContext(ctx, fmt.Sprintf(">>> %s", query))

row := db.sqlDB.QueryRowContext(ctx, query, args...)

fields = append(fields, zap.Duration("time", time.Since(start)), zap.Error(row.Err()))
db.l.Sugar().With(fields...).Debugf("<<< %s", query)
fields = append(fields, slog.Duration("time", time.Since(start)), logging.Error(row.Err()))
db.l.With(fields...).DebugContext(ctx, fmt.Sprintf("<<< %s", query))

return row
}
Expand All @@ -106,21 +108,18 @@ func (db *DB) QueryRowContext(ctx context.Context, query string, args ...any) *s
func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
start := time.Now()

fields := []any{zap.Any("args", args)}
db.l.Sugar().With(fields...).Debugf(">>> %s", query)
fields := []any{slog.Any("args", args)}
db.l.With(fields...).DebugContext(ctx, fmt.Sprintf(">>> %s", query))

res, err := db.sqlDB.ExecContext(ctx, query, args...)

// to differentiate between 0 and nil
var ra *int64

if res != nil {
rav, _ := res.RowsAffected()
ra = &rav
ra, _ := res.RowsAffected()
fields = append(fields, slog.Int64("rows", ra))
}

fields = append(fields, zap.Int64p("rows", ra), zap.Duration("time", time.Since(start)), zap.Error(err))
db.l.Sugar().With(fields...).Debugf("<<< %s", query)
fields = append(fields, slog.Duration("time", time.Since(start)), logging.Error(err))
db.l.With(fields...).DebugContext(ctx, fmt.Sprintf("<<< %s", query))

return res, err
}
Expand Down
40 changes: 19 additions & 21 deletions internal/util/fsql/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
import (
"context"
"database/sql"
"fmt"
"log/slog"
"time"

"go.uber.org/zap"

"github.com/FerretDB/FerretDB/internal/util/logging"
"github.com/FerretDB/FerretDB/internal/util/resource"
)

Expand All @@ -29,12 +30,12 @@
// It exposes the subset of *sql.Tx methods we use.
type Tx struct {
sqlTx *sql.Tx
l *zap.Logger
l *slog.Logger
token *resource.Token
}

// wrapTx creates new Tx.
func wrapTx(tx *sql.Tx, l *zap.Logger) *Tx {
func wrapTx(tx *sql.Tx, l *slog.Logger) *Tx {
if tx == nil {
return nil
}
Expand Down Expand Up @@ -66,13 +67,13 @@
func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error) {
start := time.Now()

fields := []any{zap.Any("args", args)}
tx.l.Sugar().With(fields...).Debugf(">>> %s", query)
fields := []any{slog.Any("args", args)}
tx.l.With(fields...).DebugContext(ctx, fmt.Sprintf(">>> %s", query))

Check warning on line 71 in internal/util/fsql/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/util/fsql/tx.go#L70-L71

Added lines #L70 - L71 were not covered by tests

rows, err := tx.sqlTx.QueryContext(ctx, query, args...)

fields = append(fields, zap.Duration("time", time.Since(start)), zap.Error(err))
tx.l.Sugar().With(fields...).Debugf("<<< %s", query)
fields = append(fields, slog.Duration("time", time.Since(start)), logging.Error(err))
tx.l.With(fields...).DebugContext(ctx, fmt.Sprintf("<<< %s", query))

Check warning on line 76 in internal/util/fsql/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/util/fsql/tx.go#L75-L76

Added lines #L75 - L76 were not covered by tests

return wrapRows(rows), err
}
Expand All @@ -81,13 +82,13 @@
func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row {
start := time.Now()

fields := []any{zap.Any("args", args)}
tx.l.Sugar().With(fields...).Debugf(">>> %s", query)
fields := []any{slog.Any("args", args)}
tx.l.With(fields...).DebugContext(ctx, fmt.Sprintf(">>> %s", query))

Check warning on line 86 in internal/util/fsql/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/util/fsql/tx.go#L85-L86

Added lines #L85 - L86 were not covered by tests

row := tx.sqlTx.QueryRowContext(ctx, query, args...)

fields = append(fields, zap.Duration("time", time.Since(start)), zap.Error(row.Err()))
tx.l.Sugar().With(fields...).Debugf("<<< %s", query)
fields = append(fields, slog.Duration("time", time.Since(start)), logging.Error(row.Err()))
tx.l.With(fields...).DebugContext(ctx, fmt.Sprintf("<<< %s", query))

Check warning on line 91 in internal/util/fsql/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/util/fsql/tx.go#L90-L91

Added lines #L90 - L91 were not covered by tests

return row
}
Expand All @@ -96,21 +97,18 @@
func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
start := time.Now()

fields := []any{zap.Any("args", args)}
tx.l.Sugar().With(fields...).Debugf(">>> %s", query)
fields := []any{slog.Any("args", args)}
tx.l.With(fields...).DebugContext(ctx, fmt.Sprintf(">>> %s", query))

res, err := tx.sqlTx.ExecContext(ctx, query, args...)

// to differentiate between 0 and nil
var ra *int64

if res != nil {
rav, _ := res.RowsAffected()
ra = &rav
ra, _ := res.RowsAffected()
fields = append(fields, slog.Int64("rows", ra))
}

fields = append(fields, zap.Int64p("rows", ra), zap.Duration("time", time.Since(start)), zap.Error(err))
tx.l.Sugar().With(fields...).Debugf("<<< %s", query)
fields = append(fields, slog.Duration("time", time.Since(start)), logging.Error(err))
tx.l.With(fields...).DebugContext(ctx, fmt.Sprintf("<<< %s", query))

return res, err
}