feat(telemetry): Implement telemetry setup in database client
Add OpenTelemetry integration to the database client by creating a `setupTelemetry` function. Initialize a tracer and configure logging with the otellogrus hook. Call this function during the connection process to enable tracing and logging for database operations.
This commit is contained in:
parent
e839e7ba2a
commit
2d9db01d72
@ -8,13 +8,22 @@ import (
|
||||
"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/trace"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const tracingName = "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/database"
|
||||
|
||||
var (
|
||||
//go:embed migrations/*.sql
|
||||
var embedMigrations embed.FS
|
||||
var client *gorm.DB
|
||||
embedMigrations embed.FS
|
||||
client *gorm.DB
|
||||
tracer trace.Tracer
|
||||
logger *log.Logger
|
||||
)
|
||||
|
||||
func Connect(_ context.Context, config models.DatabaseConfig) error {
|
||||
var localSSL string
|
||||
@ -32,12 +41,14 @@ func Connect(_ context.Context, config models.DatabaseConfig) error {
|
||||
return err
|
||||
}
|
||||
|
||||
client = sqlDB
|
||||
err = migrateDatabase(sqlDB, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
setupTelemetry()
|
||||
|
||||
client = sqlDB
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -68,6 +79,14 @@ func migrateDatabase(dbPool *gorm.DB, config models.DatabaseConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func setupTelemetry() {
|
||||
tracer = otel.Tracer(tracingName)
|
||||
|
||||
hook := otellogrus.NewHook(tracingName)
|
||||
logger.AddHook(hook)
|
||||
|
||||
}
|
||||
|
||||
func GetGorm(ctx context.Context) (*gorm.DB, error) {
|
||||
if client == nil {
|
||||
return nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
|
||||
|
Loading…
Reference in New Issue
Block a user