Skip to content

Commit

Permalink
[Fix] Fixed logging for underlying Go SDK (#3917)
Browse files Browse the repository at this point in the history
## Changes
<!-- Summary of your changes that are easy to understand -->
Go SDK doesn't contain valid context for terraform logger, due to this
we get a nil logger from context:
https://github.com/hashicorp/terraform-plugin-log/blob/main/tflog/provider.go#L40.

We always use the context from terraform and ignore the one from Go SDK.

## Tests
<!-- 
How is this tested? Please see the checklist below and also describe any
other relevant tests
-->
`TF_LOG=TRACE terraform apply` won't print:
https://github.com/databricks/databricks-sdk-go/blob/main/config/auth_default.go#L54
on main, prints it after change. Also tested manually for some debug
statements.

Ran integration tests and see debug logs.
- [ ] `make test` run locally
- [ ] relevant change in `docs/` folder
- [ ] covered with integration tests in `internal/acceptance`
- [ ] relevant acceptance tests are passing
- [ ] using Go SDK
  • Loading branch information
tanmay-db authored Aug 20, 2024
1 parent 53780f6 commit 2d2b4d1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 40 deletions.
2 changes: 1 addition & 1 deletion internal/acceptance/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func init() {
rand.Seed(time.Now().UnixMicro())
databricks.WithProduct("tf-integration-tests", common.Version())
os.Setenv("TF_LOG", "DEBUG")
dbproviderlogger.SetLogger()
dbproviderlogger.SetTfLogger(dbproviderlogger.NewTfLogger(context.Background()))
}

func workspaceLevel(t *testing.T, steps ...step) {
Expand Down
57 changes: 21 additions & 36 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ import (
)

type TfLogger struct {
Name string
ctx context.Context
}

// This function expects the context to have the logger key configured to hold the logger.
// Please see: GetProviderRootLogger defined in terraform-plugin-log here: https://github.com/hashicorp/terraform-plugin-log/blob/main/internal/logging/provider.go#L14 for reference.
func NewTfLogger(ctx context.Context) *TfLogger {
return &TfLogger{ctx: ctx}
}

func SetTfLogger(tfLogger *TfLogger) {
logger.DefaultLogger = tfLogger
}

// This function is always enabled because TfLogger implements the Logger interface from Go SDK and there we check
Expand All @@ -20,47 +30,22 @@ func (tfLogger *TfLogger) Enabled(_ context.Context, _ logger.Level) bool {
return true
}

func (tfLogger *TfLogger) Tracef(ctx context.Context, format string, v ...any) {
if tfLogger == nil {
tflog.Trace(ctx, fmt.Sprintf(format, v...), nil)
} else {
tflog.SubsystemTrace(ctx, tfLogger.Name, fmt.Sprintf(format, v...), nil)
}
}

func (tfLogger *TfLogger) Debugf(ctx context.Context, format string, v ...any) {
if tfLogger == nil {
tflog.Debug(ctx, fmt.Sprintf(format, v...), nil)
} else {
tflog.SubsystemDebug(ctx, tfLogger.Name, fmt.Sprintf(format, v...), nil)
}
func (tfLogger *TfLogger) Tracef(_ context.Context, format string, v ...any) {
tflog.Trace(tfLogger.ctx, fmt.Sprintf(format, v...), nil)
}

func (tfLogger *TfLogger) Infof(ctx context.Context, format string, v ...any) {
if tfLogger == nil {
tflog.Info(ctx, fmt.Sprintf(format, v...), nil)
} else {
tflog.SubsystemInfo(ctx, tfLogger.Name, fmt.Sprintf(format, v...), nil)
}
func (tfLogger *TfLogger) Debugf(_ context.Context, format string, v ...any) {
tflog.Debug(tfLogger.ctx, fmt.Sprintf(format, v...), nil)
}

func (tfLogger *TfLogger) Warnf(ctx context.Context, format string, v ...any) {
if tfLogger == nil {
tflog.Warn(ctx, fmt.Sprintf(format, v...), nil)
} else {
tflog.SubsystemWarn(ctx, tfLogger.Name, fmt.Sprintf(format, v...), nil)
}
func (tfLogger *TfLogger) Infof(_ context.Context, format string, v ...any) {
tflog.Info(tfLogger.ctx, fmt.Sprintf(format, v...), nil)
}

func (tfLogger *TfLogger) Errorf(ctx context.Context, format string, v ...any) {
if tfLogger == nil {
tflog.Error(ctx, fmt.Sprintf(format, v...), nil)
} else {
tflog.SubsystemError(ctx, tfLogger.Name, fmt.Sprintf(format, v...), nil)
}
func (tfLogger *TfLogger) Warnf(_ context.Context, format string, v ...any) {
tflog.Warn(tfLogger.ctx, fmt.Sprintf(format, v...), nil)
}

func SetLogger() {
var tfLogger *TfLogger
logger.DefaultLogger = tfLogger
func (tfLogger *TfLogger) Errorf(_ context.Context, format string, v ...any) {
tflog.Error(tfLogger.ctx, fmt.Sprintf(format, v...), nil)
}
2 changes: 1 addition & 1 deletion logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ func TestTfLogger_Enabled(t *testing.T) {
}

func TestSetLogger(t *testing.T) {
SetLogger()
SetTfLogger(NewTfLogger(context.Background()))
assert.IsType(t, &TfLogger{}, goLogger.DefaultLogger)
}
4 changes: 2 additions & 2 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/databricks/terraform-provider-databricks/common"
"github.com/databricks/terraform-provider-databricks/dashboards"
"github.com/databricks/terraform-provider-databricks/jobs"
tflogger "github.com/databricks/terraform-provider-databricks/logger"
"github.com/databricks/terraform-provider-databricks/logger"
"github.com/databricks/terraform-provider-databricks/mlflow"
"github.com/databricks/terraform-provider-databricks/mws"
"github.com/databricks/terraform-provider-databricks/permissions"
Expand Down Expand Up @@ -224,7 +224,7 @@ func DatabricksProvider() *schema.Provider {
if p.TerraformVersion != "" {
useragent.WithUserAgentExtra("terraform", p.TerraformVersion)
}
tflogger.SetLogger()
logger.SetTfLogger(logger.NewTfLogger(ctx))
return ConfigureDatabricksClient(ctx, d)
}
common.AddContextToAllResources(p, "databricks")
Expand Down

0 comments on commit 2d2b4d1

Please sign in to comment.