From f2ef4e155f195c49c36900a57b1b475a24b20fda Mon Sep 17 00:00:00 2001 From: Graham Davison Date: Fri, 8 Sep 2023 10:58:34 -0700 Subject: [PATCH] Adds `tf_backend.operation` log key --- internal/backend/remote-state/s3/backend.go | 9 +---- internal/backend/remote-state/s3/client.go | 18 +++++----- internal/backend/remote-state/s3/logging.go | 38 +++++++++++++++++++++ 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/internal/backend/remote-state/s3/backend.go b/internal/backend/remote-state/s3/backend.go index 1fecc86b3c..2474f19df1 100644 --- a/internal/backend/remote-state/s3/backend.go +++ b/internal/backend/remote-state/s3/backend.go @@ -11,7 +11,6 @@ import ( "regexp" "sort" "strings" - "sync" "time" "github.com/aws/aws-sdk-go-v2/aws" @@ -20,10 +19,8 @@ import ( "github.com/aws/aws-sdk-go-v2/service/s3" awsbase "github.com/hashicorp/aws-sdk-go-base/v2" baselogging "github.com/hashicorp/aws-sdk-go-base/v2/logging" - "github.com/hashicorp/go-hclog" "github.com/hashicorp/terraform/internal/backend" "github.com/hashicorp/terraform/internal/configs/configschema" - "github.com/hashicorp/terraform/internal/logging" "github.com/hashicorp/terraform/internal/tfdiags" "github.com/hashicorp/terraform/version" "github.com/zclconf/go-cty/cty" @@ -608,6 +605,7 @@ func formatDeprecations(attrs map[string]string) string { func (b *Backend) Configure(obj cty.Value) tfdiags.Diagnostics { ctx := context.TODO() log := logger() + log = logWithOperation(log, operationBackendConfigure) var diags tfdiags.Diagnostics if obj.IsNull() { @@ -1648,8 +1646,3 @@ func deprecatedEnvVarDiag(envvar, replacement string) tfdiags.Diagnostic { fmt.Sprintf(`The environment variable "%s" is deprecated. Use environment variable "%s" instead.`, envvar, replacement), ) } - -var logger = sync.OnceValue(func() hclog.Logger { - l := logging.HCLogger() - return l.Named("backend-s3") -}) diff --git a/internal/backend/remote-state/s3/client.go b/internal/backend/remote-state/s3/client.go index 8a06f45450..d49cfe387c 100644 --- a/internal/backend/remote-state/s3/client.go +++ b/internal/backend/remote-state/s3/client.go @@ -63,7 +63,8 @@ var testChecksumHook func() func (c *RemoteClient) Get() (payload *remote.Payload, err error) { ctx := context.TODO() - log := c.logger() + log := c.logger(operationClientGet) + ctx, baselog := baselogging.NewHcLogger(ctx, log) ctx = baselogging.RegisterLogger(ctx, baselog) @@ -182,7 +183,7 @@ func (c *RemoteClient) get(ctx context.Context) (*remote.Payload, error) { func (c *RemoteClient) Put(data []byte) error { ctx := context.TODO() - log := c.logger() + log := c.logger(operationClientPut) ctx, baselog := baselogging.NewHcLogger(ctx, log) ctx = baselogging.RegisterLogger(ctx, baselog) @@ -233,7 +234,7 @@ func (c *RemoteClient) Put(data []byte) error { func (c *RemoteClient) Delete() error { ctx := context.TODO() - log := c.logger() + log := c.logger(operationClientDelete) ctx, baselog := baselogging.NewHcLogger(ctx, log) ctx = baselogging.RegisterLogger(ctx, baselog) @@ -260,7 +261,7 @@ func (c *RemoteClient) Delete() error { func (c *RemoteClient) Lock(info *statemgr.LockInfo) (string, error) { ctx := context.TODO() - log := c.logger() + log := c.logger(operationLockerLock) if c.ddbTable == "" { return "", nil @@ -315,7 +316,7 @@ func (c *RemoteClient) Lock(info *statemgr.LockInfo) (string, error) { func (c *RemoteClient) Unlock(id string) error { ctx := context.TODO() - log := c.logger() + log := c.logger(operationLockerUnlock) log = logWithLockID(log, id) @@ -489,12 +490,13 @@ func (c *RemoteClient) getSSECustomerKeyMD5() string { return base64.StdEncoding.EncodeToString(b[:]) } -// logger returns the S3 backend logger configured with the client's bucket and path -func (c *RemoteClient) logger() hclog.Logger { - return logger().With( +// logger returns the S3 backend logger configured with the client's bucket and path and the operation +func (c *RemoteClient) logger(operation string) hclog.Logger { + log := logger().With( logKeyBucket, c.bucketName, logKeyPath, c.path, ) + return logWithOperation(log, operation) } const errBadChecksumFmt = `state data in S3 does not have the expected content. diff --git a/internal/backend/remote-state/s3/logging.go b/internal/backend/remote-state/s3/logging.go index fe765e3e23..8dc54b7727 100644 --- a/internal/backend/remote-state/s3/logging.go +++ b/internal/backend/remote-state/s3/logging.go @@ -1,7 +1,10 @@ package s3 import ( + "sync" + "github.com/hashicorp/go-hclog" + "github.com/hashicorp/terraform/internal/logging" "github.com/hashicorp/terraform/internal/states/statemgr" ) @@ -19,6 +22,41 @@ const ( logKeyBackendLockPath = "tf_backend.lock.path" ) +const ( + logKeyBackendOperation = "tf_backend.operation" +) + +const ( + operationBackendConfigSchema = "ConfigSchema" + operationBackendPrepareConfig = "PrepareConfig" + operationBackendConfigure = "Configure" + operationBackendStateMgr = "StateMgr" + operationBackendDeleteWorkspace = "DeleteWorkspace" + operationBackendWorkspaces = "Workspaces" +) + +const ( + operationClientGet = "Get" + operationClientPut = "Put" + operationClientDelete = "Delete" +) + +const ( + operationLockerLock = "Lock" + operationLockerUnlock = "Unlock" +) + +var logger = sync.OnceValue(func() hclog.Logger { + l := logging.HCLogger() + return l.Named("backend-s3") +}) + +func logWithOperation(in hclog.Logger, operation string) hclog.Logger { + return in.With( + logKeyBackendOperation, operation, + ) +} + func logWithLockInfo(in hclog.Logger, info *statemgr.LockInfo) hclog.Logger { return in.With( logKeyBackendLockId, info.ID,