From 5f8e80f7c36840e2ddbe07ebf0f95afcafdcdfe6 Mon Sep 17 00:00:00 2001 From: Pete Loaf Date: Wed, 8 Nov 2023 12:26:43 -0500 Subject: [PATCH 1/4] tracing: skip reconfiguration if tracer already configured --- internalv2compat/compat.go | 17 +++++++++++++++++ tracing/tracer.go | 15 +++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/internalv2compat/compat.go b/internalv2compat/compat.go index 7a113d400..0278b624e 100644 --- a/internalv2compat/compat.go +++ b/internalv2compat/compat.go @@ -8,6 +8,7 @@ package internalv2compat import ( "os" + "sync" "sync/atomic" "go.uber.org/zap" @@ -63,3 +64,19 @@ type IsHTTP interface { type IsThrift interface { isThrift() } + +var ( + v2TracingEnabledMutex sync.RWMutex + v2TracingEnabled bool +) + +func SetV2TracingEnabled(enabled bool) { + v2TracingEnabledMutex.Lock() + defer v2TracingEnabledMutex.Unlock() + v2TracingEnabled = enabled +} +func V2TracingEnabled() bool { + v2TracingEnabledMutex.RLock() + defer v2TracingEnabledMutex.RUnlock() + return v2TracingEnabled +} diff --git a/tracing/tracer.go b/tracing/tracer.go index 77e64baac..6b703c7e4 100644 --- a/tracing/tracer.go +++ b/tracing/tracer.go @@ -12,6 +12,7 @@ import ( "github.com/opentracing/opentracing-go" "github.com/reddit/baseplate.go/detach" + "github.com/reddit/baseplate.go/internalv2compat" "github.com/reddit/baseplate.go/log" "github.com/reddit/baseplate.go/mqsend" "github.com/reddit/baseplate.go/randbp" @@ -82,6 +83,14 @@ type Tracer struct { // If it fails to do so, UndefinedIP will be used instead, // and the error will be logged if logger is non-nil. func InitGlobalTracer(cfg Config) error { + logger := cfg.Logger + if logger == nil { + logger = log.NopWrapper + } + if internalv2compat.V2TracingEnabled() { + logger(context.Background(), `v2 tracing is enabled; skipping v0 tracer configuration`) + return nil + } var tracer Tracer if cfg.QueueName != "" { if cfg.MaxQueueSize <= 0 || cfg.MaxQueueSize > MaxQueueSize { @@ -102,13 +111,7 @@ func InitGlobalTracer(cfg Config) error { tracer.sampleRate = cfg.SampleRate tracer.useHex = cfg.UseHex - - logger := cfg.Logger - if logger == nil { - logger = log.NopWrapper - } tracer.logger = logger - tracer.maxRecordTimeout = cfg.MaxRecordTimeout ip, err := runtimebp.GetFirstIPv4() From 8d65933fc6127438f15ad63752493400a02bd0b8 Mon Sep 17 00:00:00 2001 From: Pete Loaf Date: Wed, 8 Nov 2023 13:39:09 -0500 Subject: [PATCH 2/4] plain mutex and anonymous struct --- internalv2compat/compat.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/internalv2compat/compat.go b/internalv2compat/compat.go index 0278b624e..38c5575a7 100644 --- a/internalv2compat/compat.go +++ b/internalv2compat/compat.go @@ -65,18 +65,18 @@ type IsThrift interface { isThrift() } -var ( - v2TracingEnabledMutex sync.RWMutex - v2TracingEnabled bool -) +var v2Tracing struct { + sync.Mutex + enabled bool +} func SetV2TracingEnabled(enabled bool) { - v2TracingEnabledMutex.Lock() - defer v2TracingEnabledMutex.Unlock() - v2TracingEnabled = enabled + v2Tracing.Lock() + defer v2Tracing.Unlock() + v2Tracing.enabled = enabled } func V2TracingEnabled() bool { - v2TracingEnabledMutex.RLock() - defer v2TracingEnabledMutex.RUnlock() - return v2TracingEnabled + v2Tracing.Lock() + defer v2Tracing.Unlock() + return v2Tracing.enabled } From 0631efd6055ab917b9ede292140de7242c3f8a1e Mon Sep 17 00:00:00 2001 From: Pete Loaf Date: Wed, 8 Nov 2023 13:41:26 -0500 Subject: [PATCH 3/4] lint ignore SA1019 --- tracing/tracer.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tracing/tracer.go b/tracing/tracer.go index 6b703c7e4..1a2ac5b60 100644 --- a/tracing/tracer.go +++ b/tracing/tracer.go @@ -12,6 +12,7 @@ import ( "github.com/opentracing/opentracing-go" "github.com/reddit/baseplate.go/detach" + //lint:ignore SA1019 This library is internal only, not actually deprecated "github.com/reddit/baseplate.go/internalv2compat" "github.com/reddit/baseplate.go/log" "github.com/reddit/baseplate.go/mqsend" From e61974fd63687023ff140f07a35313b21e422570 Mon Sep 17 00:00:00 2001 From: Pete Loaf Date: Wed, 8 Nov 2023 13:47:26 -0500 Subject: [PATCH 4/4] adjust logging --- tracing/tracer.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tracing/tracer.go b/tracing/tracer.go index 1a2ac5b60..a5234c3d6 100644 --- a/tracing/tracer.go +++ b/tracing/tracer.go @@ -84,12 +84,8 @@ type Tracer struct { // If it fails to do so, UndefinedIP will be used instead, // and the error will be logged if logger is non-nil. func InitGlobalTracer(cfg Config) error { - logger := cfg.Logger - if logger == nil { - logger = log.NopWrapper - } if internalv2compat.V2TracingEnabled() { - logger(context.Background(), `v2 tracing is enabled; skipping v0 tracer configuration`) + cfg.Logger.Log(context.Background(), `v2 tracing is enabled; skipping v0 tracer configuration`) return nil } var tracer Tracer @@ -112,7 +108,13 @@ func InitGlobalTracer(cfg Config) error { tracer.sampleRate = cfg.SampleRate tracer.useHex = cfg.UseHex + + logger := cfg.Logger + if logger == nil { + logger = log.NopWrapper + } tracer.logger = logger + tracer.maxRecordTimeout = cfg.MaxRecordTimeout ip, err := runtimebp.GetFirstIPv4()