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

[Internal] Replace the logger package with a more flexible log package. #1119

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ By default, Databricks SDK for Go uses [logger.SimpleLogger](https://pkg.go.dev/
Since v0.10.0, default logger prints only `INFO` level messages. To replicate more verbose behavior from the previous versions, set the `DEBUG` level in `SimpleLogger`:

```go
import "github.com/databricks/databricks-sdk-go/logger"
import "github.com/databricks/databricks-sdk-go/databricks/log"

func init() {
logger.DefaultLogger = &logger.SimpleLogger{
Expand Down
20 changes: 12 additions & 8 deletions config/auth_azure_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"golang.org/x/oauth2"

"github.com/databricks/databricks-sdk-go/config/credentials"
"github.com/databricks/databricks-sdk-go/logger"
"github.com/databricks/databricks-sdk-go/databricks/log"
)

// The header used to pass the service management token to the Databricks backend.
Expand Down Expand Up @@ -47,7 +47,7 @@ func (c AzureCliCredentials) getVisitor(ctx context.Context, cfg *Config, inner
ts := &azureCliTokenSource{ctx, cfg.Environment().AzureServiceManagementEndpoint(), cfg.AzureResourceID, cfg.AzureTenantID}
t, err := ts.Token()
if err != nil {
logger.Debugf(ctx, "Not including service management token in headers: %v", err)
log.DebugContext(ctx, "Not including service management token in headers: %v", err)
return azureVisitor(cfg, refreshableVisitor(inner)), nil
}
management := azureReuseTokenSource(t, ts)
Expand All @@ -72,7 +72,7 @@ func (c AzureCliCredentials) Configure(ctx context.Context, cfg *Config) (creden
return nil, nil
}
if strings.Contains(err.Error(), "executable file not found") {
logger.Debugf(ctx, "Most likely Azure CLI is not installed. "+
log.DebugContext(ctx, "Most likely Azure CLI is not installed. "+
"See https://docs.microsoft.com/en-us/cli/azure/?view=azure-cli-latest for details")
return nil, nil
}
Expand All @@ -86,7 +86,7 @@ func (c AzureCliCredentials) Configure(ctx context.Context, cfg *Config) (creden
if err != nil {
return nil, err
}
logger.Infof(ctx, "Using Azure CLI authentication with AAD tokens")
log.InfoContext(ctx, "Using Azure CLI authentication with AAD tokens")
return credentials.NewOAuthCredentialsProvider(visitor, ts.Token), nil
}

Expand Down Expand Up @@ -119,7 +119,7 @@ func (ts *azureCliTokenSource) getSubscription() string {
}
components := strings.Split(ts.workspaceResourceId, "/")
if len(components) < 3 {
logger.Warnf(context.Background(), "Invalid azure workspace resource ID")
log.Warning("Invalid azure workspace resource ID")
return ""
}
return components[2]
Expand All @@ -143,8 +143,12 @@ func (ts *azureCliTokenSource) Token() (*oauth2.Token, error) {
if ts.azureTenantId != "" {
tenantIdDebug = fmt.Sprintf(" for tenant %s", ts.azureTenantId)
}
logger.Infof(context.Background(), "Refreshed OAuth token for %s%s from Azure CLI, which expires on %s",
ts.resource, tenantIdDebug, it.ExpiresOn)
log.Info(
"Refreshed OAuth token for %s%s from Azure CLI, which expires on %s",
ts.resource,
tenantIdDebug,
it.ExpiresOn,
)

var extra map[string]interface{}
err = json.Unmarshal(tokenBytes, &extra)
Expand Down Expand Up @@ -199,7 +203,7 @@ func (ts *azureCliTokenSource) getTokenBytesWithTenantId(tenantId string) ([]byt
if err == nil {
return result, nil
}
logger.Infof(ts.ctx, "Failed to get token for subscription. Using resource only token.")
log.InfoContext(ts.ctx, "Failed to get token for subscription. Using resource only token.")
}
result, err := runCommand(ts.ctx, "az", args)
if ee, ok := err.(*exec.ExitError); ok {
Expand Down
4 changes: 2 additions & 2 deletions config/auth_azure_client_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"golang.org/x/oauth2/clientcredentials"

"github.com/databricks/databricks-sdk-go/config/credentials"
"github.com/databricks/databricks-sdk-go/logger"
"github.com/databricks/databricks-sdk-go/databricks/log"
)

type AzureClientSecretCredentials struct {
Expand Down Expand Up @@ -50,7 +50,7 @@ func (c AzureClientSecretCredentials) Configure(ctx context.Context, cfg *Config
if err != nil {
return nil, fmt.Errorf("resolve host: %w", err)
}
logger.Infof(ctx, "Generating AAD token for Service Principal (%s)", cfg.AzureClientID)
log.InfoContext(ctx, "Generating AAD token for Service Principal (%s)", cfg.AzureClientID)
env := cfg.Environment()
aadEndpoint := env.AzureActiveDirectoryEndpoint()
managementEndpoint := env.AzureServiceManagementEndpoint()
Expand Down
6 changes: 3 additions & 3 deletions config/auth_azure_github_oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"time"

"github.com/databricks/databricks-sdk-go/config/credentials"
"github.com/databricks/databricks-sdk-go/databricks/log"
"github.com/databricks/databricks-sdk-go/httpclient"
"github.com/databricks/databricks-sdk-go/logger"
"golang.org/x/oauth2"
)

Expand Down Expand Up @@ -50,11 +50,11 @@ func (c AzureGithubOIDCCredentials) Configure(ctx context.Context, cfg *Config)
// requestIDToken requests an ID token from the Github Action.
func requestIDToken(ctx context.Context, cfg *Config) (string, error) {
if cfg.ActionsIDTokenRequestURL == "" {
logger.Debugf(ctx, "Missing cfg.ActionsIDTokenRequestURL, likely not calling from a Github action")
log.DebugContext(ctx, "Missing cfg.ActionsIDTokenRequestURL, likely not calling from a Github action")
return "", nil
}
if cfg.ActionsIDTokenRequestToken == "" {
logger.Debugf(ctx, "Missing cfg.ActionsIDTokenRequestToken, likely not calling from a Github action")
log.DebugContext(ctx, "Missing cfg.ActionsIDTokenRequestToken, likely not calling from a Github action")
return "", nil
}

Expand Down
4 changes: 2 additions & 2 deletions config/auth_azure_msi.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"time"

"github.com/databricks/databricks-sdk-go/config/credentials"
"github.com/databricks/databricks-sdk-go/databricks/log"
"github.com/databricks/databricks-sdk-go/httpclient"
"github.com/databricks/databricks-sdk-go/logger"
"golang.org/x/oauth2"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ func (c AzureMsiCredentials) Configure(ctx context.Context, cfg *Config) (creden
return nil, fmt.Errorf("resolve host: %w", err)
}
}
logger.Debugf(ctx, "Generating AAD token via Azure MSI")
log.DebugContext(ctx, "Generating AAD token via Azure MSI")
inner := azureReuseTokenSource(nil, c.tokenSourceFor(ctx, cfg, "", env.AzureApplicationID))
management := azureReuseTokenSource(nil, c.tokenSourceFor(ctx, cfg, "", env.AzureServiceManagementEndpoint()))
visitor := azureVisitor(cfg, serviceToServiceVisitor(inner, management, xDatabricksAzureSpManagementToken))
Expand Down
6 changes: 2 additions & 4 deletions config/auth_azure_msi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import (
"time"

"github.com/databricks/databricks-sdk-go/databricks/apierr"
"github.com/databricks/databricks-sdk-go/databricks/log"
"github.com/databricks/databricks-sdk-go/httpclient/fixtures"
"github.com/databricks/databricks-sdk-go/logger"
"github.com/stretchr/testify/require"
)

func init() {
logger.DefaultLogger = &logger.SimpleLogger{
Level: logger.LevelDebug,
}
log.SetDefaultLogger(log.New(log.LevelDebug))
}

func someValidToken(bearer string) any {
Expand Down
10 changes: 5 additions & 5 deletions config/auth_databricks_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"

"github.com/databricks/databricks-sdk-go/config/credentials"
"github.com/databricks/databricks-sdk-go/logger"
"github.com/databricks/databricks-sdk-go/databricks/log"
"golang.org/x/oauth2"
)

Expand All @@ -30,11 +30,11 @@ func (c DatabricksCliCredentials) Configure(ctx context.Context, cfg *Config) (c
ts, err := newDatabricksCliTokenSource(ctx, cfg)
if err != nil {
if errors.Is(err, exec.ErrNotFound) {
logger.Debugf(ctx, "Most likely the Databricks CLI is not installed")
log.DebugContext(ctx, "Most likely the Databricks CLI is not installed")
return nil, nil
}
if err == errLegacyDatabricksCli {
logger.Debugf(ctx, "Databricks CLI version <0.100.0 detected")
log.DebugContext(ctx, "Databricks CLI version <0.100.0 detected")
return nil, nil
}
return nil, err
Expand All @@ -53,7 +53,7 @@ func (c DatabricksCliCredentials) Configure(ctx context.Context, cfg *Config) (c
}
return nil, err
}
logger.Debugf(ctx, "Using Databricks CLI authentication with Databricks OAuth tokens")
log.DebugContext(ctx, "Using Databricks CLI authentication with Databricks OAuth tokens")
visitor := refreshableVisitor(ts)
return credentials.NewOAuthCredentialsProvider(visitor, ts.Token), nil
}
Expand Down Expand Up @@ -118,6 +118,6 @@ func (ts *databricksCliTokenSource) Token() (*oauth2.Token, error) {
if err != nil {
return nil, fmt.Errorf("cannot unmarshal Databricks CLI result: %w", err)
}
logger.Infof(context.Background(), "Refreshed OAuth token from Databricks CLI, expires on %s", t.Expiry)
log.Info("Refreshed OAuth token from Databricks CLI, expires on %s", t.Expiry)
return &t, nil
}
6 changes: 3 additions & 3 deletions config/auth_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"

"github.com/databricks/databricks-sdk-go/config/credentials"
"github.com/databricks/databricks-sdk-go/logger"
"github.com/databricks/databricks-sdk-go/databricks/log"
)

var authProviders = []CredentialsStrategy{
Expand Down Expand Up @@ -48,10 +48,10 @@ func (c *DefaultCredentials) Configure(ctx context.Context, cfg *Config) (creden
for _, p := range authProviders {
if cfg.AuthType != "" && p.Name() != cfg.AuthType {
// ignore other auth types if one is explicitly enforced
logger.Infof(ctx, "Ignoring %s auth, because %s is preferred", p.Name(), cfg.AuthType)
log.InfoContext(ctx, "Ignoring %s auth, because %s is preferred", p.Name(), cfg.AuthType)
continue
}
logger.Tracef(ctx, "Attempting to configure auth: %s", p.Name())
log.TraceContext(ctx, "Attempting to configure auth: %s", p.Name())
credentialsProvider, err := p.Configure(ctx, cfg)
if err != nil {
return nil, fmt.Errorf("%s: %w", p.Name(), err)
Expand Down
4 changes: 2 additions & 2 deletions config/auth_gcp_google_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"

"github.com/databricks/databricks-sdk-go/config/credentials"
"github.com/databricks/databricks-sdk-go/logger"
"github.com/databricks/databricks-sdk-go/databricks/log"
"golang.org/x/oauth2/google"
"google.golang.org/api/idtoken"
"google.golang.org/api/option"
Expand Down Expand Up @@ -41,7 +41,7 @@ func (c GoogleCredentials) Configure(ctx context.Context, cfg *Config) (credenti
if err != nil {
return nil, fmt.Errorf("could not obtain OAuth2 token from JSON: %w", err)
}
logger.Infof(ctx, "Using Google Credentials")
log.InfoContext(ctx, "Using Google Credentials")
visitor := serviceToServiceVisitor(inner, creds.TokenSource, "X-Databricks-GCP-SA-Access-Token")
return credentials.NewOAuthCredentialsProvider(visitor, inner.Token), nil
}
Expand Down
6 changes: 3 additions & 3 deletions config/auth_gcp_google_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

"github.com/databricks/databricks-sdk-go/config/credentials"
"github.com/databricks/databricks-sdk-go/logger"
"github.com/databricks/databricks-sdk-go/databricks/log"
"golang.org/x/oauth2"
"google.golang.org/api/impersonate"
"google.golang.org/api/option"
Expand All @@ -29,7 +29,7 @@ func (c GoogleDefaultCredentials) Configure(ctx context.Context, cfg *Config) (c
return nil, err
}
if !cfg.IsAccountClient() {
logger.Infof(ctx, "Using Google Default Application Credentials for Workspace")
log.InfoContext(ctx, "Using Google Default Application Credentials for Workspace")
visitor := refreshableVisitor(inner)
return credentials.NewCredentialsProvider(visitor), nil
}
Expand All @@ -44,7 +44,7 @@ func (c GoogleDefaultCredentials) Configure(ctx context.Context, cfg *Config) (c
if err != nil {
return nil, err
}
logger.Infof(ctx, "Using Google Default Application Credentials for Accounts API")
log.InfoContext(ctx, "Using Google Default Application Credentials for Accounts API")
visitor := serviceToServiceVisitor(inner, platform, "X-Databricks-GCP-SA-Access-Token")
return credentials.NewOAuthCredentialsProvider(visitor, inner.Token), nil
}
Expand Down
4 changes: 2 additions & 2 deletions config/auth_m2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"golang.org/x/oauth2/clientcredentials"

"github.com/databricks/databricks-sdk-go/config/credentials"
"github.com/databricks/databricks-sdk-go/databricks/log"
"github.com/databricks/databricks-sdk-go/httpclient"
"github.com/databricks/databricks-sdk-go/logger"
)

var errOAuthNotSupported = errors.New("databricks OAuth is not supported for this host")
Expand All @@ -30,7 +30,7 @@ func (c M2mCredentials) Configure(ctx context.Context, cfg *Config) (credentials
if err != nil {
return nil, fmt.Errorf("oidc: %w", err)
}
logger.Debugf(ctx, "Generating Databricks OAuth token for Service Principal (%s)", cfg.ClientID)
log.DebugContext(ctx, "Generating Databricks OAuth token for Service Principal (%s)", cfg.ClientID)
ts := (&clientcredentials.Config{
ClientID: cfg.ClientID,
ClientSecret: cfg.ClientSecret,
Expand Down
6 changes: 2 additions & 4 deletions config/auth_metadata_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"time"

"github.com/databricks/databricks-sdk-go/config/credentials"
"github.com/databricks/databricks-sdk-go/databricks/log"
"github.com/databricks/databricks-sdk-go/httpclient"
"github.com/databricks/databricks-sdk-go/logger"
"golang.org/x/oauth2"
)

Expand Down Expand Up @@ -107,8 +107,6 @@ func (t metadataService) Token() (*oauth2.Token, error) {
if token == nil {
return nil, fmt.Errorf("no token returned from metadata service")
}
logger.Debugf(context.Background(),
"Refreshed access token from local metadata service, which expires on %s",
token.Expiry.Format(time.RFC3339))
log.Debug("Refreshed access token from local metadata service, which expires on %s", token.Expiry.Format(time.RFC3339))
return token, nil
}
4 changes: 2 additions & 2 deletions config/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"strings"

"github.com/databricks/databricks-sdk-go/databricks/apierr"
"github.com/databricks/databricks-sdk-go/databricks/log"
"github.com/databricks/databricks-sdk-go/httpclient"
"github.com/databricks/databricks-sdk-go/logger"
"golang.org/x/oauth2"
)

Expand Down Expand Up @@ -137,7 +137,7 @@ func (c *Config) azureEnsureWorkspaceUrl(ctx context.Context, ahr azureHostResol
return fmt.Errorf("resolve workspace: %w", err)
}
c.Host = fmt.Sprintf("https://%s", workspaceMetadata.Properties.WorkspaceURL)
logger.Debugf(ctx, "Discovered workspace url: %s", c.Host)
log.DebugContext(ctx, "Discovered workspace url: %s", c.Host)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"os/exec"
"strings"

"github.com/databricks/databricks-sdk-go/logger"
"github.com/databricks/databricks-sdk-go/databricks/log"
)

// Run a command and return the output.
func runCommand(ctx context.Context, cmd string, args []string) ([]byte, error) {
logger.Debugf(ctx, "Running command: %s %v", cmd, strings.Join(args, " "))
log.DebugContext(ctx, "Running command: %s %v", cmd, strings.Join(args, " "))
return exec.Command(cmd, args...).Output()
}
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/databricks/databricks-sdk-go/common"
"github.com/databricks/databricks-sdk-go/common/environment"
"github.com/databricks/databricks-sdk-go/config/credentials"
"github.com/databricks/databricks-sdk-go/databricks/log"
"github.com/databricks/databricks-sdk-go/httpclient"
"github.com/databricks/databricks-sdk-go/logger"
"golang.org/x/oauth2"
)

Expand Down Expand Up @@ -291,7 +291,7 @@ func (c *Config) EnsureResolved() error {
}
ctx := context.Background()
for _, loader := range c.Loaders {
logger.Tracef(ctx, "Loading config via %s", loader.Name())
log.TraceContext(ctx, "Loading config via %s", loader.Name())
err := loader.Configure(c)
if err != nil {

Expand Down
9 changes: 4 additions & 5 deletions config/config_file.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package config

import (
"context"
"fmt"
"os"
"strings"

"github.com/databricks/databricks-sdk-go/logger"
"github.com/databricks/databricks-sdk-go/databricks/log"
"gopkg.in/ini.v1"
)

Expand Down Expand Up @@ -78,7 +77,7 @@ func (l configFileLoader) Configure(cfg *Config) error {
if err != nil {
if os.IsNotExist(err) {
// early return for non-configured machines
logger.Debugf(context.Background(), "%s not found on current host", configFilePath)
log.Debug("%s not found on current host", configFilePath)
return nil
}
return fmt.Errorf("cannot parse config file: %w", err)
Expand All @@ -92,12 +91,12 @@ func (l configFileLoader) Configure(cfg *Config) error {
profileValues := configFile.Section(profile)
if len(profileValues.Keys()) == 0 {
if !hasExplicitProfile {
logger.Debugf(context.Background(), "%s has no %s profile configured", configFile.Path(), profile)
log.Debug("%s has no %s profile configured", configFile.Path(), profile)
return nil
}
return fmt.Errorf("%s has no %s profile configured", configFile.Path(), profile)
}
logger.Debugf(context.Background(), "Loading %s profile from %s", profile, configFile.Path())
log.Debug("Loading %s profile from %s", profile, configFile.Path())
err = ConfigAttributes.ResolveFromStringMapWithSource(cfg, profileValues.KeysHash(), Source{Type: SourceFile, Name: configFile.Path()})
if err != nil {
return fmt.Errorf("%s %s profile: %w", configFile.Path(), profile, err)
Expand Down
Loading
Loading