refactor(tracing): tracing & logging

added the custom util functions to handel telemetry
This commit is contained in:
SoXX 2024-08-12 11:50:45 +02:00
parent 135e09ea8e
commit ae7bc131f7

View File

@ -4,12 +4,15 @@ import (
"context"
"embed"
"fmt"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
migrate "github.com/rubenv/sql-migrate"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"gorm.io/driver/postgres"
"gorm.io/gorm"
@ -22,12 +25,28 @@ var (
embedMigrations embed.FS
client *gorm.DB
tracer trace.Tracer
logger *log.Logger
logger = log.New()
)
func Connect(_ context.Context, config models.DatabaseConfig) error {
var localSSL string
func Connect(ctx context.Context, config models.DatabaseConfig) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "Connect")
defer span.End()
localLogger.WithFields(log.Fields{
"endpoint": config.Endpoint,
"port": config.Port,
"database": config.Database,
})
span.SetAttributes(
attribute.String("endpoint", config.Endpoint),
attribute.Int("port", config.Port),
attribute.String("database", config.Database),
)
utils.HandleEvent(span, localLogger, "Starting database connection")
var localSSL string
if config.SSL {
localSSL = "require"
} else {
@ -36,46 +55,58 @@ func Connect(_ context.Context, config models.DatabaseConfig) error {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", config.Endpoint, config.Username, config.Password, config.Database, config.Port, localSSL, config.Timezone)
sqlDB, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return err
return utils.HandleError(ctx, span, localLogger, err)
}
err = migrateDatabase(sqlDB, config)
err = migrateDatabase(ctx, sqlDB, config)
if err != nil {
return err
return utils.HandleError(ctx, span, localLogger, err)
}
setupTelemetry()
client = sqlDB
utils.HandleEvent(span, localLogger, "Database connected successfully")
return nil
}
func migrateDatabase(dbPool *gorm.DB, config models.DatabaseConfig) error {
func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.DatabaseConfig) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "migrateDatabase")
defer span.End()
localLogger.WithFields(log.Fields{
"database": config.Database,
})
span.SetAttributes(
attribute.String("database", config.Database),
)
utils.HandleEvent(span, localLogger, "Starting database migration")
dialect := "postgres"
migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"}
db, err := dbPool.DB()
if err != nil {
return fmt.Errorf("postgres migration: %v", err)
return utils.HandleError(ctx, span, localLogger, err)
}
n, err := migrate.Exec(db, dialect, migrations, migrate.Up)
if err != nil {
return fmt.Errorf("postgres migration: %v", err)
return utils.HandleError(ctx, span, localLogger, err)
}
if config.Debug {
if n != 0 {
log.Infof("postgres migration: applied %d migrations!", n)
localLogger.Infof("postgres migration: applied %d migrations!", n)
} else {
log.Info("postgres migration: nothing to migrate")
localLogger.Info("postgres migration: nothing to migrate")
}
}
utils.HandleEvent(span, localLogger, "Database migration completed successfully")
return nil
}
@ -84,13 +115,18 @@ func setupTelemetry() {
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
}
func GetGorm(ctx context.Context) (*gorm.DB, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetGorm")
defer span.End()
utils.HandleEvent(span, localLogger, "Retrieving GORM client")
if client == nil {
return nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
return nil, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
utils.HandleEvent(span, localLogger, "GORM client retrieved successfully")
return client, nil
}