Skip to content

Commit

Permalink
Add namespaced azure container app tags to spans and profiles (#30637)
Browse files Browse the repository at this point in the history
(cherry picked from commit cde236e)
  • Loading branch information
duncanpharvey authored and github-actions[bot] committed Nov 13, 2024
1 parent 3797cb8 commit 83179bb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 35 deletions.
19 changes: 12 additions & 7 deletions cmd/serverless-init/cloudservice/containerapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 20 additions & 15 deletions cmd/serverless-init/cloudservice/containerapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
Expand Down
24 changes: 15 additions & 9 deletions pkg/trace/api/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -87,15 +98,10 @@ 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))
}
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)
Expand Down
12 changes: 8 additions & 4 deletions pkg/trace/api/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down

0 comments on commit 83179bb

Please sign in to comment.