Skip to content

Commit 941c16f

Browse files
authored
feat(internal/trace): export internal/trace package constants and vars (#9242)
* Export constants and vars for use by handwritten clients * Update docs for transition date to OpenTelemetry tracing default.
1 parent e8dc540 commit 941c16f

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

internal/trace/trace.go

+29-12
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,33 @@ import (
3232
)
3333

3434
const (
35-
telemetryPlatformTracingOpenCensus = "opencensus"
36-
telemetryPlatformTracingOpenTelemetry = "opentelemetry"
37-
telemetryPlatformTracingVar = "GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING"
35+
// TelemetryPlatformTracingOpenCensus is the value to which the environment
36+
// variable GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING should be
37+
// set to enable OpenCensus tracing.
38+
TelemetryPlatformTracingOpenCensus = "opencensus"
39+
// TelemetryPlatformTracingOpenCensus is the value to which the environment
40+
// variable GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING should be
41+
// set to enable OpenTelemetry tracing.
42+
TelemetryPlatformTracingOpenTelemetry = "opentelemetry"
43+
// TelemetryPlatformTracingOpenCensus is the name of the environment
44+
// variable that can be set to change the default tracing from OpenCensus
45+
// to OpenTelemetry.
46+
TelemetryPlatformTracingVar = "GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING"
47+
// OpenTelemetryTracerName is the name given to the OpenTelemetry Tracer
48+
// when it is obtained from the OpenTelemetry TracerProvider.
49+
OpenTelemetryTracerName = "cloud.google.com/go"
3850
)
3951

4052
var (
41-
// TODO(chrisdsmith): Should the name of the OpenTelemetry tracer be public and mutable?
42-
openTelemetryTracerName string = "cloud.google.com/go"
43-
openTelemetryTracingEnabled bool = strings.EqualFold(strings.TrimSpace(
44-
os.Getenv(telemetryPlatformTracingVar)), telemetryPlatformTracingOpenTelemetry)
53+
// OpenTelemetryTracingEnabled is true if the environment variable
54+
// GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING is set to the
55+
// case-insensitive value "opentelemetry".
56+
//
57+
// Do not access directly. Use instead IsOpenTelemetryTracingEnabled or
58+
// IsOpenCensusTracingEnabled. Intended for use only in unit tests. Restore
59+
// original value after each test.
60+
OpenTelemetryTracingEnabled bool = strings.EqualFold(strings.TrimSpace(
61+
os.Getenv(TelemetryPlatformTracingVar)), TelemetryPlatformTracingOpenTelemetry)
4562
)
4663

4764
// IsOpenCensusTracingEnabled returns true if the environment variable
@@ -55,20 +72,20 @@ func IsOpenCensusTracingEnabled() bool {
5572
// GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING is set to the
5673
// case-insensitive value "opentelemetry".
5774
func IsOpenTelemetryTracingEnabled() bool {
58-
return openTelemetryTracingEnabled
75+
return OpenTelemetryTracingEnabled
5976
}
6077

6178
// StartSpan adds a span to the trace with the given name. If IsOpenCensusTracingEnabled
6279
// returns true, the span will be an OpenCensus span. If IsOpenTelemetryTracingEnabled
6380
// returns true, the span will be an OpenTelemetry span. Set the environment variable
6481
// GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING to the case-insensitive
6582
// value "opentelemetry" before loading the package to use OpenTelemetry tracing.
66-
// The default will remain OpenCensus until [TBD], at which time the default will
83+
// The default will remain OpenCensus until May 29, 2024, at which time the default will
6784
// switch to "opentelemetry" and explicitly setting the environment variable to
6885
// "opencensus" will be required to continue using OpenCensus tracing.
6986
func StartSpan(ctx context.Context, name string) context.Context {
7087
if IsOpenTelemetryTracingEnabled() {
71-
ctx, _ = otel.GetTracerProvider().Tracer(openTelemetryTracerName).Start(ctx, name)
88+
ctx, _ = otel.GetTracerProvider().Tracer(OpenTelemetryTracerName).Start(ctx, name)
7289
} else {
7390
ctx, _ = trace.StartSpan(ctx, name)
7491
}
@@ -80,7 +97,7 @@ func StartSpan(ctx context.Context, name string) context.Context {
8097
// returns true, the span will be an OpenTelemetry span. Set the environment variable
8198
// GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING to the case-insensitive
8299
// value "opentelemetry" before loading the package to use OpenTelemetry tracing.
83-
// The default will remain OpenCensus until [TBD], at which time the default will
100+
// The default will remain OpenCensus until May 29, 2024, at which time the default will
84101
// switch to "opentelemetry" and explicitly setting the environment variable to
85102
// "opencensus" will be required to continue using OpenCensus tracing.
86103
func EndSpan(ctx context.Context, err error) {
@@ -166,7 +183,7 @@ func httpStatusCodeToOCCode(httpStatusCode int) int32 {
166183
// span must be an OpenTelemetry span. Set the environment variable
167184
// GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING to the case-insensitive
168185
// value "opentelemetry" before loading the package to use OpenTelemetry tracing.
169-
// The default will remain OpenCensus until [TBD], at which time the default will
186+
// The default will remain OpenCensus until May 29, 2024, at which time the default will
170187
// switch to "opentelemetry" and explicitly setting the environment variable to
171188
// "opencensus" will be required to continue using OpenCensus tracing.
172189
func TracePrintf(ctx context.Context, attrMap map[string]interface{}, format string, args ...interface{}) {

internal/trace/trace_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ var (
4141
)
4242

4343
func TestStartSpan_OpenCensus(t *testing.T) {
44-
old := openTelemetryTracingEnabled
45-
openTelemetryTracingEnabled = false
44+
old := OpenTelemetryTracingEnabled
45+
OpenTelemetryTracingEnabled = false
4646
te := testutil.NewTestExporter()
4747
t.Cleanup(func() {
48-
openTelemetryTracingEnabled = old
48+
OpenTelemetryTracingEnabled = old
4949
te.Unregister()
5050
})
5151

@@ -95,12 +95,12 @@ func TestStartSpan_OpenCensus(t *testing.T) {
9595
}
9696

9797
func TestStartSpan_OpenTelemetry(t *testing.T) {
98-
old := openTelemetryTracingEnabled
99-
openTelemetryTracingEnabled = true
98+
old := OpenTelemetryTracingEnabled
99+
OpenTelemetryTracingEnabled = true
100100
ctx := context.Background()
101101
te := testutil.NewOpenTelemetryTestExporter()
102102
t.Cleanup(func() {
103-
openTelemetryTracingEnabled = old
103+
OpenTelemetryTracingEnabled = old
104104
te.Unregister(ctx)
105105
})
106106

0 commit comments

Comments
 (0)