Skip to content

Commit

Permalink
feat: unwrap json encoded attributes in SQS & SNS
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Jul 9, 2024
1 parent 85b0d74 commit 67c5f49
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions scrapers/aws/aws.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"encoding/json"
"fmt"
"net/url"
"slices"
Expand Down Expand Up @@ -187,7 +188,7 @@ func (aws Scraper) sqs(ctx *AWSContext, config v1.AWS, results *v1.ScrapeResults
CreatedAt: lo.ToPtr(time.Unix(createdTimestamp, 0)),
BaseScraper: config.BaseScraper,
Properties: []*types.Property{getConsoleLink(ctx.Session.Region, v1.AWSSQS, queueURL, nil)},
Config: getQueueAttributesOutput.Attributes,
Config: unwrapFields(getQueueAttributesOutput.Attributes, "Policy"),
Labels: getQueueAttributesOutput.Attributes,
ConfigClass: "Queue",
Name: queueName,
Expand Down Expand Up @@ -265,7 +266,7 @@ func (aws Scraper) snsTopics(ctx *AWSContext, config v1.AWS, results *v1.ScrapeR
Type: v1.AWSSNSTopic,
BaseScraper: config.BaseScraper,
Properties: []*types.Property{getConsoleLink(ctx.Session.Region, v1.AWSSNSTopic, topicArn, nil)},
Config: attributeOutput.Attributes,
Config: unwrapFields(attributeOutput.Attributes, "Policy", "EffectiveDeliveryPolicy"),
Labels: labels,
ConfigClass: "Topic",
Name: topicName,
Expand Down Expand Up @@ -324,7 +325,7 @@ func (aws Scraper) ecsClusters(ctx *AWSContext, config v1.AWS, results *v1.Scrap
}

func (aws Scraper) ecsServices(ctx *AWSContext, config v1.AWS, client *ecs.Client, cluster, clusterName string, results *v1.ScrapeResults) {
if !config.Includes("ecsservice") {
if !config.Includes("ECSService") {
return
}

Expand Down Expand Up @@ -441,10 +442,10 @@ func (aws Scraper) ecsTasks(ctx *AWSContext, config v1.AWS, client *ecs.Client,
var name string
labels := make(map[string]string)
for _, tag := range task.Tags {
labels[*tag.Key] = *tag.Value

if *tag.Key == "Name" {
if strings.ToLower(*tag.Key) == "name" {
name = *tag.Value
} else {
labels[*tag.Key] = *tag.Value
}
}

Expand Down Expand Up @@ -580,7 +581,7 @@ func (aws Scraper) eksFargateProfiles(ctx *AWSContext, config v1.AWS, client *ek
}

func (aws Scraper) elastiCache(ctx *AWSContext, config v1.AWS, results *v1.ScrapeResults) {
if config.Excludes("ElastiCache") {
if !config.Includes("ElastiCache") {
return
}

Expand Down Expand Up @@ -608,7 +609,7 @@ func (aws Scraper) elastiCache(ctx *AWSContext, config v1.AWS, results *v1.Scrap
}

func (aws Scraper) lambdaFunctions(ctx *AWSContext, config v1.AWS, results *v1.ScrapeResults) {
if config.Excludes("lambda") {
if !config.Includes("lambda") {
return
}
ctx.Logger.V(2).Infof("scraping lambda functions")
Expand Down Expand Up @@ -1889,3 +1890,22 @@ func getConsoleLink(region, resourceType, resourceID string, opt map[string]stri
func formatStatus(status string) string {
return lo.Capitalize(strings.ReplaceAll(strings.ReplaceAll(status, "-", " "), "_", " "))
}

// unwrapFields unwraps the given JSON encoded fields in the map.
func unwrapFields(m map[string]string, fields ...string) map[string]any {
var output = make(map[string]any)
for k, v := range m {
if lo.Contains(fields, k) {
var unwrapped map[string]any
if err := json.Unmarshal([]byte(v), &unwrapped); err != nil {
output[k] = v
} else {
output[k] = unwrapped
}
} else {
output[k] = v
}
}

return output
}

0 comments on commit 67c5f49

Please sign in to comment.