Skip to content

Commit

Permalink
For instrumented db driver, manually wrap to use RDSPostgresDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Hobson committed Jul 20, 2023
1 parent 2e08a62 commit d956d4d
Showing 1 changed file with 17 additions and 36 deletions.
53 changes: 17 additions & 36 deletions pkg/cli/dbconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"database/sql"
"database/sql/driver"
"fmt"
"os"
"path/filepath"
Expand All @@ -13,7 +14,7 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/rds"
pop "github.com/gobuffalo/pop/v6"
"github.com/luna-duclos/instrumentedsql"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand Down Expand Up @@ -363,47 +364,27 @@ func InitDatabase(v *viper.Viper, creds *credentials.Credentials, logger *zap.Lo
}

if dbUseInstrumentedDriver {
// to fake pop out, we need to register the otelsql instrumented
// driver under the driverName that pop would use. To do that,
// we need to get the otelsql driver.Driver, which is easiest
// to get from sql.DB.Driver()
db, err := sql.Open(dbConnectionDetails.Driver, "")
if err != nil {
logger.Error("Failed opening uninstrumented connection", zap.Error(err))
return nil, err
}
currentDriver := db.Driver()
err = db.Close()
if err != nil {
logger.Error("Failed closing uninstrumented connection", zap.Error(err))
return nil, err
}

// This is the name from pop's instrumented connection code
// https://github.com/gobuffalo/pop/blob/master/connection_instrumented.go#L44
popInstrumentedDriverName := "instrumented-sql-driver-postgres"
instrumentedDriverName := "instrumented-" + dbConnectionDetails.Driver
// and we're going to fake out pop with the Driver so that the
// driver name matches what pop is looking for, but it will
// wind up using the desired driver under a wrapped otelsql connection
dbConnectionDetails.Driver = "postgres"
spanOptions := otelsql.SpanOptions{
Ping: true,
RowsNext: v.GetBool(DbDebugFlag),
Ping: true,
RowsNext: v.GetBool(DbDebugFlag),
OmitConnResetSession: true,
OmitConnectorConnect: true,
}
sql.Register(popInstrumentedDriverName,
otelsql.WrapDriver(currentDriver,
otelsql.WithSpanOptions(spanOptions)))

// now we can update the connection details to indicate we
// want an instrumented connection
dbConnectionDetails.UseInstrumentedDriver = true
// pop expects at least one option when using instrumented
// sql, but the options will be ignored since we are faking
// things out
dbConnectionDetails.InstrumentedDriverOptions = []instrumentedsql.Opt{
instrumentedsql.WithOmitArgs(),
otelDriver := otelsql.WrapDriver(&iampg.RDSPostgresDriver{},
otelsql.WithSpanOptions(spanOptions))
_, ok := otelDriver.(driver.DriverContext)
if ok {
sql.Register(instrumentedDriverName, otelDriver)
sqlx.BindDriver(instrumentedDriverName, sqlx.DOLLAR)
dbConnectionDetails.Driver = instrumentedDriverName
logger.Info("Using otelsql instrumented sql driver")
} else {
logger.Error("Could not wrap otelsql instrumented sql driver")
}
logger.Info("Using otelsql instrumented sql driver")
}

err := dbConnectionDetails.Finalize()
Expand Down

0 comments on commit d956d4d

Please sign in to comment.