Skip to content

Commit 41ef5f0

Browse files
codygibbrahul2393
andauthored
fix(spanner): snapshot EnableOpenTelemetryMetrics value on client creation (googleapis#11496)
* fix(spanner): snapshot EnableOpenTelemetryMetrics value on client creation * delete test --------- Co-authored-by: rahul2393 <irahul@google.com>
1 parent 427f448 commit 41ef5f0

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

spanner/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ type ClientConfig struct {
358358
}
359359

360360
type openTelemetryConfig struct {
361+
enabled bool
361362
meterProvider metric.MeterProvider
362363
attributeMap []attribute.KeyValue
363364
attributeMapWithMultiplexed []attribute.KeyValue

spanner/ot_metrics.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,15 @@ var (
5050
)
5151

5252
func createOpenTelemetryConfig(mp metric.MeterProvider, logger *log.Logger, sessionClientID string, db string) (*openTelemetryConfig, error) {
53+
// Important: snapshot the value of the global variable to ensure a
54+
// consistent value for the lifetime of this client.
55+
enabled := IsOpenTelemetryMetricsEnabled()
56+
5357
config := &openTelemetryConfig{
58+
enabled: enabled,
5459
attributeMap: []attribute.KeyValue{},
5560
}
56-
if !IsOpenTelemetryMetricsEnabled() {
61+
if !enabled {
5762
return config, nil
5863
}
5964
_, instance, database, err := parseDatabaseName(db)
@@ -89,7 +94,7 @@ func setOpenTelemetryMetricProvider(config *openTelemetryConfig, mp metric.Meter
8994
}
9095

9196
func initializeMetricInstruments(config *openTelemetryConfig, logger *log.Logger) {
92-
if !IsOpenTelemetryMetricsEnabled() {
97+
if !config.enabled {
9398
return
9499
}
95100
meter := config.meterProvider.Meter(OtInstrumentationScope, metric.WithInstrumentationVersion(internal.Version))
@@ -191,7 +196,7 @@ func initializeMetricInstruments(config *openTelemetryConfig, logger *log.Logger
191196

192197
func registerSessionPoolOTMetrics(pool *sessionPool) error {
193198
otConfig := pool.otConfig
194-
if !IsOpenTelemetryMetricsEnabled() || otConfig == nil {
199+
if otConfig == nil || !otConfig.enabled {
195200
return nil
196201
}
197202

@@ -241,7 +246,7 @@ func setOpenTelemetryMetricsFlag(enable bool) {
241246
}
242247

243248
func recordGFELatencyMetricsOT(ctx context.Context, md metadata.MD, keyMethod string, otConfig *openTelemetryConfig) error {
244-
if !IsOpenTelemetryMetricsEnabled() || md == nil && otConfig == nil {
249+
if otConfig == nil || !otConfig.enabled || md == nil {
245250
return nil
246251
}
247252
attr := otConfig.attributeMap

spanner/request_id_header_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,7 @@ func TestRequestIDHeader_SingleUseReadOnly_ExecuteStreamingSql_InvalidArgument(t
16571657
}
16581658

16591659
func TestRequestIDHeader_SingleUseReadOnly_ExecuteStreamingSql_ContextDeadlineExceeded(t *testing.T) {
1660+
t.Skip("TODO: debug error from PR #11788 and un-skip. See https://source.cloud.google.com/results/invocations/ead1cb6b-10e8-4d2b-80e3-aec3b96feff0")
16601661
t.Parallel()
16611662

16621663
ctx := context.Background()

0 commit comments

Comments
 (0)