feat: add tracing
This commit is contained in:
47
templates/project/providers/tracing/config.go
Normal file
47
templates/project/providers/tracing/config.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package tracing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.ipao.vip/rogeecn/atom/container"
|
||||||
|
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
const DefaultPrefix = "Tracing"
|
||||||
|
|
||||||
|
func DefaultProvider() container.ProviderContainer {
|
||||||
|
return container.ProviderContainer{
|
||||||
|
Provider: Provide,
|
||||||
|
Options: []opt.Option{
|
||||||
|
opt.Prefix(DefaultPrefix),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 自定义的 Logger 实现
|
||||||
|
type jaegerLogrus struct {
|
||||||
|
logger *logrus.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *jaegerLogrus) Error(msg string) {
|
||||||
|
l.logger.Error(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *jaegerLogrus) Infof(msg string, args ...interface{}) {
|
||||||
|
l.logger.Infof(msg, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Name string
|
||||||
|
Reporter_LocalAgentHostPort string //: "127.0.0.1:6831",
|
||||||
|
Reporter_CollectorEndpoint string //: "http://127.0.0.1:14268/api/traces",
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) format() {
|
||||||
|
if c.Reporter_LocalAgentHostPort == "" {
|
||||||
|
c.Reporter_LocalAgentHostPort = "127.0.0.1:6831"
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.Reporter_CollectorEndpoint == "" {
|
||||||
|
c.Reporter_CollectorEndpoint = "http://127.0.0.1:14268/api/traces"
|
||||||
|
}
|
||||||
|
}
|
||||||
57
templates/project/providers/tracing/provider.go
Normal file
57
templates/project/providers/tracing/provider.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package tracing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.ipao.vip/rogeecn/atom/container"
|
||||||
|
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||||
|
opentracing "github.com/opentracing/opentracing-go"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
config "github.com/uber/jaeger-client-go/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Provide(opts ...opt.Option) error {
|
||||||
|
o := opt.New(opts...)
|
||||||
|
var conf Config
|
||||||
|
if err := o.UnmarshalConfig(&conf); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
conf.format()
|
||||||
|
return container.Container.Provide(func() (opentracing.Tracer, io.Closer, error) {
|
||||||
|
log := logrus.New()
|
||||||
|
log.SetFormatter(&logrus.TextFormatter{
|
||||||
|
FullTimestamp: true,
|
||||||
|
TimestampFormat: "2006-01-02 15:04:05",
|
||||||
|
})
|
||||||
|
|
||||||
|
cfg := &config.Configuration{
|
||||||
|
ServiceName: conf.Name,
|
||||||
|
Sampler: &config.SamplerConfig{
|
||||||
|
Type: "const",
|
||||||
|
Param: 1,
|
||||||
|
},
|
||||||
|
Reporter: &config.ReporterConfig{
|
||||||
|
LogSpans: true,
|
||||||
|
LocalAgentHostPort: "127.0.0.1:6831",
|
||||||
|
CollectorEndpoint: "http://127.0.0.1:14268/api/traces",
|
||||||
|
BufferFlushInterval: 100 * time.Millisecond,
|
||||||
|
QueueSize: 1000,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用自定义的 logger
|
||||||
|
jLogger := &jaegerLogrus{logger: log}
|
||||||
|
tracer, closer, err := cfg.NewTracer(
|
||||||
|
config.Logger(jLogger),
|
||||||
|
config.ZipkinSharedRPCSpan(true),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, errors.Wrapf(err, "无法初始化 Jaeger: %v", err)
|
||||||
|
}
|
||||||
|
opentracing.SetGlobalTracer(tracer)
|
||||||
|
|
||||||
|
return tracer, closer, nil
|
||||||
|
}, o.DiOptions()...)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user