Skip to content

Commit

Permalink
Unexport DB init helpers, add control for observability
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Nov 16, 2024
1 parent 12dfab0 commit 62bbc08
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
4 changes: 4 additions & 0 deletions database/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ type Config struct {
MaxOpen int `split_words:"true" default:"5"`
InitConn bool `split_words:"true"`
ApplyMigrations bool `split_words:"true"`

// MethodSkipPackages provides helper package paths to skip when identifying method name for observability.
// Item example: "github.com/jmoiron/sqlx".
MethodSkipPackages []string
}
33 changes: 19 additions & 14 deletions database/observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"go.opencensus.io/trace"
)

// WithTracing instruments database connector with OpenCensus tracing.
func WithTracing(dbConnector driver.Connector) driver.Connector {
// withTracing instruments database connector with OpenCensus tracing.
func withTracing(dbConnector driver.Connector) driver.Connector {

Check notice on line 18 in database/observability.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

1 statement(s) on lines 18:20 are not covered by tests.
return ocsql.WrapConnector(dbConnector, tracingOptions()...)
}

// DriverNameWithTracing registers database driver name with OpenCensus tracing.
func DriverNameWithTracing(driverName string) (string, error) {
// driverNameWithTracing registers database driver name with OpenCensus tracing.
func driverNameWithTracing(driverName string) (string, error) {

Check notice on line 23 in database/observability.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

1 statement(s) on lines 23:25 are not covered by tests.
return ocsql.Register(driverName, tracingOptions()...)
}

Expand All @@ -34,17 +34,17 @@ func tracingOptions() []ocsql.TraceOption {
}
}

// WithQueriesLogging instruments database connector with query logging.
func WithQueriesLogging(dbConnector driver.Connector, logger ctxd.Logger, statsTracker stats.Tracker) driver.Connector {
return dbwrap.WrapConnector(dbConnector, wrapOptions(logger, statsTracker)...)
// withQueriesLogging instruments database connector with query logging.
func withQueriesLogging(cfg Config, dbConnector driver.Connector, logger ctxd.Logger, statsTracker stats.Tracker) driver.Connector {
return dbwrap.WrapConnector(dbConnector, wrapOptions(logger, statsTracker, cfg.MethodSkipPackages)...)

Check notice on line 39 in database/observability.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

1 statement(s) on lines 38:40 are not covered by tests.
}

// DriverNameWithQueriesLogging registers database driver name with query logging.
func DriverNameWithQueriesLogging(driverName string, logger ctxd.Logger, statsTracker stats.Tracker) (string, error) {
return dbwrap.Register(driverName, wrapOptions(logger, statsTracker)...)
// driverNameWithQueriesLogging registers database driver name with query logging.
func driverNameWithQueriesLogging(cfg Config, driverName string, logger ctxd.Logger, statsTracker stats.Tracker) (string, error) {
return dbwrap.Register(driverName, wrapOptions(logger, statsTracker, cfg.MethodSkipPackages)...)

Check notice on line 44 in database/observability.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

1 statement(s) on lines 43:45 are not covered by tests.
}

func wrapOptions(logger ctxd.Logger, statsTracker stats.Tracker) []dbwrap.Option {
func wrapOptions(logger ctxd.Logger, statsTracker stats.Tracker, skipPackages []string) []dbwrap.Option {

Check notice on line 47 in database/observability.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

1 statement(s) on lines 47:48 are not covered by tests.
if logger == nil {
logger = ctxd.NoOpLogger{}
}
Expand All @@ -53,11 +53,11 @@ func wrapOptions(logger ctxd.Logger, statsTracker stats.Tracker) []dbwrap.Option
statsTracker = stats.NoOp{}
}

skipPackages := []string{
skipPackages = append([]string{

Check notice on line 56 in database/observability.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

2 statement(s) on lines 56:64 are not covered by tests.
"github.com/Masterminds/squirrel",
"github.com/bool64/sqluct",
"github.com/jmoiron/sqlx",
}
}, skipPackages...)

Check notice on line 60 in database/observability.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

2 statement(s) on lines 56:64 are not covered by tests.

return []dbwrap.Option{
// This interceptor enables reverse debugging from DB side.
Expand All @@ -70,7 +70,7 @@ func wrapOptions(logger ctxd.Logger, statsTracker stats.Tracker) []dbwrap.Option
}),

// This option limits middleware applicability.
dbwrap.WithOperations(dbwrap.Query, dbwrap.StmtQuery, dbwrap.Exec, dbwrap.StmtExec),
dbwrap.WithOperations(dbwrap.Query, dbwrap.StmtQuery, dbwrap.Exec, dbwrap.StmtExec, dbwrap.RowsClose),

// This middleware logs statements with arguments at DEBUG level and counts stats.
dbwrap.WithMiddleware(observe(logger, statsTracker, skipPackages)),
Expand All @@ -87,6 +87,11 @@ func observe(logger ctxd.Logger, statsTracker stats.Tracker, skipPackages []stri
// Closest caller in the stack with package not equal to listed and to "database/sql".
caller := dbwrap.Caller(skipPackages...)

if operation == dbwrap.RowsClose {
statsTracker.Add(ctx, "sql_storage_rows_close", 1, "method", caller)
return

Check failure on line 92 in database/observability.go

View workflow job for this annotation

GitHub Actions / golangci-lint

naked return in func `observe.<func():81>` with 50 lines of code (nakedret)
}

Check notice on line 93 in database/observability.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

4 statement(s) on lines 86:93 are not covered by tests.

ctx, span := trace.StartSpan(ctx, caller+":"+string(operation))
span.AddAttributes(
trace.StringAttribute("stmt", statement),
Expand Down
8 changes: 4 additions & 4 deletions database/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (

// SetupStorage initializes database pool and prepares storage.
func SetupStorage(cfg Config, logger ctxd.Logger, statsTracker stats.Tracker, conn driver.Connector, migrations fs.FS) (*sqluct.Storage, error) {
conn = WithTracing(conn)
conn = WithQueriesLogging(conn, logger, statsTracker)
conn = withTracing(conn)
conn = withQueriesLogging(cfg, conn, logger, statsTracker)

Check notice on line 24 in database/storage.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

4 statement(s) on lines 22:29 are not covered by tests.

db := sql.OpenDB(conn)

Expand All @@ -30,12 +30,12 @@ func SetupStorage(cfg Config, logger ctxd.Logger, statsTracker stats.Tracker, co

// SetupStorageDSN initializes database pool and prepares storage.
func SetupStorageDSN(cfg Config, logger ctxd.Logger, statsTracker stats.Tracker, migrations fs.FS) (*sqluct.Storage, error) {
wrapName, err := DriverNameWithTracing(cfg.DriverName)
wrapName, err := driverNameWithTracing(cfg.DriverName)
if err != nil {
return nil, err
}

wrapName, err = DriverNameWithQueriesLogging(wrapName, logger, statsTracker)
wrapName, err = driverNameWithQueriesLogging(cfg, wrapName, logger, statsTracker)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 62bbc08

Please sign in to comment.