-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathlogger.go
333 lines (248 loc) · 10.3 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package awsbase
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"net/http"
"net/textproto"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/s3"
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/aws-sdk-go-v2/service/sqs"
smithylogging "github.com/aws/smithy-go/logging"
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
"github.com/hashicorp/aws-sdk-go-base/v2/logging"
"go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/semconv/v1.17.0/httpconv"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
)
type debugLogger struct {
ctx context.Context
}
func (l debugLogger) Logf(classification smithylogging.Classification, format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
if l.ctx != nil {
logger := logging.RetrieveLogger(l.ctx)
switch classification {
case smithylogging.Debug:
logger.Debug(l.ctx, s)
case smithylogging.Warn:
logger.Warn(l.ctx, s)
}
} else {
s = strings.ReplaceAll(s, "\r", "") // Works around https://github.com/jen20/teamcity-go-test/pull/2
log.Printf("[%s] missing_context: %s "+string(logging.AwsSdkKey)+"="+awsSdkGoV2Val, classification, s)
}
}
func (l debugLogger) WithContext(ctx context.Context) smithylogging.Logger {
return &debugLogger{
ctx: ctx,
}
}
const awsSdkGoV2Val = "aws-sdk-go-v2"
func awsSDKv2Attr() attribute.KeyValue {
return logging.AwsSdkKey.String(awsSdkGoV2Val)
}
type logAttributeExtractor struct{}
func (l *logAttributeExtractor) ID() string {
return "TF_AWS_LogAttributeExtractor"
}
func (l *logAttributeExtractor) HandleInitialize(ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler) (
out middleware.InitializeOutput, metadata middleware.Metadata, err error) {
logger := logging.RetrieveLogger(ctx)
region := awsmiddleware.GetRegion(ctx)
serviceID := awsmiddleware.GetServiceID(ctx)
attributes := []attribute.KeyValue{
otelaws.SystemAttr(),
otelaws.ServiceAttr(serviceID),
otelaws.RegionAttr(region),
otelaws.OperationAttr(awsmiddleware.GetOperationName(ctx)),
awsSDKv2Attr(),
}
setters := map[string]otelaws.AttributeSetter{
dynamodb.ServiceID: otelaws.DynamoDBAttributeSetter,
s3.ServiceID: s3AttributeSetter,
sqs.ServiceID: otelaws.SQSAttributeSetter,
}
if setter, ok := setters[serviceID]; ok {
attributes = append(attributes, setter(ctx, in)...)
}
for _, attribute := range attributes {
ctx = logger.SetField(ctx, string(attribute.Key), attribute.Value.AsInterface())
}
return next.HandleInitialize(ctx, in)
}
// Replaces the built-in logging middleware from https://github.com/aws/smithy-go/blob/main/transport/http/middleware_http_logging.go
// We want access to the request and response structs, 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 v2 without forcing us to manually implement
// configuration that the SDK handles for us.
type requestResponseLogger struct{}
// ID is the middleware identifier.
func (r *requestResponseLogger) ID() string {
return "TF_AWS_RequestResponseLogger"
}
func (r *requestResponseLogger) HandleDeserialize(ctx context.Context, in middleware.DeserializeInput, next middleware.DeserializeHandler,
) (
out middleware.DeserializeOutput, metadata middleware.Metadata, err error,
) {
logger := logging.RetrieveLogger(ctx)
region := awsmiddleware.GetRegion(ctx)
if signingRegion := awsmiddleware.GetSigningRegion(ctx); signingRegion != region { //nolint:staticcheck // Not retrievable elsewhere
ctx = logger.SetField(ctx, string(logging.SigningRegionKey), signingRegion)
}
if awsmiddleware.GetEndpointSource(ctx) == aws.EndpointSourceCustom {
ctx = logger.SetField(ctx, string(logging.CustomEndpointKey), true)
}
smithyRequest, ok := in.Request.(*smithyhttp.Request)
if !ok {
return out, metadata, fmt.Errorf("unknown request type %T", in.Request)
}
rc := smithyRequest.Build(ctx)
requestFields, err := logging.DecomposeHTTPRequest(ctx, rc)
if err != nil {
return out, metadata, fmt.Errorf("decomposing request: %w", err)
}
logger.Debug(ctx, "HTTP Request Sent", requestFields)
smithyRequest, err = smithyRequest.SetStream(rc.Body)
if err != nil {
return out, metadata, err
}
in.Request = smithyRequest
start := time.Now()
out, metadata, err = next.HandleDeserialize(ctx, in)
elapsed := time.Since(start)
if err == nil {
smithyResponse, ok := out.RawResponse.(*smithyhttp.Response)
if !ok {
return out, metadata, fmt.Errorf("unknown response type: %T", out.RawResponse)
}
responseFields, err := decomposeHTTPResponse(ctx, smithyResponse.Response, elapsed)
if err != nil {
return out, metadata, fmt.Errorf("decomposing response: %w", err)
}
logger.Debug(ctx, "HTTP Response Received", responseFields)
}
return out, metadata, err
}
func decomposeHTTPResponse(ctx context.Context, resp *http.Response, elapsed time.Duration) (map[string]any, error) {
var attributes []attribute.KeyValue
attributes = append(attributes, attribute.Int64("http.duration", elapsed.Milliseconds()))
attributes = append(attributes, httpconv.ClientResponse(resp)...)
attributes = append(attributes, logging.DecomposeResponseHeaders(resp)...)
bodyLogger := responseBodyLogger(ctx)
err := bodyLogger.Log(ctx, resp, &attributes)
if err != nil {
return nil, err
}
result := make(map[string]any, len(attributes))
for _, attribute := range attributes {
result[string(attribute.Key)] = attribute.Value.AsInterface()
}
return result, nil
}
func responseBodyLogger(ctx context.Context) logging.ResponseBodyLogger {
if awsmiddleware.GetServiceID(ctx) == "S3" {
if op := awsmiddleware.GetOperationName(ctx); op == "GetObject" {
return &logging.S3ObjectResponseBodyLogger{}
}
}
return &defaultResponseBodyLogger{}
}
var _ logging.ResponseBodyLogger = &defaultResponseBodyLogger{}
type defaultResponseBodyLogger struct{}
func (l *defaultResponseBodyLogger) Log(ctx context.Context, resp *http.Response, attrs *[]attribute.KeyValue) error {
content, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
// Restore the body reader
resp.Body = io.NopCloser(bytes.NewBuffer(content))
reader := textproto.NewReader(bufio.NewReader(bytes.NewReader(content)))
body, err := logging.ReadTruncatedBody(reader, logging.MaxResponseBodyLen)
if err != nil {
return err
}
*attrs = append(*attrs, attribute.String("http.response.body", body))
return nil
}
// May be contributed to go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws
// See: https://github.com/open-telemetry/opentelemetry-go-contrib/issues/4321
func s3AttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue {
s3Attributes := []attribute.KeyValue{}
switch v := in.Parameters.(type) {
case *s3.AbortMultipartUploadInput:
s3Attributes = append(s3Attributes, semconv.AWSS3Bucket(aws.ToString(v.Bucket)))
s3Attributes = append(s3Attributes, semconv.AWSS3Key(aws.ToString(v.Key)))
s3Attributes = append(s3Attributes, semconv.AWSS3UploadID(aws.ToString(v.UploadId)))
case *s3.CompleteMultipartUploadInput:
s3Attributes = append(s3Attributes, semconv.AWSS3Bucket(aws.ToString(v.Bucket)))
s3Attributes = append(s3Attributes, semconv.AWSS3Key(aws.ToString(v.Key)))
s3Attributes = append(s3Attributes, semconv.AWSS3UploadID(aws.ToString(v.UploadId)))
case *s3.CreateBucketInput:
s3Attributes = append(s3Attributes, semconv.AWSS3Bucket(aws.ToString(v.Bucket)))
case *s3.CreateMultipartUploadInput:
s3Attributes = append(s3Attributes, semconv.AWSS3Bucket(aws.ToString(v.Bucket)))
s3Attributes = append(s3Attributes, semconv.AWSS3Key(aws.ToString(v.Key)))
case *s3.DeleteBucketInput:
s3Attributes = append(s3Attributes, semconv.AWSS3Bucket(aws.ToString(v.Bucket)))
case *s3.DeleteObjectInput:
s3Attributes = append(s3Attributes, semconv.AWSS3Bucket(aws.ToString(v.Bucket)))
s3Attributes = append(s3Attributes, semconv.AWSS3Key(aws.ToString(v.Key)))
case *s3.DeleteObjectsInput:
s3Attributes = append(s3Attributes, semconv.AWSS3Bucket(aws.ToString(v.Bucket)))
s3Attributes = append(s3Attributes, semconv.AWSS3Delete(serializeDeleteShorthand(v.Delete)))
case *s3.GetObjectInput:
s3Attributes = append(s3Attributes, semconv.AWSS3Bucket(aws.ToString(v.Bucket)))
s3Attributes = append(s3Attributes, semconv.AWSS3Key(aws.ToString(v.Key)))
case *s3.HeadBucketInput:
s3Attributes = append(s3Attributes, semconv.AWSS3Bucket(aws.ToString(v.Bucket)))
case *s3.HeadObjectInput:
s3Attributes = append(s3Attributes, semconv.AWSS3Bucket(aws.ToString(v.Bucket)))
s3Attributes = append(s3Attributes, semconv.AWSS3Key(aws.ToString(v.Key)))
case *s3.ListBucketsInput:
// ListBucketsInput defines no attributes
case *s3.ListObjectsInput:
s3Attributes = append(s3Attributes, semconv.AWSS3Bucket(aws.ToString(v.Bucket)))
case *s3.ListObjectsV2Input:
s3Attributes = append(s3Attributes, semconv.AWSS3Bucket(aws.ToString(v.Bucket)))
case *s3.PutObjectInput:
s3Attributes = append(s3Attributes, semconv.AWSS3Bucket(aws.ToString(v.Bucket)))
s3Attributes = append(s3Attributes, semconv.AWSS3Key(aws.ToString(v.Key)))
case *s3.UploadPartInput:
s3Attributes = append(s3Attributes, semconv.AWSS3Bucket(aws.ToString(v.Bucket)))
s3Attributes = append(s3Attributes, semconv.AWSS3Key(aws.ToString(v.Key)))
s3Attributes = append(s3Attributes, semconv.AWSS3PartNumber(int(aws.ToInt32(v.PartNumber))))
s3Attributes = append(s3Attributes, semconv.AWSS3UploadID(aws.ToString(v.UploadId)))
}
return s3Attributes
}
func serializeDeleteShorthand(d *s3types.Delete) string {
var builder strings.Builder
fmt.Fprint(&builder, "Objects=[")
count := len(d.Objects)
for i, object := range d.Objects {
fmt.Fprint(&builder, "{")
fmt.Fprintf(&builder, "Key=%s", aws.ToString(object.Key))
if object.VersionId != nil {
fmt.Fprintf(&builder, ",VersionId=%s", aws.ToString(object.VersionId))
}
fmt.Fprint(&builder, "}")
if i+1 != count {
fmt.Fprint(&builder, ",")
}
}
fmt.Fprint(&builder, "],")
fmt.Fprintf(&builder, "Quiet=%t", aws.ToBool(d.Quiet))
return builder.String()
}