Skip to content

Commit

Permalink
Merge branch 'amazon-contributing:aws-cwa-dev' into aws-cwa-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
mitali-salvi authored Sep 18, 2023
2 parents 019746a + ced73ba commit e8cf38e
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 75 deletions.
44 changes: 22 additions & 22 deletions exporter/awsemfexporter/README.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions exporter/awsemfexporter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var patternKeyToAttributeMap = map[string]string{
"ServiceName": "service.name",
"ContainerInstanceId": "aws.ecs.container.instance.id",
"TaskDefinitionFamily": "aws.ecs.task.family",
"JobName": "job",
}

func replacePatterns(s string, attrMap map[string]string, logger *zap.Logger) (string, bool) {
Expand Down
23 changes: 23 additions & 0 deletions exporter/awsemfexporter/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ func TestReplacePatternValidServiceName(t *testing.T) {
assert.True(t, success)
}

func TestReplacePatternValidJobName(t *testing.T) {
logger := zap.NewNop()

input := "{JobName}"

attrMap := pcommon.NewMap()
attrMap.PutStr("job", "some-test-log")

s, success := replacePatterns(input, attrMaptoStringMap(attrMap), logger)

assert.Equal(t, "some-test-log", s)
assert.True(t, success)

attrMap = pcommon.NewMap()
attrMap.PutStr("JobName", "takes-precedence")
attrMap.PutStr("job", "some-test-log")

s, success = replacePatterns(input, attrMaptoStringMap(attrMap), logger)

assert.Equal(t, "takes-precedence", s)
assert.True(t, success)
}

func TestReplacePatternValidClusterName(t *testing.T) {
logger := zap.NewNop()

Expand Down
34 changes: 31 additions & 3 deletions internal/aws/awsutil/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/endpoints"
Expand Down Expand Up @@ -372,16 +373,43 @@ func getSTSRegionalEndpoint(r string) string {
}

func GetDefaultSession(logger *zap.Logger, cfg *AWSSessionSettings) (*session.Session, error) {
awsConfig := &aws.Config{
cfgFiles := getFallbackSharedConfigFiles(backwardsCompatibleUserHomeDir)
logger.Debug("Fallback shared config file(s)", zap.Strings("files", cfgFiles))
awsConfig := aws.Config{
Credentials: getRootCredentials(cfg),
}
result, serr := session.NewSession(awsConfig)
result, serr := session.NewSessionWithOptions(session.Options{
Config: awsConfig,
SharedConfigFiles: cfgFiles,
})
if serr != nil {
logger.Error("Error in creating session object waiting 15 seconds", zap.Error(serr))
time.Sleep(15 * time.Second)
result, serr = session.NewSession(awsConfig)
result, serr = session.NewSessionWithOptions(session.Options{
Config: awsConfig,
SharedConfigFiles: cfgFiles,
})
if serr != nil {
logger.Error("Retry failed for creating credential sessions", zap.Error(serr))
return result, serr
}
}
cred, err := result.Config.Credentials.Get()
if err != nil {
logger.Error("Failed to get credential from session", zap.Error(err))
} else {
logger.Debug("Using credential from session", zap.String("access-key", cred.AccessKeyID), zap.String("provider", cred.ProviderName))
}
if cred.ProviderName == ec2rolecreds.ProviderName {
var found []string
cfgFiles = getFallbackSharedConfigFiles(currentUserHomeDir)
for _, cfgFile := range cfgFiles {
if _, err = os.Stat(cfgFile); err == nil {
found = append(found, cfgFile)
}
}
if len(found) > 0 {
logger.Warn("Unused shared config file(s) found.", zap.Strings("files", found))
}
}
return result, serr
Expand Down
95 changes: 95 additions & 0 deletions internal/aws/awsutil/shared_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package awsutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil"

import (
"os"
"os/user"
"path/filepath"
"strconv"
)

const (
envAwsSdkLoadConfig = "AWS_SDK_LOAD_CONFIG"
// nolint:gosec
envAwsSharedCredentialsFile = "AWS_SHARED_CREDENTIALS_FILE"
envAwsSharedConfigFile = "AWS_CONFIG_FILE"
)

// getFallbackSharedConfigFiles follows the same logic as the AWS SDK but takes a getUserHomeDir
// function.
func getFallbackSharedConfigFiles(userHomeDirProvider func() string) []string {
var sharedCredentialsFile, sharedConfigFile string
setFromEnvVal(&sharedCredentialsFile, envAwsSharedCredentialsFile)
setFromEnvVal(&sharedConfigFile, envAwsSharedConfigFile)
if sharedCredentialsFile == "" {
sharedCredentialsFile = defaultSharedCredentialsFile(userHomeDirProvider())
}
if sharedConfigFile == "" {
sharedConfigFile = defaultSharedConfig(userHomeDirProvider())
}
var cfgFiles []string
enableSharedConfig, _ := strconv.ParseBool(os.Getenv(envAwsSdkLoadConfig))
if enableSharedConfig {
cfgFiles = append(cfgFiles, sharedConfigFile)
}
return append(cfgFiles, sharedCredentialsFile)
}

func setFromEnvVal(dst *string, keys ...string) {
for _, k := range keys {
if v := os.Getenv(k); len(v) != 0 {
*dst = v
break
}
}
}

func defaultSharedCredentialsFile(dir string) string {
return filepath.Join(dir, ".aws", "credentials")
}

func defaultSharedConfig(dir string) string {
return filepath.Join(dir, ".aws", "config")
}

// backwardsCompatibleUserHomeDir provides the home directory based on
// environment variables.
//
// Based on v1.44.106 of the AWS SDK.
func backwardsCompatibleUserHomeDir() string {
home, _ := os.UserHomeDir()
return home
}

// currentUserHomeDir attempts to use the environment variables before falling
// back on the current user's home directory.
//
// Based on v1.44.332 of the AWS SDK.
func currentUserHomeDir() string {
var home string

home = backwardsCompatibleUserHomeDir()
if len(home) > 0 {
return home
}

currUser, _ := user.Current()
if currUser != nil {
home = currUser.HomeDir
}

return home
}
42 changes: 42 additions & 0 deletions internal/aws/awsutil/shared_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package awsutil

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetFallbackSharedConfigFiles(t *testing.T) {
noOpGetUserHomeDir := func() string { return "home" }
t.Setenv(envAwsSdkLoadConfig, "true")
t.Setenv(envAwsSharedCredentialsFile, "credentials")
t.Setenv(envAwsSharedConfigFile, "config")

got := getFallbackSharedConfigFiles(noOpGetUserHomeDir)
assert.Equal(t, []string{"config", "credentials"}, got)

t.Setenv(envAwsSdkLoadConfig, "false")
got = getFallbackSharedConfigFiles(noOpGetUserHomeDir)
assert.Equal(t, []string{"credentials"}, got)

t.Setenv(envAwsSdkLoadConfig, "true")
t.Setenv(envAwsSharedCredentialsFile, "")
t.Setenv(envAwsSharedConfigFile, "")

got = getFallbackSharedConfigFiles(noOpGetUserHomeDir)
assert.Equal(t, []string{defaultSharedConfig("home"), defaultSharedCredentialsFile("home")}, got)
}
63 changes: 19 additions & 44 deletions receiver/awscontainerinsightreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,17 +345,8 @@ kubectl apply -f config.yaml
| cluster_failed_node_count | Count |
| cluster_node_count | Count |
| cluster_number_of_running_pods | Count |
| apiserver_storage_objects | Count |
| apiserver_request_total | Count |
| apiserver_request_duration_seconds | Seconds |
| apiserver_admission_controller_admission_duration_seconds | Seconds |
| rest_client_request_duration_seconds | Seconds |
| rest_client_requests_total | Count |
| etcd_request_duration_seconds | Seconds |



<br/><br/>
<br/><br/>
| Resource Attribute |
|--------------------|
| ClusterName |
Expand All @@ -365,6 +356,24 @@ kubectl apply -f config.yaml
| Version |
| Sources |

<br/><br/>
<br/><br/>

### Cluster ControlPlane
| Metric | Unit |
|-----------------------------------------------------------|---------|
| apiserver_admission_controller_admission_duration_seconds | Seconds |
| apiserver_flowcontrol_rejected_requests_total | Count |
| apiserver_flowcontrol_request_concurrency_limit | Count |
| apiserver_request_duration_seconds | Seconds |
| apiserver_request_total | Count |
| apiserver_storage_list_duration_seconds | Seconds |
| apiserver_storage_objects | Count |
| etcd_db_total_size_in_bytes | Count |
| etcd_request_duration_seconds | Seconds |
| rest_client_request_duration_seconds | Seconds |
| rest_client_requests_total | Count |

<br/><br/>
<br/><br/>

Expand Down Expand Up @@ -407,41 +416,7 @@ kubectl apply -f config.yaml
| Sources |
| kubernete |


<br/><br/>

### Cluster Endpoint
| Metric | Unit |
|-----------------------------|-------|
| etcd_db_total_size_in_bytes | bytes |

<br/><br/>
| Resource Attribute |
|--------------------|
| ClusterName |
| NodeName |
| Endpoint |
| Type |
| Timestamp |
| Version |
| Sources |

<br/><br/>

### Cluster Resources
| Metric | Unit |
|-----------------------------------------|---------|
| apiserver_storage_list_duration_seconds | Seconds |

<br/><br/>
| Resource Attribute |
|--------------------|
| ClusterName |
| NodeName |
| Type |
| Timestamp |
| Version |
| Sources |

### Cluster Deployment
| Metric | Unit |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ const (

var (
controlPlaneMetricAllowList = []string{
"apiserver_storage_objects",
"apiserver_request_total",
"apiserver_request_duration_seconds.*",
"apiserver_admission_controller_admission_duration_seconds.*",
"apiserver_flowcontrol_rejected_requests_total",
"apiserver_flowcontrol_request_concurrency_limit",
"apiserver_request_duration_seconds.*",
"apiserver_request_total",
"apiserver_storage_list_duration_seconds.*",
"apiserver_storage_objects",
"etcd_db_total_size_in_bytes.*",
"etcd_request_duration_seconds.*",
"rest_client_request_duration_seconds.*",
"rest_client_requests_total",
"etcd_request_duration_seconds.*",
"etcd_db_total_size_in_bytes.*",
"apiserver_storage_list_duration_seconds.*",
}
)

Expand Down

0 comments on commit e8cf38e

Please sign in to comment.