-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add agenthealth extension with user agent handler. (#919)
- Loading branch information
Showing
19 changed files
with
775 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package envconfig | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIsUsageDataEnabled(t *testing.T) { | ||
assert.True(t, getUsageDataEnabled()) | ||
|
||
t.Setenv(CWAGENT_USAGE_DATA, "TRUE") | ||
assert.True(t, getUsageDataEnabled()) | ||
|
||
t.Setenv(CWAGENT_USAGE_DATA, "INVALID") | ||
assert.True(t, getUsageDataEnabled()) | ||
|
||
t.Setenv(CWAGENT_USAGE_DATA, "FALSE") | ||
assert.False(t, getUsageDataEnabled()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package agenthealth | ||
|
||
import "go.opentelemetry.io/collector/component" | ||
|
||
type Config struct { | ||
IsUsageDataEnabled bool `mapstructure:"is_usage_data_enabled"` | ||
} | ||
|
||
var _ component.Config = (*Config)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package agenthealth | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/confmap/confmaptest" | ||
) | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
testCases := []struct { | ||
id component.ID | ||
want component.Config | ||
}{ | ||
{ | ||
id: component.NewID(TypeStr), | ||
want: NewFactory().CreateDefaultConfig(), | ||
}, | ||
{ | ||
id: component.NewIDWithName(TypeStr, "1"), | ||
want: &Config{IsUsageDataEnabled: false}, | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
conf, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) | ||
require.NoError(t, err) | ||
cfg := NewFactory().CreateDefaultConfig() | ||
sub, err := conf.Sub(testCase.id.String()) | ||
require.NoError(t, err) | ||
require.NoError(t, component.UnmarshalConfig(sub, cfg)) | ||
|
||
assert.NoError(t, component.ValidateConfig(cfg)) | ||
assert.Equal(t, testCase.want, cfg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package agenthealth | ||
|
||
import ( | ||
"github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware" | ||
"go.opentelemetry.io/collector/component" | ||
"go.uber.org/zap" | ||
|
||
"github.com/aws/amazon-cloudwatch-agent/extension/agenthealth/handler/useragent" | ||
) | ||
|
||
type agentHealth struct { | ||
logger *zap.Logger | ||
cfg *Config | ||
component.StartFunc | ||
component.ShutdownFunc | ||
} | ||
|
||
var _ awsmiddleware.Extension = (*agentHealth)(nil) | ||
|
||
func (ah *agentHealth) Handlers() ([]awsmiddleware.RequestHandler, []awsmiddleware.ResponseHandler) { | ||
var responseHandlers []awsmiddleware.ResponseHandler | ||
requestHandlers := []awsmiddleware.RequestHandler{useragent.NewHandler(ah.cfg.IsUsageDataEnabled)} | ||
return requestHandlers, responseHandlers | ||
} | ||
|
||
func newAgentHealth(logger *zap.Logger, cfg *Config) (*agentHealth, error) { | ||
return &agentHealth{logger: logger, cfg: cfg}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package agenthealth | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestExtension(t *testing.T) { | ||
ctx := context.Background() | ||
extension, err := newAgentHealth(zap.NewNop(), &Config{IsUsageDataEnabled: true}) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, extension) | ||
assert.NoError(t, extension.Start(ctx, componenttest.NewNopHost())) | ||
requests, responses := extension.Handlers() | ||
assert.Len(t, requests, 1) | ||
assert.Len(t, responses, 0) | ||
extension.cfg.IsUsageDataEnabled = false | ||
extension.Handlers() | ||
requests, responses = extension.Handlers() | ||
assert.Len(t, requests, 1) | ||
assert.Len(t, responses, 0) | ||
assert.NoError(t, extension.Shutdown(ctx)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package agenthealth | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/extension" | ||
) | ||
|
||
const ( | ||
TypeStr = "agenthealth" | ||
) | ||
|
||
func NewFactory() extension.Factory { | ||
return extension.NewFactory( | ||
TypeStr, | ||
createDefaultConfig, | ||
createExtension, | ||
component.StabilityLevelAlpha, | ||
) | ||
} | ||
|
||
func createDefaultConfig() component.Config { | ||
return &Config{ | ||
IsUsageDataEnabled: true, | ||
} | ||
} | ||
|
||
func createExtension(_ context.Context, settings extension.CreateSettings, cfg component.Config) (extension.Extension, error) { | ||
return newAgentHealth(settings.Logger, cfg.(*Config)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package agenthealth | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.opentelemetry.io/collector/extension/extensiontest" | ||
) | ||
|
||
func TestCreateDefaultConfig(t *testing.T) { | ||
cfg := NewFactory().CreateDefaultConfig() | ||
assert.Equal(t, &Config{IsUsageDataEnabled: true}, cfg) | ||
assert.NoError(t, componenttest.CheckConfigStruct(cfg)) | ||
} | ||
|
||
func TestCreateExtension(t *testing.T) { | ||
cfg := &Config{} | ||
got, err := NewFactory().CreateExtension(context.Background(), extensiontest.NewNopCreateSettings(), cfg) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, got) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package useragent | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware" | ||
"go.uber.org/atomic" | ||
) | ||
|
||
type userAgentHandler struct { | ||
userAgent UserAgent | ||
isUsageDataEnabled bool | ||
header *atomic.String | ||
} | ||
|
||
var _ awsmiddleware.RequestHandler = (*userAgentHandler)(nil) | ||
|
||
func (uah *userAgentHandler) ID() string { | ||
return handlerID | ||
} | ||
|
||
func (uah *userAgentHandler) Position() awsmiddleware.HandlerPosition { | ||
return awsmiddleware.After | ||
} | ||
|
||
// HandleRequest prepends the User-Agent header with the CloudWatch Agent's | ||
// user agent string. | ||
func (uah *userAgentHandler) HandleRequest(_ context.Context, r *http.Request) { | ||
newHeader := uah.Header() | ||
current := r.Header.Get(headerKeyUserAgent) | ||
if current != "" { | ||
newHeader += separator + current | ||
} | ||
r.Header.Set(headerKeyUserAgent, newHeader) | ||
} | ||
|
||
func (uah *userAgentHandler) Header() string { | ||
return uah.header.Load() | ||
} | ||
|
||
func (uah *userAgentHandler) refreshHeader() { | ||
uah.header.Store(uah.userAgent.Header(uah.isUsageDataEnabled)) | ||
} | ||
|
||
func newHandler(userAgent UserAgent, isUsageDataEnabled bool) *userAgentHandler { | ||
handler := &userAgentHandler{ | ||
userAgent: userAgent, | ||
header: &atomic.String{}, | ||
isUsageDataEnabled: isUsageDataEnabled, | ||
} | ||
handler.refreshHeader() | ||
userAgent.Listen(handler.refreshHeader) | ||
return handler | ||
} | ||
|
||
func NewHandler(isUsageDataEnabled bool) awsmiddleware.RequestHandler { | ||
return newHandler(Get(), isUsageDataEnabled) | ||
} |
Oops, something went wrong.