XinYeYouKu/app/http/trace/trace.go

57 lines
1.3 KiB
Go
Raw Normal View History

2024-06-07 18:24:56 +08:00
package trace
import (
"github.com/openzipkin/zipkin-go"
zkHttp "github.com/openzipkin/zipkin-go/reporter/http"
"log"
"qteam/config"
"sync"
)
var (
tracer *zipkin.Tracer
lock sync.Mutex
)
func Tracer() (*zipkin.Tracer, error) {
if tracer == nil {
// 有err, 不适合用sync.Once做单例
lock.Lock()
defer lock.Unlock()
if tracer == nil {
err := InitTracer(config.GetConf().ServiceName, config.GetConf().SkyWalkingOapServer)
if err != nil {
return nil, err
}
}
}
return tracer, nil
}
func InitTracer(serviceName, skyWalkingOapServer string) error {
zipkinReporter := zkHttp.NewReporter(skyWalkingOapServer)
// create our local service endpoint
endpoint, err := zipkin.NewEndpoint(serviceName, "192.168.110.65:8081")
if err != nil {
log.Fatalf("unable to create local endpoint: %+v\n", err)
}
sampler := zipkin.NewModuloSampler(1)
// Initialize the tracer.
nativeTracer, err := zipkin.NewTracer(
zipkinReporter,
zipkin.WithLocalEndpoint(endpoint),
zipkin.WithSampler(sampler),
)
// initialize our tracer
//nativeTracer, err := zipkin.NewTracer(zipkinReporter, zipkin.WithLocalEndpoint(endpoint))
if err != nil {
log.Fatalf("unable to create tracer: %+v\n", err)
}
// use zipkin-go-opentracing to wrap our tracer
tracer = nativeTracer
return err
}