Adds `tf_backend.operation` log key

s3/request-logging
Graham Davison 3 years ago
parent 61ba05b77b
commit f2ef4e155f

@ -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")
})

@ -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.

@ -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,

Loading…
Cancel
Save