feat: ent, gorm support otel trace and metrics.

This commit is contained in:
tx7do
2024-04-28 20:41:10 +08:00
parent 902a0ce860
commit 8d39ab89d9
7 changed files with 133 additions and 31 deletions

View File

@@ -5,6 +5,8 @@ import (
"gorm.io/gorm"
"gorm.io/plugin/opentelemetry/tracing"
"gorm.io/driver/clickhouse"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
@@ -18,14 +20,14 @@ type Client struct {
err error
}
func NewClient(driverName, dsn string, enableMigrate bool, gormCfg *gorm.Config) *Client {
func NewClient(driverName, dsn string, enableMigrate, enableTrace, enableMetrics bool, gormCfg *gorm.Config) *Client {
c := &Client{}
if gormCfg == nil {
gormCfg = &gorm.Config{}
}
c.err = c.createGormClient(driverName, dsn, enableMigrate, gormCfg)
c.err = c.createGormClient(driverName, dsn, enableMigrate, enableTrace, enableMetrics, gormCfg)
return c
}
@@ -35,7 +37,7 @@ func (c *Client) Error() error {
}
// createGormClient 创建GORM的客户端
func (c *Client) createGormClient(driverName, dsn string, enableMigrate bool, gormCfg *gorm.Config) error {
func (c *Client) createGormClient(driverName, dsn string, enableMigrate, enableTrace, enableMetrics bool, gormCfg *gorm.Config) error {
var driver gorm.Dialector
switch driverName {
default:
@@ -62,6 +64,17 @@ func (c *Client) createGormClient(driverName, dsn string, enableMigrate bool, go
return fmt.Errorf("failed opening connection to db: %v", err)
}
if enableTrace {
var opts []tracing.Option
if enableMetrics {
opts = append(opts, tracing.WithoutMetrics())
}
if err = client.Use(tracing.NewPlugin(opts...)); err != nil {
return fmt.Errorf("failed opening connection to db: %v", err)
}
}
// 运行数据库迁移工具
if enableMigrate {
if err = client.AutoMigrate(