Skip to content

Commit

Permalink
Fix X-Ray exporter CA bundle configuration (#986)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefchien authored Dec 7, 2023
1 parent 826a43e commit 17c66e8
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 128 deletions.
13 changes: 8 additions & 5 deletions translator/translate/otel/exporter/awsemf/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
prometheusBasePathKey = common.ConfigKey(common.LogsKey, common.MetricsCollectedKey, common.PrometheusKey)
emfProcessorBasePathKey = common.ConfigKey(prometheusBasePathKey, common.EMFProcessorKey)
endpointOverrideKey = common.ConfigKey(common.LogsKey, common.EndpointOverrideKey)
roleARNPathKey = common.ConfigKey(common.LogsKey, common.CredentialsKey, common.RoleARNKey)
)

type translator struct {
Expand Down Expand Up @@ -95,20 +96,22 @@ func (t *translator) Translate(c *confmap.Conf) (component.Config, error) {
return nil, fmt.Errorf("unable to unmarshal config: %w", err)
}
}
cfg.AWSSessionSettings.Region = agent.Global_Config.Region
cfg.AWSSessionSettings.CertificateFilePath = os.Getenv(envconfig.AWS_CA_BUNDLE)
if c.IsSet(endpointOverrideKey) {
cfg.AWSSessionSettings.Endpoint, _ = common.GetString(c, endpointOverrideKey)
}
cfg.AWSSessionSettings.CertificateFilePath = os.Getenv(envconfig.AWS_CA_BUNDLE)
cfg.AWSSessionSettings.Region = agent.Global_Config.Region
cfg.AWSSessionSettings.IMDSRetries = retryer.GetDefaultRetryNumber()
if profileKey, ok := agent.Global_Config.Credentials[agent.Profile_Key]; ok {
cfg.AWSSessionSettings.Profile = fmt.Sprintf("%v", profileKey)
}
cfg.AWSSessionSettings.Region = agent.Global_Config.Region
cfg.AWSSessionSettings.RoleARN = agent.Global_Config.Role_arn
if c.IsSet(roleARNPathKey) {
cfg.AWSSessionSettings.RoleARN, _ = common.GetString(c, roleARNPathKey)
}
if credentialsFileKey, ok := agent.Global_Config.Credentials[agent.CredentialsFile_Key]; ok {
cfg.AWSSessionSettings.SharedCredentialsFile = []string{fmt.Sprintf("%v", credentialsFileKey)}
}
cfg.AWSSessionSettings.RoleARN = agent.Global_Config.Role_arn
cfg.AWSSessionSettings.IMDSRetries = retryer.GetDefaultRetryNumber()

