feat(tracing): Implement Jaeger/OpenTracing provider with configuration options

- Added Punycode encoding implementation for cookie handling.
- Introduced serialization for cookie jar with JSON support.
- Created a comprehensive README for the tracing provider, detailing configuration and usage.
- Developed a configuration structure for tracing, including sampler and reporter settings.
- Implemented the provider logic to initialize Jaeger tracer with logging capabilities.
- Ensured graceful shutdown of the tracer on application exit.
This commit is contained in:
Rogee
2025-09-12 17:28:25 +08:00
parent 202239795b
commit 342f205b5e
37 changed files with 89 additions and 352 deletions

View File

@@ -0,0 +1,152 @@
package redis
import (
"fmt"
"time"
"github.com/redis/go-redis/v9"
"go.ipao.vip/atom/container"
"go.ipao.vip/atom/opt"
)
const DefaultPrefix = "Redis"
func DefaultProvider() container.ProviderContainer {
return container.ProviderContainer{
Provider: Provide,
Options: []opt.Option{
opt.Prefix(DefaultPrefix),
},
}
}
type Config struct {
Host string
Port uint
Password string
DB uint
Username string
ClientName string
// Pool & retries
PoolSize int
MinIdleConns int
MaxRetries int
// Timeouts (seconds); 0 = library default
DialTimeoutSeconds uint
ReadTimeoutSeconds uint
WriteTimeoutSeconds uint
ConnMaxIdleTimeSeconds uint
ConnMaxLifetimeSeconds uint
PingTimeoutSeconds uint
}
func (c *Config) format() {
if c.Host == "" {
c.Host = "localhost"
}
if c.Port == 0 {
c.Port = 6379
}
if c.DB == 0 {
c.DB = 0
}
if c.PingTimeoutSeconds == 0 {
c.PingTimeoutSeconds = 5
}
}
func (c *Config) Addr() string {
return fmt.Sprintf("%s:%d", c.Host, c.Port)
}
// DialTimeout returns the dial timeout duration, 0 means library default
func (c *Config) DialTimeout() time.Duration {
if c.DialTimeoutSeconds == 0 {
return 0
}
return time.Duration(c.DialTimeoutSeconds) * time.Second
}
// ReadTimeout returns the read timeout duration, 0 means library default
func (c *Config) ReadTimeout() time.Duration {
if c.ReadTimeoutSeconds == 0 {
return 0
}
return time.Duration(c.ReadTimeoutSeconds) * time.Second
}
// WriteTimeout returns the write timeout duration, 0 means library default
func (c *Config) WriteTimeout() time.Duration {
if c.WriteTimeoutSeconds == 0 {
return 0
}
return time.Duration(c.WriteTimeoutSeconds) * time.Second
}
// ConnMaxIdleTime returns the max idle time for a connection, 0 means default
func (c *Config) ConnMaxIdleTime() time.Duration {
if c.ConnMaxIdleTimeSeconds == 0 {
return 0
}
return time.Duration(c.ConnMaxIdleTimeSeconds) * time.Second
}
// ConnMaxLifetime returns the max lifetime for a connection, 0 means default
func (c *Config) ConnMaxLifetime() time.Duration {
if c.ConnMaxLifetimeSeconds == 0 {
return 0
}
return time.Duration(c.ConnMaxLifetimeSeconds) * time.Second
}
// PingTimeout returns the ping timeout duration, default 5s
func (c *Config) PingTimeout() time.Duration {
if c.PingTimeoutSeconds == 0 {
return 5 * time.Second
}
return time.Duration(c.PingTimeoutSeconds) * time.Second
}
// Options builds a redis.Options based on the config values.
// Only non-zero/meaningful values are set, to preserve library defaults.
func (c *Config) Options() *redis.Options {
c.format()
ro := &redis.Options{
Addr: c.Addr(),
Username: c.Username,
Password: c.Password,
DB: int(c.DB),
ClientName: c.ClientName,
}
if c.PoolSize > 0 {
ro.PoolSize = c.PoolSize
}
if c.MinIdleConns > 0 {
ro.MinIdleConns = c.MinIdleConns
}
if c.MaxRetries > 0 {
ro.MaxRetries = c.MaxRetries
}
if dt := c.DialTimeout(); dt > 0 {
ro.DialTimeout = dt
}
if rt := c.ReadTimeout(); rt > 0 {
ro.ReadTimeout = rt
}
if wt := c.WriteTimeout(); wt > 0 {
ro.WriteTimeout = wt
}
if it := c.ConnMaxIdleTime(); it > 0 {
ro.ConnMaxIdleTime = it
}
if lt := c.ConnMaxLifetime(); lt > 0 {
ro.ConnMaxLifetime = lt
}
return ro
}

View File

@@ -0,0 +1,48 @@
package redis
import (
"context"
"github.com/redis/go-redis/v9"
log "github.com/sirupsen/logrus"
"go.ipao.vip/atom/container"
"go.ipao.vip/atom/opt"
)
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config Config
if err := o.UnmarshalConfig(&config); err != nil {
return err
}
config.format()
return container.Container.Provide(func() (redis.UniversalClient, error) {
// Build options via config helper (encapsulates defaults/decisions)
ro := config.Options()
// Safe structured log (no password)
log.WithFields(log.Fields{
"addr": ro.Addr,
"db": ro.DB,
"user": ro.Username,
"client_name": ro.ClientName,
"pool_size": ro.PoolSize,
"min_idle": ro.MinIdleConns,
"retries": ro.MaxRetries,
}).Info("opening Redis connection")
rdb := redis.NewClient(ro)
// ping to verify connectivity
ctx, cancel := context.WithTimeout(context.Background(), config.PingTimeout())
defer cancel()
if _, err := rdb.Ping(ctx).Result(); err != nil {
return nil, err
}
// close hook
container.AddCloseAble(func() { _ = rdb.Close() })
return rdb, nil
}, o.DiOptions()...)
}