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"
|
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed migrations/*.sql
|
const tracingName = "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/database"
|
||||||
var embedMigrations embed.FS
|
|
||||||
var client *gorm.DB
|
var (
|
||||||
|
//go:embed migrations/*.sql
|
||||||
|
embedMigrations embed.FS
|
||||||
|
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