Skip to content

Commit

Permalink
Restore code for drop_original_metrics and move its logic to the tran…
Browse files Browse the repository at this point in the history
…slator (#340)
  • Loading branch information
sky333999 authored Jul 18, 2023
1 parent 3493d71 commit 6c2d68d
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 63 deletions.
21 changes: 17 additions & 4 deletions plugins/outputs/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ const (
)

const (
opPutLogEvents = "PutLogEvents"
opPutMetricData = "PutMetricData"
dropOriginalWildcard = "*"
opPutLogEvents = "PutLogEvents"
opPutMetricData = "PutMetricData"
)

type CloudWatch struct {
Expand Down Expand Up @@ -392,9 +391,12 @@ func (c *CloudWatch) BuildMetricDatum(metric *aggregationDatum) []*cloudwatch.Me
}

dimensionsList := c.ProcessRollup(metric.Dimensions)
for _, dimensions := range dimensionsList {
for index, 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 @@ -434,6 +436,17 @@ func (c *CloudWatch) BuildMetricDatum(metric *aggregationDatum) []*cloudwatch.Me
return datums
}

func (c *CloudWatch) IsDropping(metricName string) bool {
// Check if any metrics are provided in drop_original_metrics
if len(c.config.DropOriginalConfigs) == 0 {
return false
}
if _, ok := c.config.DropOriginalConfigs[metricName]; ok {
return true
}
return false
}

// sortedTagKeys returns a sorted list of keys in the map.
// Necessary for comparing a metric-name and its dimensions to determine
// if 2 metrics are actually the same.
Expand Down
61 changes: 61 additions & 0 deletions plugins/outputs/cloudwatch/cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/influxdata/telegraf/metric"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/aws/private-amazon-cloudwatch-agent-staging/handlers/agentinfo"
"github.com/aws/private-amazon-cloudwatch-agent-staging/internal/publisher"
Expand Down Expand Up @@ -255,6 +256,66 @@ func TestGetUniqueRollupList(t *testing.T) {
assert.EqualValues(t, expectedLists, actualLists, "Unique list result should be empty")
}

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

testCases := map[string]struct {
dropMetricsConfig map[string]bool
expectMetricsDropped map[string]bool
}{
"TestIsDroppingWithMultipleCategoryLinux": {
dropMetricsConfig: map[string]bool{
"cpu_usage_idle": true,
"cpu_time_active": true,
"nvidia_smi_utilization_gpu": true,
},
expectMetricsDropped: map[string]bool{
"cpu_usage_idle": true,
"cpu_time_active": true,
"nvidia_smi": false,
"cpu_usage_guest": false,
},
},
"TestIsDroppingWithMultipleCategoryWindows": {
dropMetricsConfig: map[string]bool{
"cpu usage_idle": true,
"cpu time_active": true,
"nvidia_smi utilization_gpu": true,
},
expectMetricsDropped: map[string]bool{
"cpu usage_idle": true,
"cpu time_active": true,
"nvidia_smi": false,
"cpu usage_guest": false,
},
},
"TestIsDroppingWithMetricDecoration": {
dropMetricsConfig: map[string]bool{
"CPU_USAGE_IDLE": true,
"cpu_time_active": true,
"nvidia_smi_utilization_gpu": true,
},
expectMetricsDropped: map[string]bool{
"cpu_usage_idle": false,
"CPU_USAGE_IDLE": true,
"nvidia_smi": false,
"nvidia_smi_utilization_gpu": true,
"cpu": false,
},
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
cw.config.DropOriginalConfigs = testCase.dropMetricsConfig
for metricName, expectMetricDropped := range testCase.expectMetricsDropped {
actualMetricDropped := cw.IsDropping(metricName)
require.Equal(t, expectMetricDropped, actualMetricDropped)
}
})
}
}

