From 4040cd6aaefc90061de14587df3ff743f561ba5a Mon Sep 17 00:00:00 2001 From: Duncan Harvey Date: Wed, 30 Oct 2024 16:10:45 -0400 Subject: [PATCH 1/3] add namespaced azure container app tags to spans and profiles --- .../cloudservice/containerapp.go | 19 ++++++++++------ pkg/trace/api/profiles.go | 22 ++++++++++++------- pkg/trace/api/profiles_test.go | 12 ++++++---- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/cmd/serverless-init/cloudservice/containerapp.go b/cmd/serverless-init/cloudservice/containerapp.go index dba7e62282f6bc..92e0433ce16981 100644 --- a/cmd/serverless-init/cloudservice/containerapp.go +++ b/cmd/serverless-init/cloudservice/containerapp.go @@ -48,24 +48,29 @@ func (c *ContainerApp) GetTags() map[string]string { replica := os.Getenv(ContainerAppReplicaName) tags := map[string]string{ - "app_name": appName, - "region": region, - "revision": revision, - "replica_name": replica, - "origin": c.GetOrigin(), - "_dd.origin": c.GetOrigin(), + "app_name": appName, + "region": region, + "revision": revision, + "replica_name": replica, + "aca.replica.name": replica, + "origin": c.GetOrigin(), + "_dd.origin": c.GetOrigin(), } if c.SubscriptionId != "" { tags["subscription_id"] = c.SubscriptionId + tags["aca.subscription.id"] = c.SubscriptionId } if c.ResourceGroup != "" { tags["resource_group"] = c.ResourceGroup + tags["aca.resource.group"] = c.ResourceGroup } if c.SubscriptionId != "" && c.ResourceGroup != "" { - tags["resource_id"] = fmt.Sprintf("/subscriptions/%v/resourcegroups/%v/providers/microsoft.app/containerapps/%v", c.SubscriptionId, c.ResourceGroup, strings.ToLower(appName)) + resourceID := fmt.Sprintf("/subscriptions/%v/resourcegroups/%v/providers/microsoft.app/containerapps/%v", c.SubscriptionId, c.ResourceGroup, strings.ToLower(appName)) + tags["resource_id"] = resourceID + tags["aca.resource.id"] = resourceID } return tags diff --git a/pkg/trace/api/profiles.go b/pkg/trace/api/profiles.go index dc1f2fe234ca80..ee556e0f8bc264 100644 --- a/pkg/trace/api/profiles.go +++ b/pkg/trace/api/profiles.go @@ -87,15 +87,21 @@ func (r *HTTPReceiver) profileProxyHandler() http.Handler { tags.WriteString("_dd.origin:lambda") } - // Azure Container App metadata - if subscriptionID, ok := r.conf.GlobalTags["subscription_id"]; ok { - tags.WriteString(fmt.Sprintf(",subscription_id:%s", subscriptionID)) + azureContainerAppTags := []string{ + "subscription_id", + "resource_group", + "resource_id", + "replicate_name", + "aca.subscription.id", + "aca.resource.group", + "aca.resource.id", + "aca.replica.name", } - if resourceGroup, ok := r.conf.GlobalTags["resource_group"]; ok { - tags.WriteString(fmt.Sprintf(",resource_group:%s", resourceGroup)) - } - if resourceID, ok := r.conf.GlobalTags["resource_id"]; ok { - tags.WriteString(fmt.Sprintf(",resource_id:%s", resourceID)) + + for _, azureContainerAppTag := range azureContainerAppTags { + if value, ok := r.conf.GlobalTags[azureContainerAppTag]; ok { + tags.WriteString(fmt.Sprintf(",%s:%s", azureContainerAppTag, value)) + } } return newProfileProxy(r.conf, targets, keys, tags.String(), r.statsd) diff --git a/pkg/trace/api/profiles_test.go b/pkg/trace/api/profiles_test.go index a5f74b62ffd840..b11b162deaf9f3 100644 --- a/pkg/trace/api/profiles_test.go +++ b/pkg/trace/api/profiles_test.go @@ -314,7 +314,7 @@ func TestProfileProxyHandler(t *testing.T) { kv := strings.Split(tag, ":") m[kv[0]] = kv[1] } - for _, tag := range []string{"subscription_id", "resource_group", "resource_id"} { + for _, tag := range []string{"subscription_id", "resource_group", "resource_id", "aca.subscription.id", "aca.resource.group", "aca.resource.id", "aca.replica.name"} { if _, ok := m[tag]; !ok { t.Fatalf("invalid X-Datadog-Additional-Tags header, should contain '%s': %q", tag, v) } @@ -324,9 +324,13 @@ func TestProfileProxyHandler(t *testing.T) { conf := newTestReceiverConfig() conf.ProfilingProxy = config.ProfilingProxyConfig{DDURL: srv.URL} conf.GlobalTags = map[string]string{ - "subscription_id": "123", - "resource_group": "test-rg", - "resource_id": "456", + "subscription_id": "123", + "resource_group": "test-rg", + "resource_id": "456", + "aca.subscription.id": "123", + "aca.resource.group": "test-rg", + "aca.resource.id": "456", + "aca.replica.name": "test-replica", } req, err := http.NewRequest("POST", "/some/path", nil) if err != nil { From e7b1c3010cf09cd9bc418a745f5958b5ac682314 Mon Sep 17 00:00:00 2001 From: Duncan Harvey Date: Thu, 31 Oct 2024 11:36:42 -0400 Subject: [PATCH 2/3] fix unit tests --- .../cloudservice/containerapp_test.go | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/cmd/serverless-init/cloudservice/containerapp_test.go b/cmd/serverless-init/cloudservice/containerapp_test.go index 7da872f0c4faac..adb1341af57fb0 100644 --- a/cmd/serverless-init/cloudservice/containerapp_test.go +++ b/cmd/serverless-init/cloudservice/containerapp_test.go @@ -27,12 +27,13 @@ func TestGetContainerAppTags(t *testing.T) { tags := service.GetTags() assert.Equal(t, map[string]string{ - "app_name": "test_app_name", - "origin": "containerapp", - "region": "eastus", - "revision": "test_revision", - "replica_name": "test--6nyz8z7-b845f7667-m7hlv", - "_dd.origin": "containerapp", + "app_name": "test_app_name", + "origin": "containerapp", + "region": "eastus", + "revision": "test_revision", + "replica_name": "test--6nyz8z7-b845f7667-m7hlv", + "aca.replica.name": "test--6nyz8z7-b845f7667-m7hlv", + "_dd.origin": "containerapp", }, tags) } @@ -53,15 +54,19 @@ func TestGetContainerAppTagsWithOptionalEnvVars(t *testing.T) { tags := service.GetTags() assert.Equal(t, map[string]string{ - "app_name": "test_app_name", - "origin": "containerapp", - "region": "eastus", - "revision": "test_revision", - "replica_name": "test--6nyz8z7-b845f7667-m7hlv", - "_dd.origin": "containerapp", - "subscription_id": "test_subscription_id", - "resource_id": "/subscriptions/test_subscription_id/resourcegroups/test_resource_group/providers/microsoft.app/containerapps/test_app_name", - "resource_group": "test_resource_group", + "app_name": "test_app_name", + "origin": "containerapp", + "region": "eastus", + "revision": "test_revision", + "replica_name": "test--6nyz8z7-b845f7667-m7hlv", + "aca.replica.name": "test--6nyz8z7-b845f7667-m7hlv", + "_dd.origin": "containerapp", + "subscription_id": "test_subscription_id", + "aca.subscription.id": "test_subscription_id", + "resource_id": "/subscriptions/test_subscription_id/resourcegroups/test_resource_group/providers/microsoft.app/containerapps/test_app_name", + "aca.resource.id": "/subscriptions/test_subscription_id/resourcegroups/test_resource_group/providers/microsoft.app/containerapps/test_app_name", + "resource_group": "test_resource_group", + "aca.resource.group": "test_resource_group", }, tags) assert.Nil(t, err) From 01dfdba8ce556a78d59c1a344b4151631df8628a Mon Sep 17 00:00:00 2001 From: Duncan Harvey Date: Thu, 31 Oct 2024 13:13:35 -0400 Subject: [PATCH 3/3] move azureContainerAppTags to global variable --- pkg/trace/api/profiles.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pkg/trace/api/profiles.go b/pkg/trace/api/profiles.go index ee556e0f8bc264..a3f2ecf87a0cc2 100644 --- a/pkg/trace/api/profiles.go +++ b/pkg/trace/api/profiles.go @@ -30,6 +30,17 @@ const ( profilingV1EndpointSuffix = "v1/input" ) +var azureContainerAppTags = []string{ + "subscription_id", + "resource_group", + "resource_id", + "replicate_name", + "aca.subscription.id", + "aca.resource.group", + "aca.resource.id", + "aca.replica.name", +} + // profilingEndpoints returns the profiling intake urls and their corresponding // api keys based on agent configuration. The main endpoint is always returned as // the first element in the slice. @@ -87,17 +98,6 @@ func (r *HTTPReceiver) profileProxyHandler() http.Handler { tags.WriteString("_dd.origin:lambda") } - azureContainerAppTags := []string{ - "subscription_id", - "resource_group", - "resource_id", - "replicate_name", - "aca.subscription.id", - "aca.resource.group", - "aca.resource.id", - "aca.replica.name", - } - for _, azureContainerAppTag := range azureContainerAppTags { if value, ok := r.conf.GlobalTags[azureContainerAppTag]; ok { tags.WriteString(fmt.Sprintf(",%s:%s", azureContainerAppTag, value))