Skip to content

Commit

Permalink
chore: add db tracing only if datadog is set
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Aug 21, 2023
1 parent 148fd65 commit 2b959aa
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

echologrus "github.com/davrux/echo-logrus/v4"
"github.com/glebarez/sqlite"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/labstack/echo/v4"
Expand All @@ -23,7 +24,6 @@ import (
"gorm.io/gorm"

"github.com/jackc/pgx/v5/stdlib"
sqlite3 "github.com/mattn/go-sqlite3"
sqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql"
gormtrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/gorm.io/gorm.v1"
)
Expand All @@ -41,26 +41,39 @@ func main() {
var db *gorm.DB
var sqlDb *sql.DB
if strings.HasPrefix(cfg.DatabaseUri, "postgres://") || strings.HasPrefix(cfg.DatabaseUri, "postgresql://") || strings.HasPrefix(cfg.DatabaseUri, "unix://") {
sqltrace.Register("pgx", &stdlib.Driver{}, sqltrace.WithServiceName("nostr-wallet-connect"))
sqlDb, err = sqltrace.Open("pgx", cfg.DatabaseUri)
if err != nil {
log.Fatalf("Failed to open DB %v", err)
}
db, err = gormtrace.Open(postgres.New(postgres.Config{Conn: sqlDb}), &gorm.Config{})
if err != nil {
log.Fatalf("Failed to open DB %v", err)
if os.Getenv("DATADOG_AGENT_URL") != "" {
sqltrace.Register("pgx", &stdlib.Driver{}, sqltrace.WithServiceName("nostr-wallet-connect"))
sqlDb, err = sqltrace.Open("pgx", cfg.DatabaseUri)
if err != nil {
log.Fatalf("Failed to open DB %v", err)
}
db, err = gormtrace.Open(postgres.New(postgres.Config{Conn: sqlDb}), &gorm.Config{})
if err != nil {
log.Fatalf("Failed to open DB %v", err)
}
} else {
db, err = gorm.Open(postgres.Open(cfg.DatabaseUri), &gorm.Config{})
if err != nil {
log.Fatalf("Failed to open DB %v", err)
}
sqlDb, err = db.DB()
if err != nil {
log.Fatalf("Failed to set DB config: %v", err)
}
}
} else {
sqltrace.Register("sqlite", &sqlite3.SQLiteDriver{}, sqltrace.WithServiceName("sqlite-example"))
sqlDb, err = sqltrace.Open("sqlite", cfg.DatabaseUri)
db, err = gorm.Open(sqlite.Open(cfg.DatabaseUri), &gorm.Config{})
if err != nil {
log.Fatalf("Failed to open DB %v", err)
}
db, err = gormtrace.Open(postgres.New(postgres.Config{Conn: sqlDb}), &gorm.Config{})
// Override SQLite config to max one connection
cfg.DatabaseMaxConns = 1
// Enable foreign keys for sqlite
db.Exec("PRAGMA foreign_keys=ON;")
sqlDb, err = db.DB()
if err != nil {
log.Fatalf("Failed to set DB config: %v", err)
}
}
sqlDb.SetMaxOpenConns(cfg.DatabaseMaxConns)
sqlDb.SetMaxIdleConns(cfg.DatabaseMaxIdleConns)
Expand Down

0 comments on commit 2b959aa

Please sign in to comment.