Skip to content

Commit

Permalink
fix: add mutex to internal trace otel test variable (#9390)
Browse files Browse the repository at this point in the history
* fix: add mutex to otel test variable

* feat: code refactoring

* feat: comments refactoring
  • Loading branch information
harshachinta authored Feb 8, 2024
1 parent fc51cc2 commit 909fbdf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
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

0 comments on commit 909fbdf

Please sign in to comment.