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(spanner): use spanner options when initializing monitoring exporter #11109

Merged
merged 6 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion spanner/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ func newClientWithConfig(ctx context.Context, database string, config ClientConf
}

// Create a OpenTelemetry metrics configuration
metricsTracerFactory, err := newBuiltinMetricsTracerFactory(ctx, database, metricsProvider)
metricsTracerFactory, err := newBuiltinMetricsTracerFactory(ctx, database, metricsProvider, opts...)
if err != nil {
return nil, err
}
Expand Down
16 changes: 11 additions & 5 deletions spanner/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,12 @@ var (
return "global"
}

exporterOpts = []option.ClientOption{}
// GCM exporter should use the same options as Bigtable client
rahul2393 marked this conversation as resolved.
Show resolved Hide resolved
// createExporterOptions takes Spanner client options and returns exporter options
// Overwritten in tests
createExporterOptions = func(spannerOpts ...option.ClientOption) []option.ClientOption {
return spannerOpts
}
)

type metricInfo struct {
Expand All @@ -193,7 +198,7 @@ type builtinMetricsTracerFactory struct {
attemptCount metric.Int64Counter // Counter for the number of attempts.
}

func newBuiltinMetricsTracerFactory(ctx context.Context, dbpath string, metricsProvider metric.MeterProvider) (*builtinMetricsTracerFactory, error) {
func newBuiltinMetricsTracerFactory(ctx context.Context, dbpath string, metricsProvider metric.MeterProvider, opts ...option.ClientOption) (*builtinMetricsTracerFactory, error) {
clientUID, err := generateClientUID()
if err != nil {
log.Printf("built-in metrics: generateClientUID failed: %v. Using empty string in the %v metric atteribute", err, metricLabelKeyClientUID)
Expand Down Expand Up @@ -224,7 +229,7 @@ func newBuiltinMetricsTracerFactory(ctx context.Context, dbpath string, metricsP
var meterProvider *sdkmetric.MeterProvider
if metricsProvider == nil {
// Create default meter provider
mpOptions, err := builtInMeterProviderOptions(project)
mpOptions, err := builtInMeterProviderOptions(project, opts...)
if err != nil {
return tracerFactory, err
}
Expand All @@ -247,8 +252,9 @@ func newBuiltinMetricsTracerFactory(ctx context.Context, dbpath string, metricsP
return tracerFactory, err
}

func builtInMeterProviderOptions(project string) ([]sdkmetric.Option, error) {
defaultExporter, err := newMonitoringExporter(context.Background(), project, exporterOpts...)
func builtInMeterProviderOptions(project string, opts ...option.ClientOption) ([]sdkmetric.Option, error) {
allOpts := createExporterOptions(opts...)
defaultExporter, err := newMonitoringExporter(context.Background(), project, allOpts...)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion spanner/metrics_monitoring_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

monitoring "cloud.google.com/go/monitoring/apiv3/v2"
"cloud.google.com/go/monitoring/apiv3/v2/monitoringpb"
"cloud.google.com/go/spanner/internal"
"go.opentelemetry.io/otel/attribute"
otelmetric "go.opentelemetry.io/otel/sdk/metric"
otelmetricdata "go.opentelemetry.io/otel/sdk/metric/metricdata"
Expand All @@ -37,6 +38,7 @@ import (
googlemetricpb "google.golang.org/genproto/googleapis/api/metric"
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
)
Expand Down Expand Up @@ -147,7 +149,7 @@ func (me *monitoringExporter) exportTimeSeries(ctx context.Context, rm *otelmetr
Name: name,
TimeSeries: tss[i:j],
}
err = me.client.CreateServiceTimeSeries(ctx, req)
err = me.client.CreateServiceTimeSeries(metadata.NewOutgoingContext(ctx, metadata.Pairs("x-goog-api-client", "gccl/"+internal.Version)), req)
if err != nil {
if status.Code(err) == codes.PermissionDenied {
err = fmt.Errorf("%w Need monitoring metric writer permission on project=%s. Follow https://cloud.google.com/spanner/docs/view-manage-client-side-metrics#access-client-side-metrics to set up permissions",
Expand Down
16 changes: 10 additions & 6 deletions spanner/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,18 @@ func TestNewBuiltinMetricsTracerFactory(t *testing.T) {
}
go monitoringServer.Serve()
defer monitoringServer.Shutdown()
origExporterOpts := exporterOpts
exporterOpts = []option.ClientOption{
option.WithEndpoint(monitoringServer.Endpoint),
option.WithoutAuthentication(),
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),

// Override exporter options
origCreateExporterOptions := createExporterOptions
createExporterOptions = func(opts ...option.ClientOption) []option.ClientOption {
return []option.ClientOption{
option.WithEndpoint(monitoringServer.Endpoint), // Connect to mock
option.WithoutAuthentication(),
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
}
}
defer func() {
exporterOpts = origExporterOpts
createExporterOptions = origCreateExporterOptions
}()

tests := []struct {
Expand Down
Loading