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

[extension/awsmiddleware] Add ID to context to trace requests. #128

Merged
merged 5 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix README
  • Loading branch information
jefchien committed Oct 20, 2023
commit 89f769fae0842ed404080c20c47caaf0c1422e25
5 changes: 3 additions & 2 deletions extension/awsmiddleware/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# AWS Middleware

An AWS middleware extension provides request and/or response handlers that can be configured on AWS SDK v1/v2 clients.
Other components can configure their AWS SDK clients using the `awsmiddleware.ConfigureSDKv1` and `awsmiddleware.ConfigureSDKv2` functions.
Other components can configure their AWS SDK clients using `awsmiddleware.GetConfigurer` and the `ConfigureSDKv1` and
`ConfigureSDKv2` functions available on the `Configurer`.

The `awsmiddleware.Extension` interface extends `component.Extension` by adding the following method:
```
Expand All @@ -26,4 +27,4 @@ HandleResponse(id string, r *http.Response)
- `Position` determines whether the handler is appended to the front or back of the existing list. Insertion is done
in the order of the handlers provided.
- `HandleRequest/Response` provides a hook to handle the request/response before and after they've been sent along
with an attached ID.
with an attached request ID.
2 changes: 1 addition & 1 deletion extension/awsmiddleware/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ func GetConfigurer(extensions map[component.ID]component.Component, middlewareID
if err != nil {
return nil, err
}
return NewConfigurer(middleware), nil
return NewConfigurer(middleware.Handlers()), nil
}
4 changes: 3 additions & 1 deletion extension/awsmiddleware/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func TestGetConfigurer(t *testing.T) {
id := component.NewID("test")
nopExtension, err := extensiontest.NewNopBuilder().Create(context.Background(), extensiontest.NewNopCreateSettings())
require.Error(t, err)
middlewareExtension := new(MockMiddlewareExtension)
middlewareExtension.On("Handlers").Return(nil, nil)
testCases := map[string]struct {
extensions map[component.ID]component.Component
wantErr error
Expand All @@ -30,7 +32,7 @@ func TestGetConfigurer(t *testing.T) {
wantErr: errNotMiddleware,
},
"WithMiddlewareExtension": {
extensions: map[component.ID]component.Component{id: new(MockMiddlewareExtension)},
extensions: map[component.ID]component.Component{id: middlewareExtension},
},
}
for name, testCase := range testCases {
Expand Down
27 changes: 14 additions & 13 deletions extension/awsmiddleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,30 @@ type Extension interface {
Middleware
}

// Configurer wraps a Middleware and provides convenience functions
// for applying it to the AWS SDKs.
// Configurer provides functions for applying request/response handlers
// to the AWS SDKs.
type Configurer struct {
Middleware
requestHandlers []RequestHandler
responseHandlers []ResponseHandler
}

// NewConfigurer wraps the Middleware.
func NewConfigurer(mw Middleware) *Configurer {
return &Configurer{Middleware: mw}
// NewConfigurer sets the request/response handlers.
func NewConfigurer(requestHandlers []RequestHandler, responseHandlers []ResponseHandler) *Configurer {
return &Configurer{requestHandlers: requestHandlers, responseHandlers: responseHandlers}
}

// ConfigureSDKv1 adds middleware to the AWS SDK v1. Request handlers are added to the
// Build handler list and response handlers are added to the Unmarshal handler list.
// Build handler list and response handlers are added to the ValidateResponse handler list.
// Build will only be run once per request, but if there are errors, ValidateResponse will
// be run again for each configured retry.
func (c *Configurer) ConfigureSDKv1(handlers *request.Handlers) error {
var errs error
requestHandlers, responseHandlers := c.Middleware.Handlers()
for _, handler := range requestHandlers {
for _, handler := range c.requestHandlers {
if err := appendHandler(&handlers.Build, namedRequestHandler(handler), handler.Position()); err != nil {
errs = errors.Join(errs, fmt.Errorf("%w (%q): %w", errInvalidHandler, handler.ID(), err))
}
}
for _, handler := range responseHandlers {
for _, handler := range c.responseHandlers {
if err := appendHandler(&handlers.ValidateResponse, namedResponseHandler(handler), handler.Position()); err != nil {
errs = errors.Join(errs, fmt.Errorf("%w (%q): %w", errInvalidHandler, handler.ID(), err))
}
Expand All @@ -139,16 +141,15 @@ func (c *Configurer) ConfigureSDKv1(handlers *request.Handlers) error {
// Build step and response handlers are added to the Deserialize step.
func (c *Configurer) ConfigureSDKv2(config *aws.Config) error {
var errs error
requestHandlers, responseHandlers := c.Middleware.Handlers()
for _, handler := range requestHandlers {
for _, handler := range c.requestHandlers {
relativePosition, err := toRelativePosition(handler.Position())
if err != nil {
errs = errors.Join(errs, fmt.Errorf("%w (%q): %w", errInvalidHandler, handler.ID(), err))
continue
}
config.APIOptions = append(config.APIOptions, withBuildOption(&requestMiddleware{RequestHandler: handler}, relativePosition))
}
for _, handler := range responseHandlers {
for _, handler := range c.responseHandlers {
relativePosition, err := toRelativePosition(handler.Position())
if err != nil {
errs = errors.Join(errs, fmt.Errorf("%w (%q): %w", errInvalidHandler, handler.ID(), err))
Expand Down
8 changes: 4 additions & 4 deletions extension/awsmiddleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestInvalidHandlers(t *testing.T) {
handler.On("Position").Return(HandlerPosition(-1))
middleware := new(MockMiddlewareExtension)
middleware.On("Handlers").Return([]RequestHandler{handler}, []ResponseHandler{handler})
c := NewConfigurer(middleware)
c := NewConfigurer(middleware.Handlers())
// v1
client := awstesting.NewClient()
err := c.ConfigureSDKv1(&client.Handlers)
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestAppendOrder(t *testing.T) {
requestHandlers,
[]ResponseHandler{handler},
)
c := NewConfigurer(middleware)
c := NewConfigurer(middleware.Handlers())
// v1
client := awstesting.NewClient(&awsv1.Config{
Region: awsv1.String("mock-region"),
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestConfigureSDKv1(t *testing.T) {
})
require.Equal(t, 3, client.Handlers.Build.Len())
require.Equal(t, 1, client.Handlers.ValidateResponse.Len())
assert.NoError(t, NewConfigurer(middleware).ConfigureSDKv1(&client.Handlers))
assert.NoError(t, NewConfigurer(middleware.Handlers()).ConfigureSDKv1(&client.Handlers))
assert.Equal(t, 5, client.Handlers.Build.Len())
assert.Equal(t, 2, client.Handlers.ValidateResponse.Len())
s3Client := &s3v1.S3{Client: client}
Expand All @@ -238,7 +238,7 @@ func TestConfigureSDKv2(t *testing.T) {
middleware, recorder, server := setup(t)
defer server.Close()
cfg := awsv2.Config{Region: "us-east-1", RetryMaxAttempts: 0}
assert.NoError(t, NewConfigurer(middleware).ConfigureSDKv2(&cfg))
assert.NoError(t, NewConfigurer(middleware.Handlers()).ConfigureSDKv2(&cfg))
s3Client := s3v2.NewFromConfig(cfg, func(options *s3v2.Options) {
options.BaseEndpoint = awsv2.String(server.URL)
})
Expand Down
Loading