Skip to content

Commit

Permalink
[extension/awsmiddleware] Add AWS middleware extension interface (ama…
Browse files Browse the repository at this point in the history
  • Loading branch information
jefchien authored and lisguo committed Oct 20, 2023
1 parent 7db3e1b commit 607b652
Show file tree
Hide file tree
Showing 12 changed files with 844 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +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/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
24 changes: 12 additions & 12 deletions exporter/awsemfexporter/metric_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1456,22 +1456,22 @@ func TestGroupedMetricToCWMeasurementsWithFilters(t *testing.T) {
MetricNameSelectors: []string{"metric(1|3)"},
},
}, []cWMeasurement{
{
Namespace: namespace,
Dimensions: [][]string{{}},
Metrics: []map[string]string{
{
"Name": "metric1",
"Unit": "Count",
},
{
"Name": "metric3",
"Unit": "Seconds",
{
Namespace: namespace,
Dimensions: [][]string{{}},
Metrics: []map[string]string{
{
"Name": "metric1",
"Unit": "Count",
},
{
"Name": "metric3",
"Unit": "Seconds",
},
},
},
},
},
},
{
"label matchers",
[]*MetricDeclaration{
Expand Down
1 change: 1 addition & 0 deletions extension/awsmiddleware/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
29 changes: 29 additions & 0 deletions extension/awsmiddleware/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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.

The `awsmiddleware.Extension` interface extends `component.Extension` by adding the following methods:
```
RequestHandlers() []RequestHandler
ResponseHandlers() []ResponseHandler
```

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

The `awsmiddleware.ResponseHandler` interface contains the following methods:
```
ID() string
Position() HandlerPosition
HandleResponse(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.
27 changes: 27 additions & 0 deletions extension/awsmiddleware/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package awsmiddleware // import "github.com/open-telemetry/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"`
}

// 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
}
return nil, errNotMiddleware
}
return nil, fmt.Errorf("failed to resolve AWS client handler %q: %w", c.MiddlewareID, errNotFound)
}
67 changes: 67 additions & 0 deletions extension/awsmiddleware/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package awsmiddleware

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"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) {
id := component.NewID("test")
cfg := &Config{MiddlewareID: id}
nopExtension, err := extensiontest.NewNopBuilder().Create(context.Background(), extensiontest.NewNopCreateSettings())
require.Error(t, err)
testCases := map[string]struct {
extensions map[component.ID]component.Component
wantErr error
}{
"WithNoExtensions": {
extensions: map[component.ID]component.Component{},
wantErr: errNotFound,
},
"WithNonMiddlewareExtension": {
extensions: map[component.ID]component.Component{id: nopExtension},
wantErr: errNotMiddleware,
},
"WithMiddlewareExtension": {
extensions: map[component.ID]component.Component{id: &testMiddlewareExtension{}},
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
got, err := cfg.GetMiddleware(testCase.extensions)
if testCase.wantErr != nil {
assert.Error(t, err)
assert.ErrorIs(t, err, testCase.wantErr)
assert.Nil(t, got)
} else {
assert.NoError(t, err)
assert.NotNil(t, got)
}
})
}
}
6 changes: 6 additions & 0 deletions extension/awsmiddleware/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// 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"
51 changes: 51 additions & 0 deletions extension/awsmiddleware/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module github.com/open-telemetry/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-v2/service/s3 v1.40.0
github.com/aws/smithy-go v1.15.0
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/collector/component v0.87.0
go.opentelemetry.io/collector/extension v0.87.0
)

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/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/s3shared v1.15.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/knadh/koanf/v2 v2.0.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
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
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.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/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 607b652

Please sign in to comment.