func TestIsFlushable(t *testing.T) {
svc := new(mockCloudWatchClient)
res := cloudwatch.PutMetricDataOutput{}
Expand Down
28 changes: 14 additions & 14 deletions plugins/outputs/cloudwatch/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +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"`
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"`
DropOriginalConfigs map[string]bool `mapstructure:"drop_original_metrics,omitempty"`
Namespace string `mapstructure:"namespace"`

// ResourceToTelemetrySettings is the option for converting resource
// attributes to telemetry attributes.
Expand Down
12 changes: 5 additions & 7 deletions plugins/outputs/cloudwatch/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,10 @@ func TestConfigDropOriginConfigs(t *testing.T) {
assert.Equal(t, 1, len(c.Exporters))
c2, ok := c.Exporters[component.NewID(TypeStr)].(*Config)
assert.True(t, ok)
drop := c2.DropOriginConfigs
drop := c2.DropOriginalConfigs
assert.NotEmpty(t, drop)
assert.Len(t, drop, 2)
assert.NotNil(t, drop["cpu"])
assert.Equal(t, []string{"time", "usage"}, drop["cpu"])
assert.NotNil(t, drop["foo"])
assert.Nil(t, drop["bar"])
assert.Equal(t, []string{"bar"}, drop["foo"])
assert.Len(t, drop, 3)
assert.True(t, drop["cpu_time"])
assert.True(t, drop["cpu_usage"])
assert.True(t, drop["foo_bar"])
}
8 changes: 3 additions & 5 deletions plugins/outputs/cloudwatch/testdata/drop_original.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ exporters:
awscloudwatch:
region: us-yeast-99
drop_original_metrics:
cpu:
- time
- usage
foo:
- bar
cpu_time: true
cpu_usage: true
foo_bar: true

service:
pipelines:
Expand Down
5 changes: 2 additions & 3 deletions translator/tocwconfig/sampleConfig/complete_linux_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ connectors: {}
exporters:
awscloudwatch:
drop_original_metrics:
cpu:
- cpu_usage_idle
- time_active
CPU_USAGE_IDLE: true
cpu_time_active: true
endpoint_override: https://monitoring-fips.us-west-2.amazonaws.com
force_flush_interval: 1m0s
max_datums_per_call: 1000
Expand Down
10 changes: 4 additions & 6 deletions translator/tocwconfig/sampleConfig/drop_origin_linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ connectors: {}
exporters:
awscloudwatch:
drop_original_metrics:
cpu:
- cpu_usage_idle
- time_active
nvidia_smi:
- utilization_gpu
- temperature_gpu
CPU_USAGE_IDLE: true
cpu_time_active: true
nvidia_smi_utilization_gpu: true
nvidia_smi_temperature_gpu: true
force_flush_interval: 1m0s
max_datums_per_call: 1000
max_values_per_datum: 150
Expand Down
6 changes: 0 additions & 6 deletions translator/tocwconfig/tocwconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,6 @@ func TestAdvancedConfig(t *testing.T) {
checkTranslation(t, "advanced_config_windows", "windows", expectedEnvVars, "")
}

func TestDropOriginConfig(t *testing.T) {
resetContext(t)
expectedEnvVars := map[string]string{}
checkTranslation(t, "drop_origin_linux", "linux", expectedEnvVars, "")
}

func TestLogOnlyConfig(t *testing.T) {
resetContext(t)
expectedEnvVars := map[string]string{}
Expand Down
6 changes: 6 additions & 0 deletions translator/tocwconfig/tocwconfig_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ func TestCompleteConfigUnix(t *testing.T) {
checkTranslation(t, "complete_linux_config", "linux", expectedEnvVars, "")
checkTranslation(t, "complete_darwin_config", "darwin", nil, "")
}

func TestDropOriginConfig(t *testing.T) {
resetContext(t)
expectedEnvVars := map[string]string{}
checkTranslation(t, "drop_origin_linux", "linux", expectedEnvVars, "")
}
2 changes: 2 additions & 0 deletions translator/translate/otel/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
CredentialsKey = "credentials"
RoleARNKey = "role_arn"
MetricsCollectionIntervalKey = "metrics_collection_interval"
MeasurementKey = "measurement"
DropOriginalMetricsKey = "drop_original_metrics"
ForceFlushIntervalKey = "force_flush_interval"
ContainerInsightsMetricGranularity = "metric_granularity"
PreferFullPodName = "prefer_full_pod_name"
Expand Down
Loading

0 comments on commit 6c2d68d

Please sign in to comment.