mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-10 08:29:58 +08:00
fix(trace): use sync.Once to prevent multiple trace initialization (#5244)
Signed-off-by: kevin <wanjunfeng@gmail.com>
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/lang"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/exporters/jaeger"
|
||||
@@ -30,42 +29,36 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
agents = make(map[string]lang.PlaceholderType)
|
||||
lock sync.Mutex
|
||||
tp *sdktrace.TracerProvider
|
||||
once sync.Once
|
||||
tp *sdktrace.TracerProvider
|
||||
shutdownOnceFn = sync.OnceFunc(func() {
|
||||
if tp != nil {
|
||||
_ = tp.Shutdown(context.Background())
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
// StartAgent starts an opentelemetry agent.
|
||||
// It uses sync.Once to ensure the agent is initialized only once,
|
||||
// similar to prometheus.StartAgent and logx.SetUp.
|
||||
// This prevents multiple ServiceConf.SetUp() calls from reinitializing
|
||||
// the global tracer provider when running multiple servers (e.g., REST + RPC)
|
||||
// in the same process.
|
||||
func StartAgent(c Config) {
|
||||
if c.Disabled {
|
||||
return
|
||||
}
|
||||
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
_, ok := agents[c.Endpoint]
|
||||
if ok {
|
||||
return
|
||||
}
|
||||
|
||||
// if error happens, let later calls run.
|
||||
if err := startAgent(c); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
agents[c.Endpoint] = lang.Placeholder
|
||||
once.Do(func() {
|
||||
if err := startAgent(c); err != nil {
|
||||
logx.Error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// StopAgent shuts down the span processors in the order they were registered.
|
||||
func StopAgent() {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
if tp != nil {
|
||||
_ = tp.Shutdown(context.Background())
|
||||
tp = nil
|
||||
}
|
||||
shutdownOnceFn()
|
||||
}
|
||||
|
||||
func createExporter(c Config) (sdktrace.SpanExporter, error) {
|
||||
|
||||
Reference in New Issue
Block a user