Skip to content

Commit

Permalink
chore: Allow setting gosnowflake logging level from environment varia…
Browse files Browse the repository at this point in the history
…ble (#2285)

* Allow setting gosnowflake logging level from environment variable

* Fix test for GH actions
  • Loading branch information
sfc-gh-asawicki authored Dec 20, 2023
1 parent 4e2613c commit 843e8fc
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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) .
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
10 changes: 9 additions & 1 deletion pkg/sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
28 changes: 27 additions & 1 deletion pkg/sdk/client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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())
})
}
15 changes: 15 additions & 0 deletions pkg/sdk/internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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())
})
}

0 comments on commit 843e8fc

Please sign in to comment.