Skip to content

Commit

Permalink
Add spanner emulator env var detection
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim committed May 3, 2022
1 parent 53f8741 commit 5297edd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ require (
github.com/shopspring/decimal v1.3.1
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.11.0
github.com/stretchr/testify v1.7.1
go.buf.build/protocolbuffers/go/prometheus/prometheus v1.2.2
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.31.0
Expand Down Expand Up @@ -139,7 +140,6 @@ require (
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.11.0 // indirect
github.com/stretchr/objx v0.3.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
Expand Down
18 changes: 16 additions & 2 deletions internal/datastore/spanner/migrations/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"cloud.google.com/go/spanner"
admin "cloud.google.com/go/spanner/admin/database/apiv1"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"google.golang.org/api/option"
"google.golang.org/grpc/codes"

Expand All @@ -16,6 +17,7 @@ import (
const (
tableSchemaVersion = "schema_version"
colVersionNum = "version_num"
emulatorSettingKey = "SPANNER_EMULATOR_HOST"
)

// SpannerMigrationDriver can migrate a Cloud Spanner instance
Expand All @@ -28,8 +30,11 @@ type SpannerMigrationDriver struct {
// NewSpannerDriver returns a migration driver for the given Cloud Spanner instance
func NewSpannerDriver(database, credentialsFilePath string) (SpannerMigrationDriver, error) {
ctx := context.Background()

log.Info().Str("spanner-emulator-host", os.Getenv("SPANNER_EMULATOR_HOST")).Msg("spanner emulator")
setting, found := detectEmulatorSetting()
if found {
os.Setenv(emulatorSettingKey, setting)
}
log.Info().Str("spanner-emulator-host", os.Getenv(emulatorSettingKey)).Msg("spanner emulator")
log.Info().Str("credentials", credentialsFilePath).Str("db", database).Msg("connecting")
client, err := spanner.NewClient(ctx, database, option.WithCredentialsFile(credentialsFilePath))
if err != nil {
Expand Down Expand Up @@ -84,3 +89,12 @@ func (smd SpannerMigrationDriver) Close() error {
}

var _ migrate.Driver = SpannerMigrationDriver{}

func detectEmulatorSetting() (string, bool) {
viper.BindEnv(emulatorSettingKey)
val := viper.GetString(emulatorSettingKey)
if len(val) > 0 {
return val, true
}
return "", false
}
18 changes: 18 additions & 0 deletions internal/datastore/spanner/spanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"strings"
"time"

"cloud.google.com/go/spanner"
Expand Down Expand Up @@ -58,6 +59,11 @@ func NewSpannerDatastore(database string, opts ...Option) (datastore.Datastore,
return nil, fmt.Errorf(errUnableToInstantiate, err)
}

setting, found := detectEmulatorSetting()
if found {
os.Setenv("SPANNER_EMULATOR_HOST", setting)
}

config.gcInterval = common.WithJitter(0.2, config.gcInterval)
log.Info().Float64("factor", 0.2).Msg("gc configured with jitter")
log.Info().Str("spanner-emulator-host", os.Getenv("SPANNER_EMULATOR_HOST")).Msg("spanner emulator")
Expand Down Expand Up @@ -136,3 +142,15 @@ func statementFromSQL(sql string, args []interface{}) spanner.Statement {
Params: params,
}
}

func detectEmulatorSetting() (string, bool) {
for _, env := range os.Environ() {
varPair := strings.SplitN(env, "=", 2)
key := varPair[0]
value := varPair[1]
if strings.Contains(key, "SPANNER_EMULATOR_HOST") && len(value) > 0 {
return value, true
}
}
return "", false
}

0 comments on commit 5297edd

Please sign in to comment.