SDK v3 #8
@ -8,13 +8,22 @@ import (
|
|||||||
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
|
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
|
||||||
migrate "github.com/rubenv/sql-migrate"
|
migrate "github.com/rubenv/sql-migrate"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"go.opentelemetry.io/contrib/bridges/otellogrus"
|
||||||
|
"go.opentelemetry.io/otel"
|
||||||
|
"go.opentelemetry.io/otel/trace"
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const tracingName = "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/database"
|
||||||
|
|
||||||
|
var (
|
||||||
//go:embed migrations/*.sql
|
//go:embed migrations/*.sql
|
||||||
var embedMigrations embed.FS
|
embedMigrations embed.FS
|
||||||
var client *gorm.DB
|
client *gorm.DB
|
||||||
|
tracer trace.Tracer
|
||||||
|
logger *log.Logger
|
||||||
|
)
|
||||||
|
|
||||||
func Connect(_ context.Context, config models.DatabaseConfig) error {
|
func Connect(_ context.Context, config models.DatabaseConfig) error {
|
||||||
var localSSL string
|
var localSSL string
|
||||||
@ -32,12 +41,14 @@ func Connect(_ context.Context, config models.DatabaseConfig) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
client = sqlDB
|
|
||||||
err = migrateDatabase(sqlDB, config)
|
err = migrateDatabase(sqlDB, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setupTelemetry()
|
||||||
|
|
||||||
|
client = sqlDB
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,6 +79,14 @@ func migrateDatabase(dbPool *gorm.DB, config models.DatabaseConfig) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setupTelemetry() {
|
||||||
|
tracer = otel.Tracer(tracingName)
|
||||||
|
|
||||||
|
hook := otellogrus.NewHook(tracingName)
|
||||||
|
logger.AddHook(hook)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func GetGorm(ctx context.Context) (*gorm.DB, error) {
|
func GetGorm(ctx context.Context) (*gorm.DB, error) {
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
|
return nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
|
||||||
|
Loading…
Reference in New Issue
Block a user