Skip to content

Commit

Permalink
Deprecate old metric decorator code (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
JayPolanco authored May 8, 2023
1 parent 4824277 commit 0247587
Show file tree
Hide file tree
Showing 12 changed files with 16 additions and 396 deletions.
80 changes: 1 addition & 79 deletions plugins/outputs/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"context"
"log"
"reflect"
"runtime"
"sort"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -64,7 +62,6 @@ type CloudWatch struct {
datumBatchChan chan []*cloudwatch.MetricDatum
metricDatumBatch *MetricDatumBatch
shutdownChan chan struct{}
metricDecorations *MetricDecorations
retries int
publisher *publisher.Publisher
retryer *retryer.LogThrottleRetryer
Expand All @@ -82,16 +79,11 @@ func (c *CloudWatch) Capabilities() consumer.Capabilities {
}

func (c *CloudWatch) Start(_ context.Context, host component.Host) error {
var err error
c.publisher, _ = publisher.NewPublisher(
publisher.NewNonBlockingFifoQueue(metricChanBufferSize),
maxConcurrentPublisher,
2*time.Second,
c.WriteToCloudWatch)
c.metricDecorations, err = NewMetricDecorations(c.config.MetricDecorations)
if err != nil {
return err
}
credentialConfig := &configaws.CredentialConfig{
Region: c.config.Region,
AccessKey: c.config.AccessKey,
Expand All @@ -116,8 +108,6 @@ func (c *CloudWatch) Start(_ context.Context, host component.Host) error {
svc.Handlers.Build.PushBackNamed(handlers.NewCustomHeaderHandler("User-Agent", agentinfo.Get().UserAgent()))
//Format unique roll up list
c.config.RollupDimensions = GetUniqueRollupList(c.config.RollupDimensions)
//Construct map for metrics that dropping origin
c.droppingOriginMetrics = c.GetDroppingDimensionMap()
c.svc = svc
c.retryer = logThrottleRetryer
c.startRoutines()
Expand Down Expand Up @@ -376,31 +366,6 @@ func (c *CloudWatch) WriteToCloudWatch(req interface{}) {
}
}

func (c *CloudWatch) decorateMetricName(category string, name string) (decoratedName string) {
if c.metricDecorations != nil {
decoratedName = c.metricDecorations.getRename(category, name)
}
if decoratedName == "" {
if name == "value" {
decoratedName = category
} else {
separator := "_"
if runtime.GOOS == "windows" {
separator = " "
}
decoratedName = strings.Join([]string{category, name}, separator)
}
}
return
}

func (c *CloudWatch) decorateMetricUnit(category string, name string) (decoratedUnit string) {
if c.metricDecorations != nil {
decoratedUnit = c.metricDecorations.getUnit(category, name)
}
return
}

// BuildMetricDatum may just return the datum as-is.
// Or it might expand it into many datums due to dimension aggregation.
// There may also be more datums due to resize() on a distribution.
Expand All @@ -420,12 +385,9 @@ func (c *CloudWatch) BuildMetricDatum(metric *aggregationDatum) []*cloudwatch.Me
}

dimensionsList := c.ProcessRollup(metric.Dimensions)
for index, dimensions := range dimensionsList {
for _, dimensions := range dimensionsList {
//index == 0 means it's the original metrics, and if the metric name and dimension matches, skip creating
//metric datum
if index == 0 && c.IsDropping(*metric.MetricDatum.MetricName) {
continue
}
if len(distList) == 0 {
// Not a distribution.
datum := &cloudwatch.MetricDatum{
Expand Down Expand Up @@ -565,46 +527,6 @@ func GetUniqueRollupList(inputLists [][]string) [][]string {
return uniqueLists
}

func (c *CloudWatch) IsDropping(metricName string) bool {
// Check if any metrics are provided in drop_original_metrics
if len(c.droppingOriginMetrics) == 0 {
return false
}
if dropping := c.droppingOriginMetrics.Contains(metricName); dropping {
return dropping
}
separator := "_"
if runtime.GOOS == "windows" {
separator = " "
}
// Breakdown metricName to check if drop original specified only for category name (*)
unDecoratedMetrics := strings.Split(metricName, separator)
for index := range unDecoratedMetrics {
category := strings.Join(unDecoratedMetrics[:index], separator)
if dropping := c.droppingOriginMetrics.Contains(category); dropping {
return dropping
}
}

return false
}

func (c *CloudWatch) GetDroppingDimensionMap() collections.Set[string] {
droppingMetrics := collections.NewSet[string]()
for category, fields := range c.config.DropOriginConfigs {
for index, field := range fields {
if index == 0 && dropOriginalWildcard == field {
droppingMetrics.Add(category)
} else {
decoratedName := c.decorateMetricName(category, field)
droppingMetrics.Add(decoratedName)

}
}
}
return droppingMetrics
}

func (c *CloudWatch) SampleConfig() string {
return ""
}
Expand Down
64 changes: 1 addition & 63 deletions plugins/outputs/cloudwatch/cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cloudwatch
import (
"context"
"log"
"runtime"
"strconv"
"strings"
"testing"
Expand All @@ -16,11 +15,9 @@ import (
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
"github.com/aws/private-amazon-cloudwatch-agent-staging/internal/publisher"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/aws/private-amazon-cloudwatch-agent-staging/internal/publisher"
)

// Return true if found.
Expand Down Expand Up @@ -459,62 +456,3 @@ func TestCloudWatch_metricDatumBatchFull(t *testing.T) {
<-c.datumBatchChan
assert.False(t, c.metricDatumBatchFull())
}

func TestIsDropping(t *testing.T) {
svc := new(mockCloudWatchClient)
cw := newCloudWatch(svc, time.Second)

tests := map[string]struct {
input map[string][]string
checks []string
want []bool
unix_os bool
}{
"TestIsDroppingWithMultipleCategory": {
input: map[string][]string{"cpu": []string{"usage_idle", "time_active"},
"nvidia_smi": {"utilization_gpu"}},
checks: []string{"cpu_usage_idle", "cpu_time_active", "nvidia_smi", "cpu_usage_guest"},
want: []bool{true, true, false, false},
unix_os: true,
},
"TestIsDroppingWith*": {
input: map[string][]string{"cpu": []string{"usage_idle", "time_active"},
"nvidia_smi": {"*"}},
checks: []string{"cpu_usage_idle", "cpu_time_active", "nvidia_smi", "nvidia_smi_utilization_gpu", "cpu"},
want: []bool{true, true, true, true, false},
unix_os: true,
},
"TestIsDroppingWithMultipleCategoryWindows": {
input: map[string][]string{"cpu": []string{"usage_idle", "time_active"},
"nvidia_smi": {"utilization_gpu"}},
checks: []string{"cpu usage_idle", "cpu time_active", "nvidia_smi", "cpu usage_guest"},
want: []bool{true, true, false, false},
unix_os: false,
},

"TestIsDroppingWith*Windows": {
input: map[string][]string{"cpu": []string{"usage_idle", "time_active"},
"nvidia_smi": {"*"}},
checks: []string{"cpu usage_idle", "cpu time_active", "nvidia_smi", "nvidia_smi utilization_gpu", "cpu"},
want: []bool{true, true, true, true, false},
unix_os: false,
},
}
for _, test := range tests {
cw.config.DropOriginConfigs = test.input
cw.droppingOriginMetrics = cw.GetDroppingDimensionMap()
for i, check := range test.checks {
if runtime.GOOS == "windows" {
if test.unix_os {
continue
}
} else {
if !(test.unix_os) {
continue
}
}
got := cw.IsDropping(check)
require.Equal(t, test.want[i], got)
}
}
}
29 changes: 14 additions & 15 deletions plugins/outputs/cloudwatch/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ import (

// Config represent a configuration for the CloudWatch logs exporter.
type Config struct {
Region string `mapstructure:"region"`
EndpointOverride string `mapstructure:"endpoint_override,omitempty"`
AccessKey string `mapstructure:"access_key,omitempty"`
SecretKey string `mapstructure:"secret_key,omitempty"`
RoleARN string `mapstructure:"role_arn,omitempty"`
Profile string `mapstructure:"profile,omitempty"`
SharedCredentialFilename string `mapstructure:"shared_credential_file,omitempty"`
Token string `mapstructure:"token,omitempty"`
ForceFlushInterval time.Duration `mapstructure:"force_flush_interval"`
MaxDatumsPerCall int `mapstructure:"max_datums_per_call"`
MaxValuesPerDatum int `mapstructure:"max_values_per_datum"`
MetricDecorations []MetricDecorationConfig `mapstructure:"metric_decoration,omitempty"`
RollupDimensions [][]string `mapstructure:"rollup_dimensions,omitempty"`
DropOriginConfigs map[string][]string `mapstructure:"drop_original_metrics,omitempty"`
Namespace string `mapstructure:"namespace"`
Region string `mapstructure:"region"`
EndpointOverride string `mapstructure:"endpoint_override,omitempty"`
AccessKey string `mapstructure:"access_key,omitempty"`
SecretKey string `mapstructure:"secret_key,omitempty"`
RoleARN string `mapstructure:"role_arn,omitempty"`
Profile string `mapstructure:"profile,omitempty"`
SharedCredentialFilename string `mapstructure:"shared_credential_file,omitempty"`
Token string `mapstructure:"token,omitempty"`
ForceFlushInterval time.Duration `mapstructure:"force_flush_interval"`
MaxDatumsPerCall int `mapstructure:"max_datums_per_call"`
MaxValuesPerDatum int `mapstructure:"max_values_per_datum"`
RollupDimensions [][]string `mapstructure:"rollup_dimensions,omitempty"`
DropOriginConfigs map[string][]string `mapstructure:"drop_original_metrics,omitempty"`
Namespace string `mapstructure:"namespace"`

// ResourceToTelemetrySettings is the option for converting resource
// attributes to telemetry attributes.
Expand Down
82 changes: 0 additions & 82 deletions plugins/outputs/cloudwatch/metric_decoration.go

This file was deleted.

Loading

0 comments on commit 0247587

Please sign in to comment.