Skip to content

Commit

Permalink
Add standard tags to ECS logs (#22)
Browse files Browse the repository at this point in the history
* add application_id tag

* rest of the changes

* Remove duplicate service.name

* fix tests

* comments

* fix test

* rename applicaiton_id env var
  • Loading branch information
Lulu413 authored May 28, 2024
1 parent 7c404f1 commit df58fc3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
40 changes: 23 additions & 17 deletions enricher/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
)

type Enricher struct {
canvaAWSAccount string
canvaAppName string
logGroup string
ecsTaskFamily string
ecsTaskRevision int
canvaAWSAccount string
canvaApplicationId string
canvaAppName string
canvaComponent string
logGroup string
ecsTaskFamily string
ecsTaskRevision int
}

var _ enricher.IEnricher = (*Enricher)(nil)
Expand Down Expand Up @@ -44,24 +46,28 @@ func NewEnricher() *Enricher {
}

return &Enricher{
canvaAWSAccount: os.Getenv("CANVA_AWS_ACCOUNT"),
canvaAppName: os.Getenv("CANVA_APP_NAME"),
logGroup: os.Getenv("LOG_GROUP"),
ecsTaskFamily: ecsTaskFamily,
ecsTaskRevision: ecsTaskRevision,
canvaAWSAccount: os.Getenv("CANVA_AWS_ACCOUNT"),
canvaApplicationId: os.Getenv("CANVA_APPLICATION_ID"),
canvaAppName: os.Getenv("CANVA_APP_NAME"),
canvaComponent: os.Getenv("CANVA_COMPONENT"),
logGroup: os.Getenv("LOG_GROUP"),
ecsTaskFamily: ecsTaskFamily,
ecsTaskRevision: ecsTaskRevision,
}
}

// EnrichRecord modifies existing record.
func (enr *Enricher) EnrichRecord(r map[interface{}]interface{}, t time.Time) map[interface{}]interface{} {
resource := map[interface{}]interface{}{
mappings.RESOURCE_ACCOUNT_ID: enr.canvaAWSAccount,
"service.name": enr.canvaAppName,
"cloud.platform": "aws_ecs",
"aws.ecs.launchtype": "EC2",
"aws.ecs.task.family": enr.ecsTaskFamily,
"aws.ecs.task.revision": enr.ecsTaskRevision,
"aws.log.group.names": enr.logGroup,
mappings.RESOURCE_ACCOUNT_ID: enr.canvaAWSAccount,
mappings.RESOURCE_APPLICATION_ID: enr.canvaApplicationId,
mappings.RESOURCE_COMPONENT: enr.canvaComponent,
"service.name": enr.canvaAppName,
"cloud.platform": "aws_ecs",
"aws.ecs.launchtype": "EC2",
"aws.ecs.task.family": enr.ecsTaskFamily,
"aws.ecs.task.revision": enr.ecsTaskRevision,
"aws.log.group.names": enr.logGroup,
}
body := make(map[interface{}]interface{})

Expand Down
38 changes: 21 additions & 17 deletions enricher/ecs/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ func TestEnrichRecords(t *testing.T) {
{
name: "enrich",
enr: &Enricher{
canvaAWSAccount: "canva_aws_account_val",
canvaAppName: "canva_app_name_val",
logGroup: "log_group_val",
ecsTaskFamily: "ecs_task_family_val",
ecsTaskRevision: 10001,
canvaAWSAccount: "canva_aws_account_val",
canvaApplicationId: "canva_application_id_val",
canvaAppName: "canva_app_name_val",
canvaComponent: "canva_component_val",
logGroup: "log_group_val",
ecsTaskFamily: "ecs_task_family_val",
ecsTaskRevision: 10001,
},
args: args{
map[interface{}]interface{}{
Expand All @@ -46,18 +48,20 @@ func TestEnrichRecords(t *testing.T) {
},
want: map[interface{}]interface{}{
mappings.RESOURCE_FIELD_NAME: map[interface{}]interface{}{
mappings.RESOURCE_ACCOUNT_ID: "canva_aws_account_val",
"service.name": "canva_app_name_val",
"cloud.platform": "aws_ecs",
"aws.ecs.launchtype": "EC2",
"aws.ecs.task.family": "ecs_task_family_val",
"aws.ecs.task.revision": 10001,
"aws.log.group.names": "log_group_val",
"host.id": "ec2_instance_id_val",
"aws.ecs.cluster.name": "ecs_cluster_val",
"aws.ecs.task.arn": "ecs_task_arn_val",
"container.id": "container_id_val",
"container.name": "container_name_val",
mappings.RESOURCE_ACCOUNT_ID: "canva_aws_account_val",
mappings.RESOURCE_APPLICATION_ID: "canva_application_id_val",
mappings.RESOURCE_COMPONENT: "canva_component_val",
mappings.RESOURCE_SERVICE_NAME: "canva_app_name_val",
"cloud.platform": "aws_ecs",
"aws.ecs.launchtype": "EC2",
"aws.ecs.task.family": "ecs_task_family_val",
"aws.ecs.task.revision": 10001,
"aws.log.group.names": "log_group_val",
"host.id": "ec2_instance_id_val",
"aws.ecs.cluster.name": "ecs_cluster_val",
"aws.ecs.task.arn": "ecs_task_arn_val",
"container.id": "container_id_val",
"container.name": "container_name_val",
},
"body": map[interface{}]interface{}{
"other_key_1": "other_value_1",
Expand Down
2 changes: 2 additions & 0 deletions enricher/mappings/mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
)

const (
RESOURCE_APPLICATION_ID = "application_id"
RESOURCE_FIELD_NAME = "resource"
RESOURCE_SERVICE_NAME = "service.name"
RESOURCE_PARTITION = "cloud.partition"
Expand All @@ -32,6 +33,7 @@ const (
RESOURCE_ORGANIZATION = "organization"
RESOURCE_PLATFORM = "cloud.platform"
RESOURCE_PROVIDER = "cloud.provider"
RESOURCE_COMPONENT = "component"
)

const (
Expand Down

0 comments on commit df58fc3

Please sign in to comment.