Skip to content

Commit

Permalink
[processor/awsentity] Scrape auto scaling group attributes from resou…
Browse files Browse the repository at this point in the history
…rce metrics (#1521)
  • Loading branch information
zhihonl authored Jan 29, 2025
1 parent 44f7d6f commit 8d8b261
Show file tree
Hide file tree
Showing 23 changed files with 133 additions and 43 deletions.
68 changes: 67 additions & 1 deletion plugins/processors/awsentity/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/aws/amazon-cloudwatch-agent/extension/entitystore"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsentity/entityattributes"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsentity/internal/k8sattributescraper"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/ec2tagger"
"github.com/aws/amazon-cloudwatch-agent/translator/config"
)

Expand Down Expand Up @@ -131,6 +132,12 @@ func (p *awsEntityProcessor) processMetrics(_ context.Context, md pmetric.Metric
switch p.config.EntityType {
case entityattributes.Resource:
if p.config.Platform == config.ModeEC2 {
// ec2tagger processor may have picked up the ASG name from an ec2:DescribeTags call
if getAutoScalingGroupFromEntityStore() == EMPTY && p.config.ScrapeDatapointAttribute {
if autoScalingGroup := p.scrapeResourceEntityAttribute(rm.At(i).ScopeMetrics()); autoScalingGroup != EMPTY {
setAutoScalingGroup(autoScalingGroup)
}
}
ec2Info = getEC2InfoFromEntityStore()
if ec2Info.GetInstanceID() != EMPTY {
resourceAttrs.PutStr(entityattributes.AttributeEntityType, entityattributes.AttributeEntityAWSResource)
Expand Down Expand Up @@ -285,7 +292,8 @@ func (p *awsEntityProcessor) processMetrics(_ context.Context, md pmetric.Metric

// scrapeServiceAttribute expands the datapoint attributes and search for
// service name and environment attributes. This is only used for components
// that only emit attributes on datapoint level.
// that only emit attributes on datapoint level. This code block contains a lot
// of repeated code because OTEL metrics type do not have a common interface.
func (p *awsEntityProcessor) scrapeServiceAttribute(scopeMetric pmetric.ScopeMetricsSlice) (string, string, string) {
entityServiceName := EMPTY
entityServiceNameSource := EMPTY
Expand Down Expand Up @@ -408,6 +416,64 @@ func (p *awsEntityProcessor) scrapeServiceAttribute(scopeMetric pmetric.ScopeMet
return entityServiceName, entityEnvironmentName, entityServiceNameSource
}

// scrapeResourceEntityAttribute expands the datapoint attributes and search for
// resource entity related attributes. This is only used for components
// that only emit attributes on datapoint level. This code block contains a lot
// of repeated code because OTEL metrics type do not have a common interface.
func (p *awsEntityProcessor) scrapeResourceEntityAttribute(scopeMetric pmetric.ScopeMetricsSlice) string {
autoScalingGroup := EMPTY
for j := 0; j < scopeMetric.Len(); j++ {
metric := scopeMetric.At(j).Metrics()
for k := 0; k < metric.Len(); k++ {
if autoScalingGroup != EMPTY {
return autoScalingGroup
}
m := metric.At(k)
switch m.Type() {
case pmetric.MetricTypeGauge:
dps := m.Gauge().DataPoints()
for l := 0; l < dps.Len(); l++ {
if dpAutoScalingGroup, ok := dps.At(l).Attributes().Get(ec2tagger.CWDimensionASG); ok {
autoScalingGroup = dpAutoScalingGroup.Str()
}
}
case pmetric.MetricTypeSum:
dps := m.Sum().DataPoints()
for l := 0; l < dps.Len(); l++ {
if dpAutoScalingGroup, ok := dps.At(l).Attributes().Get(ec2tagger.CWDimensionASG); ok {
autoScalingGroup = dpAutoScalingGroup.Str()
}
}
case pmetric.MetricTypeHistogram:
dps := m.Histogram().DataPoints()
for l := 0; l < dps.Len(); l++ {
if dpAutoScalingGroup, ok := dps.At(l).Attributes().Get(ec2tagger.CWDimensionASG); ok {
autoScalingGroup = dpAutoScalingGroup.Str()
}
}
case pmetric.MetricTypeExponentialHistogram:
dps := m.ExponentialHistogram().DataPoints()
for l := 0; l < dps.Len(); l++ {
if dpAutoScalingGroup, ok := dps.At(l).Attributes().Get(ec2tagger.CWDimensionASG); ok {
autoScalingGroup = dpAutoScalingGroup.Str()
}
}
case pmetric.MetricTypeSummary:
dps := m.Sum().DataPoints()
for l := 0; l < dps.Len(); l++ {
if dpAutoScalingGroup, ok := dps.At(l).Attributes().Get(ec2tagger.CWDimensionASG); ok {
autoScalingGroup = dpAutoScalingGroup.Str()
}
}
default:
p.logger.Debug("Ignore unknown metric type", zap.String("type", m.Type().String()))
}

}
}
return autoScalingGroup
}

// getServiceAttributes prioritize service name retrieval based on
// following attribute priority
// 1. service.name
Expand Down
2 changes: 1 addition & 1 deletion plugins/processors/ec2tagger/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const sampleConfig = `

const (
Ec2InstanceTagKeyASG = "aws:autoscaling:groupName"
cwDimensionASG = "AutoScalingGroupName"
CWDimensionASG = "AutoScalingGroupName"
mdKeyInstanceId = "InstanceId"
mdKeyImageId = "ImageId"
mdKeyInstanceType = "InstanceType"
Expand Down
6 changes: 3 additions & 3 deletions plugins/processors/ec2tagger/ec2tagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (t *Tagger) updateTags() error {
key := *tag.Key
if Ec2InstanceTagKeyASG == key {
// rename to match CW dimension as applied by AutoScaling service, not the EC2 tag
key = cwDimensionASG
key = CWDimensionASG
}
tags[key] = *tag.Value
}
Expand Down Expand Up @@ -249,7 +249,7 @@ func (t *Tagger) ec2TagsRetrieved() bool {
if t.ec2TagCache != nil {
for _, key := range t.EC2InstanceTagKeys {
if key == Ec2InstanceTagKeyASG {
key = cwDimensionASG
key = CWDimensionASG
}
if key == "*" {
continue
Expand Down Expand Up @@ -303,7 +303,7 @@ func (t *Tagger) Start(ctx context.Context, host component.Host) error {
if !useAllTags && len(t.EC2InstanceTagKeys) > 0 {
// if the customer said 'AutoScalingGroupName' (the CW dimension), do what they mean not what they said
for i, key := range t.EC2InstanceTagKeys {
if cwDimensionASG == key {
if CWDimensionASG == key {
t.EC2InstanceTagKeys[i] = Ec2InstanceTagKeyASG
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ processors:
awsentity/resource:
entity_type: Resource
platform: ec2
scrape_datapoint_attribute: true
cumulativetodelta/hostDeltaMetrics:
exclude:
match_type: strict
Expand Down Expand Up @@ -86,8 +87,8 @@ service:
exporters:
- awscloudwatch
processors:
- awsentity/resource
- ec2tagger
- awsentity/resource
receivers:
- telegraf_netstat
- telegraf_swap
Expand All @@ -98,9 +99,9 @@ service:
exporters:
- awscloudwatch
processors:
- awsentity/resource
- cumulativetodelta/hostDeltaMetrics
- ec2tagger
- awsentity/resource
receivers:
- telegraf_diskio
telemetry:
Expand Down
5 changes: 3 additions & 2 deletions translator/tocwconfig/sampleConfig/advanced_config_linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ processors:
awsentity/resource:
entity_type: Resource
platform: ec2
scrape_datapoint_attribute: true
cumulativetodelta/hostDeltaMetrics:
exclude:
match_type: strict
Expand Down Expand Up @@ -94,8 +95,8 @@ service:
exporters:
- awscloudwatch
processors:
- awsentity/resource
- ec2tagger
- awsentity/resource
receivers:
- telegraf_ethtool
- telegraf_nvidia_smi
Expand All @@ -108,9 +109,9 @@ service:
exporters:
- awscloudwatch
processors:
- awsentity/resource
- cumulativetodelta/hostDeltaMetrics
- ec2tagger
- awsentity/resource
receivers:
- telegraf_diskio
telemetry:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ processors:
awsentity/resource:
entity_type: Resource
platform: ec2
scrape_datapoint_attribute: true
ec2tagger:
ec2_instance_tag_keys:
- AutoScalingGroupName
Expand Down Expand Up @@ -87,8 +88,8 @@ service:
exporters:
- awscloudwatch
processors:
- awsentity/resource
- ec2tagger
- awsentity/resource
receivers:
- telegraf_win_perf_counters/2073218482
- telegraf_win_perf_counters/2039663244
Expand Down
3 changes: 2 additions & 1 deletion translator/tocwconfig/sampleConfig/amp_config_linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ processors:
awsentity/resource:
entity_type: Resource
platform: ec2
scrape_datapoint_attribute: true
batch/host/amp:
metadata_cardinality_limit: 1000
send_batch_max_size: 0
Expand Down Expand Up @@ -152,9 +153,9 @@ service:
exporters:
- awscloudwatch
processors:
- awsentity/resource
- ec2tagger
- transform
- awsentity/resource
receivers:
- telegraf_cpu
telemetry:
Expand Down
3 changes: 2 additions & 1 deletion translator/tocwconfig/sampleConfig/basic_config_linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ processors:
awsentity/resource:
entity_type: Resource
platform: ec2
scrape_datapoint_attribute: true
ec2tagger:
ec2_instance_tag_keys:
- AutoScalingGroupName
Expand Down Expand Up @@ -60,8 +61,8 @@ service:
exporters:
- awscloudwatch
processors:
- awsentity/resource
- ec2tagger
- awsentity/resource
receivers:
- telegraf_mem
- telegraf_disk
Expand Down
3 changes: 2 additions & 1 deletion translator/tocwconfig/sampleConfig/basic_config_windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ processors:
awsentity/resource:
entity_type: Resource
platform: ec2
scrape_datapoint_attribute: true
ec2tagger:
ec2_instance_tag_keys:
- AutoScalingGroupName
Expand Down Expand Up @@ -62,8 +63,8 @@ service:
exporters:
- awscloudwatch
processors:
- awsentity/resource
- ec2tagger
- awsentity/resource
receivers:
- telegraf_win_perf_counters/1492679118
- telegraf_win_perf_counters/4283769065
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ processors:
awsentity/resource:
entity_type: Resource
platform: ec2
scrape_datapoint_attribute: true
awsentity/service/telegraf:
entity_type: Service
platform: ec2
Expand Down Expand Up @@ -280,9 +281,9 @@ service:
exporters:
- awscloudwatch
processors:
- awsentity/resource
- ec2tagger
- transform
- awsentity/resource
receivers:
- telegraf_disk
- telegraf_swap
Expand All @@ -305,10 +306,10 @@ service:
exporters:
- awscloudwatch
processors:
- awsentity/resource
- cumulativetodelta/hostDeltaMetrics
- ec2tagger
- transform
- awsentity/resource
receivers:
- telegraf_diskio
- telegraf_net
Expand Down
5 changes: 3 additions & 2 deletions translator/tocwconfig/sampleConfig/complete_linux_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ processors:
awsentity/resource:
entity_type: Resource
platform: ec2
scrape_datapoint_attribute: true
awsentity/service/telegraf:
entity_type: Service
platform: ec2
Expand Down Expand Up @@ -387,9 +388,9 @@ service:
exporters:
- awscloudwatch
processors:
- awsentity/resource
- ec2tagger
- transform
- awsentity/resource
receivers:
- telegraf_swap
- telegraf_cpu
Expand All @@ -412,10 +413,10 @@ service:
exporters:
- awscloudwatch
processors:
- awsentity/resource
- cumulativetodelta/hostDeltaMetrics/cloudwatch
- ec2tagger
- transform
- awsentity/resource
receivers:
- telegraf_net
- telegraf_diskio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ processors:
awsentity/resource:
entity_type: Resource
platform: ec2
scrape_datapoint_attribute: true
awsentity/service/telegraf:
entity_type: Service
platform: ec2
Expand Down Expand Up @@ -267,9 +268,9 @@ service:
exporters:
- awscloudwatch
processors:
- awsentity/resource
- ec2tagger
- transform
- awsentity/resource
receivers:
- telegraf_win_perf_counters/1063858558
- telegraf_nvidia_smi
Expand Down
3 changes: 2 additions & 1 deletion translator/tocwconfig/sampleConfig/delta_config_linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ processors:
awsentity/resource:
entity_type: Resource
platform: ec2
scrape_datapoint_attribute: true
cumulativetodelta/hostDeltaMetrics:
exclude:
match_type: strict
Expand Down Expand Up @@ -80,10 +81,10 @@ service:
exporters:
- awscloudwatch
processors:
- awsentity/resource
- cumulativetodelta/hostDeltaMetrics
- ec2tagger
- transform
- awsentity/resource
receivers:
- telegraf_diskio
telemetry:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ processors:
awsentity/resource:
entity_type: Resource
platform: ec2
scrape_datapoint_attribute: true
cumulativetodelta/hostDeltaMetrics:
exclude:
match_type: ""
Expand Down Expand Up @@ -63,9 +64,9 @@ service:
exporters:
- awscloudwatch
processors:
- awsentity/resource
- cumulativetodelta/hostDeltaMetrics
- ec2tagger
- awsentity/resource
receivers:
- telegraf_net
telemetry:
Expand Down
Loading

0 comments on commit 8d8b261

Please sign in to comment.