mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add logging to pgPubsub (#11953)
Should be helpful for #11950 Adds a logger to pgPubsub and logs various events, most especially connection and disconnection from postgres.
This commit is contained in:
@@ -17,6 +17,8 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"cdr.dev/slog"
|
||||
"cdr.dev/slog/sloggers/slogtest"
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/dbmem"
|
||||
"github.com/coder/coder/v2/coderd/database/postgres"
|
||||
@@ -32,6 +34,7 @@ type options struct {
|
||||
fixedTimezone string
|
||||
dumpOnFailure bool
|
||||
returnSQLDB func(*sql.DB)
|
||||
logger slog.Logger
|
||||
}
|
||||
|
||||
type Option func(*options)
|
||||
@@ -50,6 +53,12 @@ func WithDumpOnFailure() Option {
|
||||
}
|
||||
}
|
||||
|
||||
func WithLogger(logger slog.Logger) Option {
|
||||
return func(o *options) {
|
||||
o.logger = logger
|
||||
}
|
||||
}
|
||||
|
||||
func withReturnSQLDB(f func(*sql.DB)) Option {
|
||||
return func(o *options) {
|
||||
o.returnSQLDB = f
|
||||
@@ -74,7 +83,7 @@ func NewDBWithSQLDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub
|
||||
func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
|
||||
t.Helper()
|
||||
|
||||
var o options
|
||||
o := options{logger: slogtest.Make(t, nil).Named("pubsub").Leveled(slog.LevelDebug)}
|
||||
for _, opt := range opts {
|
||||
opt(&o)
|
||||
}
|
||||
@@ -118,7 +127,7 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
|
||||
}
|
||||
db = database.New(sqlDB)
|
||||
|
||||
ps, err = pubsub.New(context.Background(), sqlDB, connectionURL)
|
||||
ps, err = pubsub.New(context.Background(), o.logger, sqlDB, connectionURL)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
_ = ps.Close()
|
||||
|
||||
Reference in New Issue
Block a user