feat: 添加高级配置选项,增强追踪提供者功能,支持采样器、Reporter 和标签配置

This commit is contained in:
Rogee
2025-09-11 17:02:31 +08:00
parent 42214ee821
commit 1f17942665
3 changed files with 369 additions and 16 deletions

View File

@@ -1,11 +1,9 @@
package tracing
import (
"io"
"time"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
config "github.com/uber/jaeger-client-go/config"
"go.ipao.vip/atom/container"
@@ -19,39 +17,59 @@ func Provide(opts ...opt.Option) error {
return err
}
conf.format()
return container.Container.Provide(func() (opentracing.Tracer, io.Closer, error) {
return container.Container.Provide(func() (opentracing.Tracer, error) {
log := logrus.New()
log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
log.SetFormatter(&logrus.JSONFormatter{TimestampFormat: time.RFC3339})
cfg := &config.Configuration{
ServiceName: conf.Name,
Disabled: conf.Disabled,
Sampler: &config.SamplerConfig{
Type: "const",
Param: 1,
Type: conf.Sampler_Type,
Param: conf.Sampler_Param,
SamplingServerURL: conf.Sampler_SamplingServerURL,
MaxOperations: conf.Sampler_MaxOperations,
SamplingRefreshInterval: time.Duration(conf.Sampler_RefreshIntervalSec) * time.Second,
},
Reporter: &config.ReporterConfig{
LogSpans: true,
LogSpans: func() bool {
if conf.Reporter_LogSpans == nil {
return true
}
return *conf.Reporter_LogSpans
}(),
LocalAgentHostPort: conf.Reporter_LocalAgentHostPort,
CollectorEndpoint: conf.Reporter_CollectorEndpoint,
BufferFlushInterval: 100 * time.Millisecond,
QueueSize: 1000,
BufferFlushInterval: time.Duration(conf.Reporter_BufferFlushMs) * time.Millisecond,
QueueSize: conf.Reporter_QueueSize,
},
RPCMetrics: conf.RPCMetrics,
Gen128Bit: conf.Gen128Bit,
}
if len(conf.Tags) > 0 {
cfg.Tags = make([]opentracing.Tag, 0, len(conf.Tags))
for k, v := range conf.Tags {
cfg.Tags = append(cfg.Tags, opentracing.Tag{Key: k, Value: v})
}
}
// 使 logger
jLogger := &jaegerLogrus{logger: log}
zipkinShared := conf.ZipkinSharedRPCSpan
if !zipkinShared {
zipkinShared = true
}
tracer, closer, err := cfg.NewTracer(
config.Logger(jLogger),
config.ZipkinSharedRPCSpan(true),
config.ZipkinSharedRPCSpan(zipkinShared),
)
if err != nil {
return nil, nil, errors.Wrapf(err, "无法初始化 Jaeger: %v", err)
log.Errorf("无法初始化 Jaeger: %v", err)
return opentracing.NoopTracer{}, nil
}
opentracing.SetGlobalTracer(tracer)
container.AddCloseAble(func() { _ = closer.Close() })
return tracer, closer, nil
return tracer, nil
}, o.DiOptions()...)
}