Skip to content

Commit

Permalink
调整Trace配置名称
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaojun03 committed Sep 16, 2024
1 parent 25e43cc commit 57b1cf2
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 70 deletions.
9 changes: 4 additions & 5 deletions examples/etc/application.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
encrypt_key = "defualt app encrypt key"
cipher_prefix = "@ciphered@"

[app.http]
[http]
enable = false
host = "127.0.0.1"
port = 8010
Expand All @@ -18,12 +18,11 @@
enable_ssl = false
cert_file = ""
key_file = ""
enable_trace = false

[app.http.health_check]
[health_check]
enabled = true

[app.http.cors]
[cors]
enabled = false
cors_allowed_headers = ["*"]
cors_allowed_domains = ["*"]
Expand All @@ -41,7 +40,7 @@
cert_file = ""
key_file = ""
enable_recovery = true
enable_trace = true
trace = true

[app.trace]
enable = false
Expand Down
2 changes: 1 addition & 1 deletion ioc/config/application/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAppEnv(t *testing.T) {
}

func init() {
os.Setenv("HTTP_ENABLE_TRACE", "false")
os.Setenv("HTTP_TRACE", "false")
os.Setenv("APP_NAME", "test")
req := ioc.NewLoadConfigRequest()
err := ioc.ConfigIocObject(req)
Expand Down
30 changes: 15 additions & 15 deletions ioc/config/datasource/grom.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ func init() {
}

var defaultConfig = &dataSource{
Provider: PROVIDER_MYSQL,
Host: "127.0.0.1",
Port: 3306,
DB: application.Get().Name(),
Debug: false,
EnableTrace: true,
Provider: PROVIDER_MYSQL,
Host: "127.0.0.1",
Port: 3306,
DB: application.Get().Name(),
Debug: false,
Trace: true,
}

type dataSource struct {
ioc.ObjectImpl
Provider PROVIDER `json:"provider" yaml:"provider" toml:"provider" env:"PROVIDER"`
Host string `json:"host" yaml:"host" toml:"host" env:"HOST"`
Port int `json:"port" yaml:"port" toml:"port" env:"PORT"`
DB string `json:"database" yaml:"database" toml:"database" env:"DB"`
Username string `json:"username" yaml:"username" toml:"username" env:"USERNAME"`
Password string `json:"password" yaml:"password" toml:"password" env:"PASSWORD"`
Debug bool `json:"debug" yaml:"debug" toml:"debug" env:"DEBUG"`
EnableTrace bool `toml:"enable_trace" json:"enable_trace" yaml:"enable_trace" env:"ENABLE_TRACE"`
Provider PROVIDER `json:"provider" yaml:"provider" toml:"provider" env:"PROVIDER"`
Host string `json:"host" yaml:"host" toml:"host" env:"HOST"`
Port int `json:"port" yaml:"port" toml:"port" env:"PORT"`
DB string `json:"database" yaml:"database" toml:"database" env:"DB"`
Username string `json:"username" yaml:"username" toml:"username" env:"USERNAME"`
Password string `json:"password" yaml:"password" toml:"password" env:"PASSWORD"`
Debug bool `json:"debug" yaml:"debug" toml:"debug" env:"DEBUG"`
Trace bool `toml:"trace" json:"trace" yaml:"trace" env:"TRACE"`

db *gorm.DB
log *zerolog.Logger
Expand All @@ -58,7 +58,7 @@ func (m *dataSource) Init() error {
return err
}

if trace.Get().Enable && m.EnableTrace {
if trace.Get().Enable && m.Trace {
m.log.Info().Msg("enable gorm trace")
if err := db.Use(otelgorm.NewPlugin()); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion ioc/config/datasource/test/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
username = "root"
password = "123456"
debug = false
enable_trace = true
trace = true
16 changes: 11 additions & 5 deletions ioc/config/gin/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,36 @@ import (

func init() {
ioc.Config().Registry(&GinFramework{
EnableRecovery: true,
Recovery: true,
Mode: gin.DebugMode,
Trace: true,
})
}

type GinFramework struct {
ioc.ObjectImpl

Engine *gin.Engine
log *zerolog.Logger

// 开启recovery恢复
EnableRecovery bool `json:"enable_recovery" yaml:"enable_recovery" toml:"enable_recovery" env:"ENABLE_RECOVERY"`
Recovery bool `json:"recovery" yaml:"recovery" toml:"recovery" env:"RECOVERY"`
// Gin模式
Mode string `toml:"mode" json:"mode" yaml:"mode" env:"Mode"`
// 开启Trace
Trace bool `toml:"trace" json:"trace" yaml:"trace" env:"TRACE"`
}

func (g *GinFramework) Init() error {
g.log = log.Sub(g.Name())
g.Engine = gin.Default()
gin.SetMode(g.Mode)

if g.EnableRecovery {
if g.Recovery {
g.log.Info().Msg("enable gin recovery")
g.Engine.Use(gin.Recovery())
}

if http.Get().EnableTrace && trace.Get().Enable {
if g.Trace && trace.Get().Enable {
g.log.Info().Msg("enable gin trace")
g.Engine.Use(otelgin.Middleware(application.Get().GetAppNameWithDefault("default")))
}
Expand Down
10 changes: 7 additions & 3 deletions ioc/config/gorestful/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ import (
)

func init() {
ioc.Config().Registry(&GoRestfulFramework{})
ioc.Config().Registry(&GoRestfulFramework{
Trace: true,
})
}

type GoRestfulFramework struct {
ioc.ObjectImpl

Container *restful.Container
log *zerolog.Logger

// 开启Trace
Trace bool `toml:"trace" json:"trace" yaml:"trace" env:"TRACE"`
}

func (g *GoRestfulFramework) Priority() int {
Expand All @@ -37,7 +41,7 @@ func (g *GoRestfulFramework) Init() error {
restful.DefaultRequestContentType(restful.MIME_JSON)

// 注册路由
if http.Get().EnableTrace && trace.Get().Enable {
if g.Trace && trace.Get().Enable {
g.log.Info().Msg("enable go-restful trace")
g.Container.Filter(otelrestful.OTelFilter(application.Get().GetAppNameWithDefault("default")))
}
Expand Down
18 changes: 9 additions & 9 deletions ioc/config/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func init() {
}

var defaultConfig = &Grpc{
Host: "127.0.0.1",
Port: 18080,
EnableRecovery: true,
EnableTrace: true,
Host: "127.0.0.1",
Port: 18080,
Recovery: true,
Trace: true,
}

type Grpc struct {
Expand All @@ -38,9 +38,9 @@ type Grpc struct {
KeyFile string `json:"key_file" yaml:"key_file" toml:"key_file" env:"KEY_FILE"`

// 开启recovery恢复
EnableRecovery bool `json:"enable_recovery" yaml:"enable_recovery" toml:"enable_recovery" env:"ENABLE_RECOVERY"`
Recovery bool `json:"recovery" yaml:"recovery" toml:"recovery" env:"RECOVERY"`
// 开启Trace
EnableTrace bool `json:"enable_trace" yaml:"enable_trace" toml:"enable_trace" env:"ENABLE_TRACE"`
Trace bool `json:"trace" yaml:"trace" toml:"trace" env:"TRACE"`

// 解析后的数据
interceptors []grpc.UnaryServerInterceptor
Expand Down Expand Up @@ -91,7 +91,7 @@ func (g *Grpc) AddInterceptors(interceptors ...grpc.UnaryServerInterceptor) {
}

func (g *Grpc) Interceptors() (interceptors []grpc.UnaryServerInterceptor) {
if g.EnableRecovery {
if g.Recovery {
interceptors = append(interceptors,
recovery.NewInterceptor(recovery.NewZeroLogRecoveryHandler()).
UnaryServerInterceptor())
Expand All @@ -106,8 +106,8 @@ type ServiceInfoCtxKey struct{}
func (g *Grpc) ServerOpts() []grpc.ServerOption {
opts := []grpc.ServerOption{}
// 补充Trace选项
if trace.Get().Enable && g.EnableTrace {
g.log.Info().Msg("enable mongodb trace")
if trace.Get().Enable && g.Trace {
g.log.Info().Msg("enable grpc trace")
otelgrpc.NewServerHandler()
opts = append(opts, grpc.StatsHandler(otelgrpc.NewServerHandler()))
}
Expand Down
6 changes: 0 additions & 6 deletions ioc/config/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ var defaultConfig = &Http{
WriteTimeoutSecond: 60,
IdleTimeoutSecond: 600,
MaxHeaderSize: "16kb",
EnableTrace: true,
}

type Http struct {
Expand All @@ -55,11 +54,6 @@ type Http struct {
// header最大大小
MaxHeaderSize string `json:"max_header_size" yaml:"max_header_size" toml:"max_header_size" env:"MAX_HEADER_SIZE"`

// 开启Trace
EnableTrace bool `toml:"enable_trace" json:"enable_trace" yaml:"enable_trace" env:"ENABLE_TRACE"`
// 开启Trace
Debug bool `toml:"debug" json:"debug" yaml:"debug" env:"DEBUG"`

// 解析后的数据
maxHeaderBytes uint64
log *zerolog.Logger
Expand Down
22 changes: 11 additions & 11 deletions ioc/config/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ func init() {
}

var defaultConfig = &mongoDB{
Database: application.Get().GetAppNameWithDefault("admin"),
AuthDB: "admin",
Endpoints: []string{"127.0.0.1:27017"},
EnableTrace: true,
Database: application.Get().GetAppNameWithDefault("admin"),
AuthDB: "admin",
Endpoints: []string{"127.0.0.1:27017"},
Trace: true,
}

type mongoDB struct {
Endpoints []string `toml:"endpoints" json:"endpoints" yaml:"endpoints" env:"ENDPOINTS" envSeparator:","`
UserName string `toml:"username" json:"username" yaml:"username" env:"USERNAME"`
Password string `toml:"password" json:"password" yaml:"password" env:"PASSWORD"`
Database string `toml:"database" json:"database" yaml:"database" env:"DATABASE"`
AuthDB string `toml:"auth_db" json:"auth_db" yaml:"auth_db" env:"AUTH_DB"`
EnableTrace bool `toml:"enable_trace" json:"enable_trace" yaml:"enable_trace" env:"ENABLE_TRACE"`
Endpoints []string `toml:"endpoints" json:"endpoints" yaml:"endpoints" env:"ENDPOINTS" envSeparator:","`
UserName string `toml:"username" json:"username" yaml:"username" env:"USERNAME"`
Password string `toml:"password" json:"password" yaml:"password" env:"PASSWORD"`
Database string `toml:"database" json:"database" yaml:"database" env:"DATABASE"`
AuthDB string `toml:"auth_db" json:"auth_db" yaml:"auth_db" env:"AUTH_DB"`
Trace bool `toml:"trace" json:"trace" yaml:"trace" env:"TRACE"`

client *mongo.Client
ioc.ObjectImpl
Expand Down Expand Up @@ -99,7 +99,7 @@ func (m *mongoDB) getClient() (*mongo.Client, error) {
}
opts.SetHosts(m.Endpoints)
opts.SetConnectTimeout(5 * time.Second)
if trace.Get().Enable && m.EnableTrace {
if trace.Get().Enable && m.Trace {
m.log.Info().Msg("enable mongodb trace")
opts.Monitor = otelmongo.NewMonitor(
otelmongo.WithCommandAttributeDisabled(true),
Expand Down
2 changes: 1 addition & 1 deletion ioc/config/mongo/test/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
password = "123456"
database = "admin"
auth_db = "admin"
enable_trace = true
trace = true
22 changes: 11 additions & 11 deletions ioc/config/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ func init() {
}

var defaultConfig = &Redis{
DB: 0,
Endpoints: []string{"127.0.0.1:6379"},
EnableTrace: true,
DB: 0,
Endpoints: []string{"127.0.0.1:6379"},
Trace: true,
}

type Redis struct {
ioc.ObjectImpl
Endpoints []string `toml:"endpoints" json:"endpoints" yaml:"endpoints" env:"ENDPOINTS" envSeparator:","`
DB int `toml:"db" json:"db" yaml:"db" env:"DB"`
UserName string `toml:"username" json:"username" yaml:"username" env:"USERNAME"`
Password string `toml:"password" json:"password" yaml:"password" env:"PASSWORD"`
EnableTrace bool `toml:"enable_trace" json:"enable_trace" yaml:"enable_trace" env:"ENABLE_TRACE"`
EnableMetrics bool `toml:"enable_metrics" json:"enable_metrics" yaml:"enable_metrics" env:"ENABLE_METRICS"`
Endpoints []string `toml:"endpoints" json:"endpoints" yaml:"endpoints" env:"ENDPOINTS" envSeparator:","`
DB int `toml:"db" json:"db" yaml:"db" env:"DB"`
UserName string `toml:"username" json:"username" yaml:"username" env:"USERNAME"`
Password string `toml:"password" json:"password" yaml:"password" env:"PASSWORD"`
Trace bool `toml:"trace" json:"trace" yaml:"trace" env:"TRACE"`
Metric bool `toml:"metric" json:"metric" yaml:"metric" env:"METRIC"`

client redis.UniversalClient
log *zerolog.Logger
Expand All @@ -53,14 +53,14 @@ func (m *Redis) Init() error {
Password: m.Password,
})

if trace.Get().Enable && m.EnableTrace {
if trace.Get().Enable && m.Trace {
m.log.Info().Msg("enable redis trace")
if err := redisotel.InstrumentTracing(rdb); err != nil {
return err
}
}

if m.EnableMetrics {
if m.Metric {
if err := redisotel.InstrumentMetrics(rdb); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions ioc/config/redis/test/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
db = 0
username = ""
password = ""
enable_trace = true
enable_metrics = false
trace = true
metric = false

0 comments on commit 57b1cf2

Please sign in to comment.