From df58fc336752863412568af66b43dfcd4373cfc5 Mon Sep 17 00:00:00 2001 From: Louise Harlen Date: Tue, 28 May 2024 16:43:22 +0100 Subject: [PATCH] Add standard tags to ECS logs (#22) * add application_id tag * rest of the changes * Remove duplicate service.name * fix tests * comments * fix test * rename applicaiton_id env var --- enricher/ecs/ecs.go | 40 ++++++++++++++++++++--------------- enricher/ecs/ecs_test.go | 38 ++++++++++++++++++--------------- enricher/mappings/mappings.go | 2 ++ 3 files changed, 46 insertions(+), 34 deletions(-) diff --git a/enricher/ecs/ecs.go b/enricher/ecs/ecs.go index 3e8ecd5..11f9141 100644 --- a/enricher/ecs/ecs.go +++ b/enricher/ecs/ecs.go @@ -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) @@ -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{}) diff --git a/enricher/ecs/ecs_test.go b/enricher/ecs/ecs_test.go index 1ea01ad..6df7874 100644 --- a/enricher/ecs/ecs_test.go +++ b/enricher/ecs/ecs_test.go @@ -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{}{ @@ -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", diff --git a/enricher/mappings/mappings.go b/enricher/mappings/mappings.go index ccf88e2..122fa95 100644 --- a/enricher/mappings/mappings.go +++ b/enricher/mappings/mappings.go @@ -22,6 +22,7 @@ const ( ) const ( + RESOURCE_APPLICATION_ID = "application_id" RESOURCE_FIELD_NAME = "resource" RESOURCE_SERVICE_NAME = "service.name" RESOURCE_PARTITION = "cloud.partition" @@ -32,6 +33,7 @@ const ( RESOURCE_ORGANIZATION = "organization" RESOURCE_PLATFORM = "cloud.platform" RESOURCE_PROVIDER = "cloud.provider" + RESOURCE_COMPONENT = "component" ) const (