Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add mutex to internal trace otel test variable #9390

Merged
merged 3 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions internal/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os"
"strings"
"sync"

"go.opencensus.io/trace"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -50,17 +51,23 @@ const (
)

var (
// OpenTelemetryTracingEnabled is true if the environment variable
// openTelemetryTracingEnabledMu guards access to openTelemetryTracingEnabled field
openTelemetryTracingEnabledMu = sync.RWMutex{}
// openTelemetryTracingEnabled is true if the environment variable
// GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING is set to the
// case-insensitive value "opentelemetry".
//
// Do not access directly. Use instead IsOpenTelemetryTracingEnabled or
// IsOpenCensusTracingEnabled. Intended for use only in unit tests. Restore
// original value after each test.
OpenTelemetryTracingEnabled bool = strings.EqualFold(strings.TrimSpace(
openTelemetryTracingEnabled bool = strings.EqualFold(strings.TrimSpace(
os.Getenv(TelemetryPlatformTracingVar)), TelemetryPlatformTracingOpenTelemetry)
)

// SetOpenTelemetryTracingEnabledField programmatically sets the value provided by GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING for the purpose of unit testing.
// Do not invoke it directly. Intended for use only in unit tests. Restore original value after each test.
func SetOpenTelemetryTracingEnabledField(enabled bool) {
openTelemetryTracingEnabledMu.Lock()
defer openTelemetryTracingEnabledMu.Unlock()
openTelemetryTracingEnabled = enabled
}

// IsOpenCensusTracingEnabled returns true if the environment variable
// GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING is NOT set to the
// case-insensitive value "opentelemetry".
Expand All @@ -72,7 +79,9 @@ func IsOpenCensusTracingEnabled() bool {
// GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING is set to the
// case-insensitive value "opentelemetry".
func IsOpenTelemetryTracingEnabled() bool {
return OpenTelemetryTracingEnabled
openTelemetryTracingEnabledMu.RLock()
defer openTelemetryTracingEnabledMu.RUnlock()
return openTelemetryTracingEnabled
}

// StartSpan adds a span to the trace with the given name. If IsOpenCensusTracingEnabled
Expand Down
12 changes: 6 additions & 6 deletions internal/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ var (
)

func TestStartSpan_OpenCensus(t *testing.T) {
old := OpenTelemetryTracingEnabled
OpenTelemetryTracingEnabled = false
old := IsOpenTelemetryTracingEnabled()
SetOpenTelemetryTracingEnabledField(false)
te := testutil.NewTestExporter()
t.Cleanup(func() {
OpenTelemetryTracingEnabled = old
SetOpenTelemetryTracingEnabledField(old)
te.Unregister()
})

Expand Down Expand Up @@ -95,12 +95,12 @@ func TestStartSpan_OpenCensus(t *testing.T) {
}

func TestStartSpan_OpenTelemetry(t *testing.T) {
old := OpenTelemetryTracingEnabled
OpenTelemetryTracingEnabled = true
old := IsOpenTelemetryTracingEnabled()
SetOpenTelemetryTracingEnabledField(true)
ctx := context.Background()
te := testutil.NewOpenTelemetryTestExporter()
t.Cleanup(func() {
OpenTelemetryTracingEnabled = old
SetOpenTelemetryTracingEnabledField(old)
te.Unregister(ctx)
})

Expand Down
6 changes: 3 additions & 3 deletions spanner/test/opentelemetry/test/ot_traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ import (
func TestSpannerTracesWithOpenTelemetry(t *testing.T) {
ctx := context.Background()
te := newOpenTelemetryTestExporter(false, true)
old := trace.OpenTelemetryTracingEnabled
trace.OpenTelemetryTracingEnabled = true
old := trace.IsOpenTelemetryTracingEnabled()
trace.SetOpenTelemetryTracingEnabledField(true)

t.Cleanup(func() {
trace.OpenTelemetryTracingEnabled = old
trace.SetOpenTelemetryTracingEnabledField(old)
te.Unregister(ctx)
})

Expand Down
Loading