Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
majanjua-amzn authored Jan 29, 2025
2 parents 53a4b64 + 8d8b261 commit cb8d9a6
Show file tree
Hide file tree
Showing 26 changed files with 160 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ run:

output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
format: colored-line-number
formats: colored-line-number

# print lines of code with issue, default is true
print-issued-lines: true
Expand Down
13 changes: 13 additions & 0 deletions plugins/outputs/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@ func (c *CloudWatch) pushMetricDatum() {
case metric := <-c.metricChan:
entity, datums := c.BuildMetricDatum(metric)
numberOfPartitions := len(datums)
/* We currently do not account for entity information as a part of the payload size.
This is by design and should be revisited once the SDK protocol changes.
In the meantime there has been a payload limit increase applied in the background to accommodate this decision
Otherwise to include entity size you would do something like this:
c.metricDatumBatch.Size += calculateEntitySize(entity)
In addition to calculating the size of the entity object, you might also need to account for any extra bytes that get
added on an individual metric level when entity data is present (depends on how the sdk protocol changes)—something like:
c.metricDatumBatch.Size += payload(datums[i], entityPresent=true)
File diff that could be useful: https://github.com/aws/amazon-cloudwatch-agent/compare/af960d7...459ef7c
*/
for i := 0; i < numberOfPartitions; i++ {
entityStr := entityToString(entity)
c.metricDatumBatch.Partition[entityStr] = append(c.metricDatumBatch.Partition[entityStr], datums[i])
Expand Down
13 changes: 13 additions & 0 deletions plugins/outputs/cloudwatch/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ const (
valueOverheads = 47
// &MetricData.member.1.Unit=Kilobytes/Second
unitOverheads = 42

/* Entity overheads - these would be used to calculate entity size if we decide to include it as a part of the payload.
The three main components are the KeyAttributes key/value pair, Attributes key/value pair, and StrictEntityValidation
// &StrictEntityValidation=false
strictEntityValidationSize = 29
// &EntityMetricData.member.100.Entity.KeyAttributes.entry.1.key= &EntityMetricData.member.100.Entity.KeyAttributes.entry.1.value=
entityKeyAttributesOverhead = 62 + 64
// &EntityMetricData.member.100.Entity.Attributes.entry.1.key= &EntityMetricData.member.100.Entity.Attributes.entry.1.value=
entityAttributesOverhead = 59 + 61
// EntityMetricData.member.100.
entityMetricDataPrefixOverhead = 28
*/
)

// Set seed once.
Expand Down
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
Loading

0 comments on commit cb8d9a6

Please sign in to comment.