Skip to content

Commit

Permalink
Adds duration for AWS SDK v1 requests
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison committed Feb 1, 2023
1 parent 273924f commit 8e0a692
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion v2/awsv1shim/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func setAWSFields(ctx context.Context, r *request.Request) context.Context {
return ctx
}

type durationKeyT string

const durationKey durationKeyT = "request-duration"

// Replaces the built-in logging middleware from https://github.com/aws/aws-sdk-go/blob/main/aws/client/logger.go
// We want access to the request struct, and cannot get it from the built-in.
// The typical route of adding logging to the http.RoundTripper doesn't work for the AWS SDK for Go v1 without forcing us to manually implement
Expand Down Expand Up @@ -81,6 +85,10 @@ func logRequest(r *request.Request) {
}

tflog.Debug(ctx, "HTTP Request Sent", requestFields)

ctx = context.WithValue(ctx, durationKey, time.Now())

r.SetContext(ctx)
}

// Replaces the built-in logging middleware from https://github.com/aws/aws-sdk-go/blob/main/aws/client/logger.go
Expand Down Expand Up @@ -112,9 +120,14 @@ func logResponse(r *request.Request) {
handlerFn := func(req *request.Request) {
ctx := r.Context()

var elapsed time.Duration
if start, ok := ctx.Value(durationKey).(time.Time); ok {
elapsed = time.Since(start)
}

ctx = setAWSFields(ctx, r)

responseFields, err := decomposeHTTPResponse(r.HTTPResponse, bodyBuffer, 0)
responseFields, err := decomposeHTTPResponse(r.HTTPResponse, bodyBuffer, elapsed)
if err != nil {
tflog.Error(ctx, fmt.Sprintf("decomposing response: %s", err))
return
Expand Down

0 comments on commit 8e0a692

Please sign in to comment.