-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
152 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
plugins/processors/awsapplicationsignals/internal/prune/metric_pruner.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package prune | ||
|
||
import ( | ||
"errors" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
|
||
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/common" | ||
) | ||
|
||
type MetricPruner struct { | ||
} | ||
|
||
func (p *MetricPruner) ShouldBeDropped(attributes pcommon.Map) (bool, error) { | ||
for _, attributeKey := range common.IndexableMetricAttributes { | ||
if val, ok := attributes.Get(attributeKey); ok { | ||
if !isAsciiPrintable(val.Str()) { | ||
return true, errors.New("Metric attribute " + attributeKey + " must contain only ASCII characters.") | ||
} | ||
} | ||
} | ||
return false, nil | ||
} | ||
|
||
func NewPruner() *MetricPruner { | ||
return &MetricPruner{} | ||
} | ||
|
||
func isAsciiPrintable(val string) bool { | ||
nonWhitespaceFound := false | ||
for _, c := range val { | ||
if c < 32 || c > 126 { | ||
return false | ||
} else if !nonWhitespaceFound && c != 32 { | ||
nonWhitespaceFound = true | ||
} | ||
} | ||
return nonWhitespaceFound | ||
} |
85 changes: 85 additions & 0 deletions
85
plugins/processors/awsapplicationsignals/internal/prune/metric_pruner_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package prune | ||
|
||
import ( | ||
"testing" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
|
||
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/common" | ||
) | ||
|
||
func TestMetricPrunerWithIndexableAttribute(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
val string | ||
want bool | ||
}{ | ||
{ | ||
"testShouldDropChineseChar", | ||
"漢", | ||
true, | ||
}, { | ||
"testShouldDropSymbolChar", | ||
"€, £, µ", | ||
true, | ||
}, { | ||
"testShouldDropAllBlackSpace", | ||
" ", | ||
true, | ||
}, | ||
{ | ||
"testShouldDropAllTab", | ||
" ", | ||
true, | ||
}, { | ||
"testShouldKeepEnglishWord", | ||
"abcdefg-", | ||
false, | ||
}, | ||
} | ||
|
||
p := &MetricPruner{} | ||
for _, tt := range tests { | ||
attributes := pcommon.NewMap() | ||
attributes.PutStr(common.MetricAttributeLocalService, tt.val) | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, _ := p.ShouldBeDropped(attributes) | ||
if got != tt.want { | ||
t.Errorf("ShouldBeDropped() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestMetricPrunerWithNonIndexableAttribute(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
val string | ||
want bool | ||
}{ | ||
{ | ||
"testShouldKeepChineseChar", | ||
"漢", | ||
false, | ||
}, { | ||
"testShouldKeepEnglishWord", | ||
"abcdefg-", | ||
false, | ||
}, | ||
} | ||
|
||
p := &MetricPruner{} | ||
for _, tt := range tests { | ||
attributes := pcommon.NewMap() | ||
attributes.PutStr(common.AttributeEC2InstanceId, tt.val) | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, _ := p.ShouldBeDropped(attributes) | ||
if got != tt.want { | ||
t.Errorf("ShouldBeDropped() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters