Skip to content

Commit

Permalink
Reduce logging for known non-convertible JMX metric units. (#1244)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefchien authored Jul 10, 2024
1 parent ccd6933 commit b348881
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
19 changes: 19 additions & 0 deletions internal/cloudwatch/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"

"github.com/aws/amazon-cloudwatch-agent/internal/util/collections"
"github.com/aws/amazon-cloudwatch-agent/internal/util/unit"
)

Expand Down Expand Up @@ -72,6 +73,20 @@ var scaledBaseUnits = map[types.StandardUnit]map[unit.MetricPrefix]types.Standar
},
}

var knownNonConvertibleUnits = collections.NewSet(
// JMX/Tomcat units
"errors",
"threads",
"requests",
// JMX/Kafka units
"{messages}",
"{requests}",
"{partitions}",
"{operations}",
"{controllers}",
"{elections}",
)

// ToStandardUnit converts from the OTEL unit names to the corresponding names
// supported by AWS CloudWatch. Some OTEL unit types are unsupported.
func ToStandardUnit(unit string) (string, float64, error) {
Expand All @@ -81,6 +96,10 @@ func ToStandardUnit(unit string) (string, float64, error) {

func toStandardUnit(unit string) (types.StandardUnit, float64, error) {
u := strings.ToLower(unit)
// silently convert to None
if knownNonConvertibleUnits.Contains(u) {
return types.StandardUnitNone, 1, nil
}
if standardUnit, ok := standardUnits[u]; ok {
return standardUnit, 1, nil
}
Expand Down
10 changes: 10 additions & 0 deletions internal/cloudwatch/unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,13 @@ func TestScaledUnits(t *testing.T) {
assert.Equal(t, testCase.scale, scale)
}
}

func TestKnownNonStandardUnits(t *testing.T) {
testCases := []string{"errors", "{requests}"}
for _, testCase := range testCases {
unit, scale, err := ToStandardUnit(testCase)
assert.NoError(t, err)
assert.Equal(t, "None", unit)
assert.EqualValues(t, 1, scale)
}
}

0 comments on commit b348881

Please sign in to comment.