Skip to content

Commit

Permalink
[extension/awsmiddleware] Add ID to context to trace requests. (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefchien authored Oct 20, 2023
1 parent 6e9de26 commit a8ff477
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 151 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exporter/tencentcloudlogserviceexporter/ @open-telemetry/collect
exporter/zipkinexporter/ @open-telemetry/collector-contrib-approvers @MovieStoreGuy @astencel-sumo @crobert-1

extension/asapauthextension/ @open-telemetry/collector-contrib-approvers @jamesmoessis @MovieStoreGuy
extension/awsmiddleware @open-telemetry/collector-contrib-approvers @jefchien
extension/awsmiddleware/ @open-telemetry/collector-contrib-approvers @jefchien
extension/awsproxy/ @open-telemetry/collector-contrib-approvers @Aneurysm9 @mxiamxia
extension/basicauthextension/ @open-telemetry/collector-contrib-approvers @jpkrohling @svrakitin @frzifus
extension/bearertokenauthextension/ @open-telemetry/collector-contrib-approvers @jpkrohling @pavankrish123 @frzifus
Expand Down
1 change: 1 addition & 0 deletions cmd/checkapi/allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ exporter/lokiexporter
exporter/pulsarexporter
exporter/sentryexporter
exporter/sumologicexporter
extension/awsmiddleware
extension/observer/ecsobserver
extension/observer
extension/observer/k8sobserver
Expand Down
15 changes: 8 additions & 7 deletions extension/awsmiddleware/README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
# 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 methods:
The `awsmiddleware.Extension` interface extends `component.Extension` by adding the following method:
```
RequestHandlers() []RequestHandler
ResponseHandlers() []ResponseHandler
Handlers() ([]RequestHandler, []ResponseHandler)
```

The `awsmiddleware.RequestHandler` interface contains the following methods:
```
ID() string
Position() HandlerPosition
HandleRequest(r *http.Request)
HandleRequest(id string, r *http.Request)
```

The `awsmiddleware.ResponseHandler` interface contains the following methods:
```
ID() string
Position() HandlerPosition
HandleResponse(r *http.Response)
HandleResponse(id string, r *http.Response)
```

- `ID` uniquely identifies a handler. Middleware will fail if there is clashing
- `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.
- `HandleRequest/Response` provides a hook to handle the request/response before and after they've been sent along
with an attached request ID.
30 changes: 18 additions & 12 deletions extension/awsmiddleware/config.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package awsmiddleware // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/awsmiddleware"
package awsmiddleware // import "github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware"

import (
"fmt"

"go.opentelemetry.io/collector/component"
)

// Config defines the configuration for an AWS Middleware extension.
type Config struct {
// MiddlewareID is the ID of the Middleware extension.
MiddlewareID component.ID `mapstructure:"middleware"`
}
type ID = component.ID

// GetMiddleware retrieves the extension implementing Middleware based on the MiddlewareID.
func (c Config) GetMiddleware(extensions map[component.ID]component.Component) (Middleware, error) {
if ext, found := extensions[c.MiddlewareID]; found {
if mw, ok := ext.(Middleware); ok {
return mw, nil
// getMiddleware retrieves the extension implementing Middleware based on the middlewareID.
func getMiddleware(extensions map[component.ID]component.Component, middlewareID ID) (Middleware, error) {
if extension, found := extensions[middlewareID]; found {
if middleware, ok := extension.(Middleware); ok {
return middleware, nil
}
return nil, errNotMiddleware
}
return nil, fmt.Errorf("failed to resolve AWS client handler %q: %w", c.MiddlewareID, errNotFound)
return nil, fmt.Errorf("failed to resolve AWS middleware %q: %w", middlewareID, errNotFound)
}

// GetConfigurer retrieves the extension implementing Middleware based on the middlewareID and
// wraps it in a Configurer.
func GetConfigurer(extensions map[component.ID]component.Component, middlewareID ID) (*Configurer, error) {
middleware, err := getMiddleware(extensions, middlewareID)
if err != nil {
return nil, err
}
return newConfigurer(middleware.Handlers()), nil
}
26 changes: 5 additions & 21 deletions extension/awsmiddleware/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,12 @@ import (
"go.opentelemetry.io/collector/extension/extensiontest"
)

type testMiddlewareExtension struct {
component.StartFunc
component.ShutdownFunc
requestHandlers []RequestHandler
responseHandlers []ResponseHandler
}

var _ Extension = (*testMiddlewareExtension)(nil)

func (t *testMiddlewareExtension) RequestHandlers() []RequestHandler {
return t.requestHandlers
}

func (t *testMiddlewareExtension) ResponseHandlers() []ResponseHandler {
return t.responseHandlers
}

func TestGetMiddleware(t *testing.T) {
func TestGetConfigurer(t *testing.T) {
id := component.NewID("test")
cfg := &Config{MiddlewareID: id}
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 @@ -48,12 +32,12 @@ func TestGetMiddleware(t *testing.T) {
wantErr: errNotMiddleware,
},
"WithMiddlewareExtension": {
extensions: map[component.ID]component.Component{id: &testMiddlewareExtension{}},
extensions: map[component.ID]component.Component{id: middlewareExtension},
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
got, err := cfg.GetMiddleware(testCase.extensions)
got, err := GetConfigurer(testCase.extensions, id)
if testCase.wantErr != nil {
assert.Error(t, err)
assert.ErrorIs(t, err, testCase.wantErr)
Expand Down
2 changes: 1 addition & 1 deletion extension/awsmiddleware/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

// Package awsmiddleware defines an extension interface providing request and response handlers that can be
// configured on AWS SDK clients.
package awsmiddleware // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/awsmiddleware"
package awsmiddleware // import "github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware"
38 changes: 20 additions & 18 deletions extension/awsmiddleware/go.mod
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
module github.com/open-telemetry/opentelemetry-collector-contrib/extension/awsmiddleware
module github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware

go 1.20

require (
github.com/aws/aws-sdk-go v1.45.24
github.com/aws/aws-sdk-go-v2 v1.21.1
github.com/aws/aws-sdk-go v1.45.2
github.com/aws/aws-sdk-go-v2 v1.21.2
github.com/aws/aws-sdk-go-v2/service/s3 v1.40.0
github.com/aws/smithy-go v1.15.0
github.com/google/uuid v1.3.1
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/collector/component v0.87.0
go.opentelemetry.io/collector/extension v0.87.0
go.opentelemetry.io/collector/component v0.84.1-0.20230908201109-ab3d6c5b6470
go.opentelemetry.io/collector/extension v0.84.1-0.20230908201109-ab3d6c5b6470
)

require (
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.13 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.42 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.36 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.41 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.35 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.1.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.14 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.36 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.36 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.35 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.15.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand All @@ -32,20 +33,21 @@ require (
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.87.0 // indirect
go.opentelemetry.io/collector/confmap v0.87.0 // indirect
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0016 // indirect
go.opentelemetry.io/collector/pdata v1.0.0-rcv0016 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.84.1-0.20230908201109-ab3d6c5b6470 // indirect
go.opentelemetry.io/collector/confmap v0.84.1-0.20230908201109-ab3d6c5b6470 // indirect
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0014.0.20230908201109-ab3d6c5b6470 // indirect
go.opentelemetry.io/collector/pdata v1.0.0-rcv0014.0.20230908201109-ab3d6c5b6470 // indirect
go.opentelemetry.io/otel v1.17.0 // indirect
go.opentelemetry.io/otel/metric v1.17.0 // indirect
go.opentelemetry.io/otel/trace v1.17.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.2 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit a8ff477

Please sign in to comment.