From 5c7f814d6f42e2f9ae99cb960e96e701e972d8f6 Mon Sep 17 00:00:00 2001 From: Saumya Shah Date: Tue, 12 Nov 2024 22:19:46 +0530 Subject: [PATCH] fix: use sync.Once to stop reinitializing gRPCTarget expvar in parallel executions Signed-off-by: Saumya Shah --- cmd/agent/app/reporter/connect_metrics.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/agent/app/reporter/connect_metrics.go b/cmd/agent/app/reporter/connect_metrics.go index 833f0c67f07..d0f6f5770c1 100644 --- a/cmd/agent/app/reporter/connect_metrics.go +++ b/cmd/agent/app/reporter/connect_metrics.go @@ -5,6 +5,7 @@ package reporter import ( "expvar" + "sync" "github.com/jaegertracing/jaeger/pkg/metrics" ) @@ -23,17 +24,21 @@ type ConnectMetrics struct { target *expvar.String } +var ( + gRPCTarget *expvar.String + gRPCTargetOnce sync.Once +) + // NewConnectMetrics will be initialize ConnectMetrics func NewConnectMetrics(mf metrics.Factory) *ConnectMetrics { cm := &ConnectMetrics{} metrics.MustInit(&cm.metrics, mf.Namespace(metrics.NSOptions{Name: "connection_status"}), nil) - if r := expvar.Get("gRPCTarget"); r == nil { - cm.target = expvar.NewString("gRPCTarget") - } else { - cm.target = r.(*expvar.String) - } - + // expvar.String is not thread-safe, so we need to initialize it only once + gRPCTargetOnce.Do(func() { + gRPCTarget = expvar.NewString("gRPCTarget") + }) + cm.target = gRPCTarget return cm }