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

Unexport DB init helpers, add control for observability #19

Merged
merged 1 commit into from
Nov 16, 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
4 changes: 4 additions & 0 deletions database/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package database

Check notice on line 1 in database/config.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

File is not covered by tests.

import "time"

Expand All @@ -11,4 +11,8 @@
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 @@
"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 @@
}
}

// 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 @@
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 @@
}),

// 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 @@
// 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 @@

// 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 @@

// 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
Loading