diff --git a/Makefile b/Makefile index 7597519a45..44f3ec6b58 100644 --- a/Makefile +++ b/Makefile @@ -77,7 +77,7 @@ test-architecture: ## check architecture constraints between packages go test ./pkg/architests/... -v test-client: ## runs test that checks sdk.Client without instrumentedsql - SF_TF_NO_INSTRUMENTED_SQL=1 go test ./pkg/sdk/internal/client/... -v + SF_TF_NO_INSTRUMENTED_SQL=1 SF_TF_GOSNOWFLAKE_LOG_LEVEL=debug go test ./pkg/sdk/internal/client/... -v build-local: ## build the binary locally go build -o $(BASE_BINARY_NAME) . diff --git a/README.md b/README.md index 92ef72c870..5b4e49a116 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,18 @@ Set environment variable `SF_TF_ADDITIONAL_DEBUG_LOGGING` to a non-empty value. ## Additional SQL Client configuration Currently underlying sql [gosnowflake](https://github.com/snowflakedb/gosnowflake) driver is wrapped with [instrumentedsql](https://github.com/luna-duclos/instrumentedsql). In order to use raw [gosnowflake](https://github.com/snowflakedb/gosnowflake) driver, set environment variable `SF_TF_NO_INSTRUMENTED_SQL` to a non-empty value. +By default, the underlying driver is set to error level logging. It can be changed by setting `SF_TF_GOSNOWFLAKE_LOG_LEVEL` to one of: +- `panic` +- `fatal` +- `error` +- `warn` +- `warning` +- `info` +- `debug` +- `trace` + +*note*: It's possible it will be one of the provider config parameters in the future provider versions. + ## Contributing Cf. [Contributing](./CONTRIBUTING.md). diff --git a/pkg/sdk/client.go b/pkg/sdk/client.go index 68aa7ad232..6f790f84d3 100644 --- a/pkg/sdk/client.go +++ b/pkg/sdk/client.go @@ -13,10 +13,14 @@ import ( "github.com/snowflakedb/gosnowflake" ) -var instrumentedSQL bool +var ( + instrumentedSQL bool + gosnowflakeLoggingLevel string +) func init() { instrumentedSQL = os.Getenv("SF_TF_NO_INSTRUMENTED_SQL") == "" + gosnowflakeLoggingLevel = os.Getenv("SF_TF_GOSNOWFLAKE_LOG_LEVEL") } type Client struct { @@ -120,6 +124,10 @@ func NewClient(cfg *gosnowflake.Config) (*Client, error) { driverName = "snowflake-instrumented" } + if gosnowflakeLoggingLevel != "" { + cfg.Tracing = gosnowflakeLoggingLevel + } + dsn, err := gosnowflake.DSN(cfg) if err != nil { return nil, err diff --git a/pkg/sdk/client_integration_test.go b/pkg/sdk/client_integration_test.go index 43f9de4608..fdc1ce666d 100644 --- a/pkg/sdk/client_integration_test.go +++ b/pkg/sdk/client_integration_test.go @@ -3,10 +3,11 @@ package sdk import ( "context" "database/sql" + "os" "testing" + "github.com/snowflakedb/gosnowflake" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) @@ -76,3 +77,28 @@ func TestClient_queryOne(t *testing.T) { require.NoError(t, err) require.Equal(t, 1, row.One) } + +func TestClient_NewClientDriverLoggingLevel(t *testing.T) { + t.Run("get default gosnowflake driver logging level", func(t *testing.T) { + config := DefaultConfig() + _, err := NewClient(config) + require.NoError(t, err) + + var expected string + if os.Getenv("GITHUB_ACTIONS") != "" { + expected = "fatal" + } else { + expected = "error" + } + assert.Equal(t, expected, gosnowflake.GetLogger().GetLogLevel()) + }) + + t.Run("set gosnowflake driver logging level with config", func(t *testing.T) { + config := DefaultConfig() + config.Tracing = "trace" + _, err := NewClient(config) + require.NoError(t, err) + + assert.Equal(t, "trace", gosnowflake.GetLogger().GetLogLevel()) + }) +} diff --git a/pkg/sdk/internal/client/client_test.go b/pkg/sdk/internal/client/client_test.go index 9c2b87db52..6bcf5cbc50 100644 --- a/pkg/sdk/internal/client/client_test.go +++ b/pkg/sdk/internal/client/client_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/snowflakedb/gosnowflake" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -27,3 +28,17 @@ func TestNewClientWithoutInstrumentedSQL(t *testing.T) { assert.Contains(t, sql.Drivers(), "snowflake") }) } + +func TestNewClientWithDebugLoggingSetFromEnv(t *testing.T) { + t.Run("set gosnowflake driver logging to debug", func(t *testing.T) { + if os.Getenv("SF_TF_GOSNOWFLAKE_LOG_LEVEL") == "" { + t.Skip("Skipping TestNewClientWithDebugLoggingSet, because SF_TF_GOSNOWFLAKE_LOG_LEVEL is not set") + } + + config := sdk.DefaultConfig() + _, err := sdk.NewClient(config) + require.NoError(t, err) + + assert.Equal(t, "debug", gosnowflake.GetLogger().GetLogLevel()) + }) +}