if isEcs(c) {
if err := setEcsFields(c, cfg); err != nil {
Expand Down
129 changes: 66 additions & 63 deletions translator/translate/otel/exporter/awsemf/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"

"github.com/aws/amazon-cloudwatch-agent/cfg/envconfig"
"github.com/aws/amazon-cloudwatch-agent/internal/util/testutil"
legacytranslator "github.com/aws/amazon-cloudwatch-agent/translator"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/agent"
Expand All @@ -24,25 +25,26 @@ var nilSlice []string
var nilMetricDescriptorsSlice []awsemfexporter.MetricDescriptor

func TestTranslator(t *testing.T) {
tt := NewTranslator()
t.Setenv(envconfig.AWS_CA_BUNDLE, "/ca/bundle")
agent.Global_Config.Region = "us-east-1"
agent.Global_Config.Role_arn = "global_arn"
tt := NewTranslator()
require.EqualValues(t, "awsemf", tt.ID().String())
testCases := map[string]struct {
env map[string]string
input map[string]interface{}
want map[string]interface{} // Can't construct & use awsemfexporter.Config as it uses internal only types
input map[string]any
want map[string]any // Can't construct & use awsemfexporter.Config as it uses internal only types
wantErr error
}{
"GenerateAwsEmfExporterConfigEcs": {
input: map[string]interface{}{
"logs": map[string]interface{}{
"metrics_collected": map[string]interface{}{
"ecs": map[string]interface{}{},
input: map[string]any{
"logs": map[string]any{
"metrics_collected": map[string]any{
"ecs": map[string]any{},
},
},
},
want: map[string]interface{}{
want: map[string]any{
"namespace": "ECS/ContainerInsights",
"log_group_name": "/aws/ecs/containerinsights/{ClusterName}/performance",
"log_stream_name": "NodeTelemetry-{ContainerInstanceId}",
Expand Down Expand Up @@ -74,16 +76,16 @@ func TestTranslator(t *testing.T) {
},
},
"GenerateAwsEmfExporterConfigEcsDisableMetricExtraction": {
input: map[string]interface{}{
"logs": map[string]interface{}{
"metrics_collected": map[string]interface{}{
"ecs": map[string]interface{}{
input: map[string]any{
"logs": map[string]any{
"metrics_collected": map[string]any{
"ecs": map[string]any{
"disable_metric_extraction": true,
},
},
},
},
want: map[string]interface{}{
want: map[string]any{
"namespace": "ECS/ContainerInsights",
"log_group_name": "/aws/ecs/containerinsights/{ClusterName}/performance",
"log_stream_name": "NodeTelemetry-{ContainerInstanceId}",
Expand Down Expand Up @@ -115,14 +117,14 @@ func TestTranslator(t *testing.T) {
},
},
"GenerateAwsEmfExporterConfigKubernetes": {
input: map[string]interface{}{
"logs": map[string]interface{}{
"metrics_collected": map[string]interface{}{
"kubernetes": map[string]interface{}{},
input: map[string]any{
"logs": map[string]any{
"metrics_collected": map[string]any{
"kubernetes": map[string]any{},
},
},
},
want: map[string]interface{}{
want: map[string]any{
"namespace": "ContainerInsights",
"log_group_name": "/aws/containerinsights/{ClusterName}/performance",
"log_stream_name": "{NodeName}",
Expand Down Expand Up @@ -181,16 +183,16 @@ func TestTranslator(t *testing.T) {
},
},
"GenerateAwsEmfExporterConfigKubernetesDisableMetricExtraction": {
input: map[string]interface{}{
"logs": map[string]interface{}{
"metrics_collected": map[string]interface{}{
"kubernetes": map[string]interface{}{
input: map[string]any{
"logs": map[string]any{
"metrics_collected": map[string]any{
"kubernetes": map[string]any{
"disable_metric_extraction": true,
},
},
},
},
want: map[string]interface{}{
want: map[string]any{
"namespace": "ContainerInsights",
"log_group_name": "/aws/containerinsights/{ClusterName}/performance",
"log_stream_name": "{NodeName}",
Expand Down Expand Up @@ -249,16 +251,16 @@ func TestTranslator(t *testing.T) {
},
},
"GenerateAwsEmfExporterConfigKubernetesWithEnableFullPodAndContainerMetrics": {
input: map[string]interface{}{
"logs": map[string]interface{}{
"metrics_collected": map[string]interface{}{
"kubernetes": map[string]interface{}{
input: map[string]any{
"logs": map[string]any{
"metrics_collected": map[string]any{
"kubernetes": map[string]any{
"enhanced_container_insights": true,
},
},
},
},
want: map[string]interface{}{
want: map[string]any{
"namespace": "ContainerInsights",
"log_group_name": "/aws/containerinsights/{ClusterName}/performance",
"log_stream_name": "{NodeName}",
Expand Down Expand Up @@ -497,30 +499,30 @@ func TestTranslator(t *testing.T) {
},
},
"GenerateAwsEmfExporterConfigPrometheus": {
input: map[string]interface{}{
"logs": map[string]interface{}{
"metrics_collected": map[string]interface{}{
"prometheus": map[string]interface{}{
input: map[string]any{
"logs": map[string]any{
"metrics_collected": map[string]any{
"prometheus": map[string]any{
"log_group_name": "/test/log/group",
"log_stream_name": "{LogStreamName}",
"emf_processor": map[string]interface{}{
"metric_declaration": []interface{}{
map[string]interface{}{
"emf_processor": map[string]any{
"metric_declaration": []any{
map[string]any{
"source_labels": []string{"Service", "Namespace"},
"label_matcher": "(.*node-exporter.*|.*kube-dns.*);kube-system$",
"dimensions": [][]string{{"Service", "Namespace"}},
"metric_selectors": []string{"^coredns_dns_request_type_count_total$"},
},
},
"metric_unit": map[string]interface{}{
"metric_unit": map[string]any{
"jvm_gc_collection_seconds_sum": "Milliseconds",
},
},
},
},
},
},
want: map[string]interface{}{
want: map[string]any{
"namespace": "CWAgent/Prometheus",
"log_group_name": "/test/log/group",
"log_stream_name": "{JobName}",
Expand Down Expand Up @@ -554,18 +556,18 @@ func TestTranslator(t *testing.T) {
},
},
"GenerateAwsEmfExporterConfigPrometheusDisableMetricExtraction": {
input: map[string]interface{}{
"logs": map[string]interface{}{
"metrics_collected": map[string]interface{}{
"prometheus": map[string]interface{}{
input: map[string]any{
"logs": map[string]any{
"metrics_collected": map[string]any{
"prometheus": map[string]any{
"disable_metric_extraction": true,
"log_group_name": "/test/log/group",
"log_stream_name": "{JobName}",
},
},
},
},
want: map[string]interface{}{
want: map[string]any{
"namespace": "",
"log_group_name": "/test/log/group",
"log_stream_name": "{JobName}",
Expand All @@ -587,22 +589,22 @@ func TestTranslator(t *testing.T) {
},
},
"GenerateAwsEmfExporterConfigPrometheusNoDeclarations": {
input: map[string]interface{}{
"logs": map[string]interface{}{
"metrics_collected": map[string]interface{}{
"prometheus": map[string]interface{}{
input: map[string]any{
"logs": map[string]any{
"metrics_collected": map[string]any{
"prometheus": map[string]any{
"log_group_name": "/test/log/group",
"log_stream_name": "{JobName}",
"emf_processor": map[string]interface{}{
"metric_unit": map[string]interface{}{
"emf_processor": map[string]any{
"metric_unit": map[string]any{
"jvm_gc_collection_seconds_sum": "Milliseconds",
},
},
},
},
},
},
want: map[string]interface{}{
want: map[string]any{
"namespace": "CWAgent/Prometheus",
"log_group_name": "/test/log/group",
"log_stream_name": "{JobName}",
Expand All @@ -629,17 +631,17 @@ func TestTranslator(t *testing.T) {
},
},
"GenerateAwsEmfExporterConfigPrometheusNoEmfProcessor": {
input: map[string]interface{}{
"logs": map[string]interface{}{
"metrics_collected": map[string]interface{}{
"prometheus": map[string]interface{}{
input: map[string]any{
"logs": map[string]any{
"metrics_collected": map[string]any{
"prometheus": map[string]any{
"log_group_name": "/test/log/group",
"log_stream_name": "{JobName}",
},
},
},
},
want: map[string]interface{}{
want: map[string]any{
"namespace": "",
"log_group_name": "/test/log/group",
"log_stream_name": "{JobName}",
Expand Down Expand Up @@ -683,6 +685,7 @@ func TestTranslator(t *testing.T) {
assert.Equal(t, testCase.want["resource_to_telemetry_conversion"], gotCfg.ResourceToTelemetrySettings)
assert.ElementsMatch(t, testCase.want["metric_declarations"], gotCfg.MetricDeclarations)
assert.ElementsMatch(t, testCase.want["metric_descriptors"], gotCfg.MetricDescriptors)
assert.Equal(t, "/ca/bundle", gotCfg.CertificateFilePath)
assert.Equal(t, "global_arn", gotCfg.RoleARN)
assert.Equal(t, "us-east-1", gotCfg.Region)
assert.NotNil(t, gotCfg.MiddlewareID)
Expand All @@ -695,26 +698,26 @@ func TestTranslator(t *testing.T) {
func TestTranslateAppSignals(t *testing.T) {
tt := NewTranslatorWithName(common.AppSignals)
testCases := map[string]struct {
input map[string]interface{}
input map[string]any
want *confmap.Conf
wantErr error
isKubernetes bool
}{
"WithAppSignalsEnabledEKS": {
input: map[string]interface{}{
"logs": map[string]interface{}{
"metrics_collected": map[string]interface{}{
"app_signals": map[string]interface{}{},
input: map[string]any{
"logs": map[string]any{
"metrics_collected": map[string]any{
"app_signals": map[string]any{},
},
}},
want: testutil.GetConf(t, filepath.Join("appsignals_config_eks.yaml")),
isKubernetes: true,
},
"WithAppSignalsEnabledGeneric": {
input: map[string]interface{}{
"logs": map[string]interface{}{
"metrics_collected": map[string]interface{}{
"app_signals": map[string]interface{}{},
input: map[string]any{
"logs": map[string]any{
"metrics_collected": map[string]any{
"app_signals": map[string]any{},
},
}},
want: testutil.GetConf(t, filepath.Join("appsignals_config_generic.yaml")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ endpoint: https://x-ray-endpoint.us-west-2.amazonaws.com
region: us-west-2
imds_retries: 1
proxy_address: https://proxy.proxy.com
certificate_file_path: /ca/bundle
telemetry:
enabled: true
include_metadata: true
Expand Down
Loading

0 comments on commit 17c66e8

Please sign in to comment.