Skip to content

Commit bd966bc

Browse files
authored
fix(bigtable): Correct the Bigtable monitoring client options (#12410)
1 parent f8ed7ed commit bd966bc

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

bigtable/metrics.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323
"os"
24+
"reflect"
2425
"time"
2526

2627
"cloud.google.com/go/bigtable/internal"
@@ -123,11 +124,20 @@ var (
123124
return "go-" + uuid.NewString() + "@" + hostname, nil
124125
}
125126

127+
endpointOptionType = reflect.TypeOf(option.WithEndpoint(""))
128+
126129
// GCM exporter should use the same options as Bigtable client
127-
// createExporterOptions takes Bigtable client options and returns exporter options
130+
// createExporterOptions takes Bigtable client options and returns exporter options,
131+
// filtering out any WithEndpoint option to ensure the metrics exporter uses its default endpoint.
128132
// Overwritten in tests
129133
createExporterOptions = func(btOpts ...option.ClientOption) []option.ClientOption {
130-
return btOpts
134+
filteredOptions := []option.ClientOption{}
135+
for _, opt := range btOpts {
136+
if reflect.TypeOf(opt) != endpointOptionType {
137+
filteredOptions = append(filteredOptions, opt)
138+
}
139+
}
140+
return filteredOptions
131141
}
132142
)
133143

bigtable/metrics_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/base64"
2323
"fmt"
2424
"io"
25+
"reflect"
2526
"slices"
2627
"sort"
2728
"strings"
@@ -476,3 +477,49 @@ func TestToOtelMetricAttrs(t *testing.T) {
476477
})
477478
}
478479
}
480+
481+
func TestCreateExporterOptionsFiltering(t *testing.T) {
482+
endpointOpt := option.WithEndpoint("test.endpoint")
483+
apiKeyOpt := option.WithAPIKey("test.apikey")
484+
audiencesOpt := option.WithAudiences("test.audience")
485+
486+
inputOpts := []option.ClientOption{
487+
endpointOpt,
488+
apiKeyOpt,
489+
audiencesOpt,
490+
}
491+
492+
filteredOpts := createExporterOptions(inputOpts...)
493+
494+
foundEndpointOpt := false
495+
foundAPIKeyOpt := false
496+
foundAudiencesOpt := false
497+
498+
for _, opt := range filteredOpts {
499+
if reflect.TypeOf(opt) == reflect.TypeOf(endpointOpt) {
500+
foundEndpointOpt = true
501+
}
502+
if reflect.TypeOf(opt) == reflect.TypeOf(apiKeyOpt) {
503+
foundAPIKeyOpt = true
504+
}
505+
if reflect.TypeOf(opt) == reflect.TypeOf(audiencesOpt) {
506+
foundAudiencesOpt = true
507+
}
508+
}
509+
510+
if foundEndpointOpt {
511+
t.Errorf("option.WithEndpoint was found in filtered options, but it should have been filtered out")
512+
}
513+
514+
if !foundAPIKeyOpt {
515+
t.Errorf("option.WithAPIKey was not found in filtered options, but it should have been preserved")
516+
}
517+
518+
if !foundAudiencesOpt {
519+
t.Errorf("option.WithAudiences was not found in filtered options, but it should have been preserved")
520+
}
521+
522+
if len(filteredOpts) != 2 {
523+
t.Errorf("Expected 2 options to be returned, got %d", len(filteredOpts))
524+
}
525+
}

0 commit comments

Comments
 (0)