Skip to content

Commit

Permalink
Do not assume "aws" partition in ecs detector. Fixes open-telemetry#3166
Browse files Browse the repository at this point in the history
  • Loading branch information
ahobson authored Feb 23, 2023
1 parent d296842 commit e915b7c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Fixed

- Remove use of deprecated `"math/rand".Seed` in `go.opentelemetry.io/contrib/instrumentation/github.com/Shopify/sarama/otelsarama/example/producer`. (#3396)
- `detectors/aws/ecs`: Do not assume "aws" partition in ecs detector to prevent panic in AWS GovCloud. (#3167)

## [1.14.0/0.39.0/0.8.0] - 2023-02-07

Expand Down
28 changes: 21 additions & 7 deletions detectors/aws/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"net/http"
"os"
"regexp"
"runtime"
"strings"

Expand Down Expand Up @@ -156,20 +155,35 @@ func (detector *resourceDetector) getLogsAttributes(metadata *ecsmetadata.Contai
}

containerArn := metadata.ContainerARN
// https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html
const arnPartition = 1
const arnRegion = 3
const arnAccountId = 4
containerArnParts := strings.Split(containerArn, ":")
// a valid arn should have at least 6 parts
if len(containerArnParts) < 6 {
return nil, errCannotRetrieveLogsStreamMetadataV4
}
logsRegion := logsOptions.AwsRegion
if len(logsRegion) < 1 {
r := regexp.MustCompile(`arn:aws:ecs:([^:]+):.*`)
logsRegion = r.FindStringSubmatch(containerArn)[1]
logsRegion = containerArnParts[arnRegion]
}

r := regexp.MustCompile(`arn:aws:ecs:[^:]+:([^:]+):.*`)
awsAccount := r.FindStringSubmatch(containerArn)[1]
awsPartition := containerArnParts[arnPartition]
awsAccount := containerArnParts[arnAccountId]

awsLogGroupArn := strings.Join([]string{"arn", awsPartition, "logs",
logsRegion, awsAccount, "log-group", logsOptions.AwsLogsGroup,
"*"}, ":")
awsLogStreamArn := strings.Join([]string{"arn", awsPartition, "logs",
logsRegion, awsAccount, "log-group", logsOptions.AwsLogsGroup,
"log-stream", logsOptions.AwsLogsStream}, ":")

return []attribute.KeyValue{
semconv.AWSLogGroupNames(logsOptions.AwsLogsGroup),
semconv.AWSLogGroupARNs(fmt.Sprintf("arn:aws:logs:%s:%s:log-group:%s:*", logsRegion, awsAccount, logsOptions.AwsLogsGroup)),
semconv.AWSLogGroupARNs(awsLogGroupArn),
semconv.AWSLogStreamNames(logsOptions.AwsLogsStream),
semconv.AWSLogStreamARNs(fmt.Sprintf("arn:aws:logs:%s:%s:log-group:%s:log-stream:%s", logsRegion, awsAccount, logsOptions.AwsLogsGroup, logsOptions.AwsLogsStream)),
semconv.AWSLogStreamARNs(awsLogStreamArn),
}, nil
}

Expand Down
33 changes: 33 additions & 0 deletions detectors/aws/ecs/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"

metadata "github.com/brunoscheufler/aws-ecs-metadata-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
Expand Down Expand Up @@ -116,3 +117,35 @@ func TestReturnsIfNoEnvVars(t *testing.T) {
assert.NoError(t, err, "failure to detect when not on platform must not be an error")
assert.Nil(t, res, "failure to detect should return a nil Resource to optimize merge")
}

// handles alternative aws partitions (e.g. AWS GovCloud).
func TestLogsAttributesAlternatePartition(t *testing.T) {
os.Clearenv()
detector := &resourceDetector{utils: nil}

containerMetadata := &metadata.ContainerMetadataV4{
LogDriver: "awslogs",
LogOptions: struct {
AwsLogsCreateGroup string `json:"awslogs-create-group"`
AwsLogsGroup string `json:"awslogs-group"`
AwsLogsStream string `json:"awslogs-stream"`
AwsRegion string `json:"awslogs-region"`
}{
"fake-create",
"fake-group",
"fake-stream",
"",
},
ContainerARN: "arn:arn-partition:arn-svc:arn-region:arn-account:arn-resource",
}
actualAttributes, err := detector.getLogsAttributes(containerMetadata)
assert.NoError(t, err, "failure with nonstandard partitition")

expectedAttributes := []attribute.KeyValue{
semconv.AWSLogGroupNames(containerMetadata.LogOptions.AwsLogsGroup),
semconv.AWSLogGroupARNs("arn:arn-partition:logs:arn-region:arn-account:log-group:fake-group:*"),
semconv.AWSLogStreamNames(containerMetadata.LogOptions.AwsLogsStream),
semconv.AWSLogStreamARNs("arn:arn-partition:logs:arn-region:arn-account:log-group:fake-group:log-stream:fake-stream"),
}
assert.Equal(t, expectedAttributes, actualAttributes, "logs attributes are incorrect")
}

0 comments on commit e915b7c

Please sign in to comment.