Skip to content

Commit

Permalink
test that resource attribute (scalar/column oid) end in correct digit
Browse files Browse the repository at this point in the history
  • Loading branch information
kuiperda committed Aug 21, 2023
1 parent c4af256 commit 4b7532c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
19 changes: 16 additions & 3 deletions receiver/snmpreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ var (
errMsgColumnResourceAttributeBadName = `metric '%s' column_oid resource_attribute '%s' must match a resource_attribute config`
errMsgColumnIndexedIdentifierRequired = `metric '%s' column_oid must either have an indexed resource_attribute or an indexed_value_prefix/oid attribute`
errMsgMultipleKeysSetOnResourceAttribute = `resource attribute %s has too many keys set, must have only one of oid, scalar_oid, or indexed_value_prefix`
errScalarOIDResourceAttributeEndsInNonzeroDigit = `resource attribute %s has key scalar_oid %s that ends in a nonzero digit (scalar oids should not be indexed)`
errColumnOIDResourceAttributeEndsInZero = `resource attribute %s has key oid %s that ends in a zero (column oids should be indexed)`

// Config errors
errEmptyEndpoint = errors.New("endpoint must be specified")
Expand Down Expand Up @@ -134,12 +136,12 @@ type Config struct {
type ResourceAttributeConfig struct {
// Description is optional and describes what the resource attribute represents
Description string `mapstructure:"description"`
// OID is required only if scalarOID and IndexedValuePrefix are not defined.
// OID is required only if ScalarOID and IndexedValuePrefix are not defined.
// This is the column OID which will provide indexed values to be used for this resource attribute. These indexed values
// will ultimately each be associated with a different "resource" as an attribute on that resource. Indexed metric values
// will then be used to associate metric datapoints to the matching "resource" (based on matching indexes).
OID string `mapstructure:"oid"`
// scalarOID is required only if OID and IndexedValuePrefix are not defined.
// ScalarOID is required only if OID and IndexedValuePrefix are not defined.
// This is the scalar OID which will provide a value to be used for this resource attribute.
// Single or indexed metrics can then be associated with the resource. (Indexed metrics also need an indexed attribute or resource attribute to associate with a scalar metric resource attribute)
ScalarOID string `mapstructure:"scalar_oid"`
Expand Down Expand Up @@ -577,10 +579,21 @@ func validateResourceAttributeConfigs(cfg *Config) error {
return nil
}

// Make sure each Resource Attribute has either an OID or scalarOID or IndexedValuePrefix
// Make sure each Resource Attribute has either an OID or ScalarOID or IndexedValuePrefix
for attrName, attrCfg := range resourceAttributes {
if attrCfg.OID == "" && attrCfg.ScalarOID == "" && attrCfg.IndexedValuePrefix == "" {
combinedErr = multierr.Append(combinedErr, fmt.Errorf(errMsgResourceAttributeNoOIDOrScalarOIDOrPrefix, attrName))
continue
}
// Check if scalar and column oid resource attribute values end in the right digit
if attrCfg.ScalarOID != "" {
nums := strings.Split(attrCfg.ScalarOID, ".")
if nums[len(nums) - 1] != "0" {combinedErr = multierr.Append(combinedErr, fmt.Errorf(errScalarOIDResourceAttributeEndsInNonzeroDigit, attrName, attrCfg.ScalarOID))}
continue
}
if attrCfg.OID != "" {
nums := strings.Split(attrCfg.OID, ".")
if nums[len(nums) - 1] == "0" {combinedErr = multierr.Append(combinedErr, fmt.Errorf(errColumnOIDResourceAttributeEndsInZero, attrName, attrCfg.OID))}
}
}

Expand Down
25 changes: 25 additions & 0 deletions receiver/snmpreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,19 @@ func TestLoadConfigMetricConfigs(t *testing.T) {
expectedConfigMultipleKeysOnResourceAttribute.ResourceAttributes["ra1"] = &ResourceAttributeConfig{ScalarOID: "0", OID: "1"}
expectedConfigMultipleKeysOnResourceAttribute.Metrics["m3"].ColumnOIDs[0].ResourceAttributes = []string{"ra1"}

expectedConfigScalarOIDResourceAttributeEndsInNonzeroDigit := factory.CreateDefaultConfig().(*Config)
expectedConfigScalarOIDResourceAttributeEndsInNonzeroDigit.Metrics = getBaseMetricConfig(true, false)
expectedConfigScalarOIDResourceAttributeEndsInNonzeroDigit.ResourceAttributes = getBaseResourceAttrConfig("scalar_oid")
expectedConfigScalarOIDResourceAttributeEndsInNonzeroDigit.ResourceAttributes["ra1"] = &ResourceAttributeConfig{ScalarOID: "1"}
expectedConfigScalarOIDResourceAttributeEndsInNonzeroDigit.ResourceAttributes["ra2"] = &ResourceAttributeConfig{OID: "1"}
expectedConfigScalarOIDResourceAttributeEndsInNonzeroDigit.Metrics["m3"].ColumnOIDs[0].ResourceAttributes = []string{"ra1", "ra2"}

expectedConfigColumnOIDResourceAttributeEndsInZero := factory.CreateDefaultConfig().(*Config)
expectedConfigColumnOIDResourceAttributeEndsInZero.Metrics = getBaseMetricConfig(true, false)
expectedConfigColumnOIDResourceAttributeEndsInZero.ResourceAttributes = getBaseResourceAttrConfig("oid")
expectedConfigColumnOIDResourceAttributeEndsInZero.ResourceAttributes["ra1"] = &ResourceAttributeConfig{OID: "0"}
expectedConfigColumnOIDResourceAttributeEndsInZero.Metrics["m3"].ColumnOIDs[0].ResourceAttributes = []string{"ra1"}

testCases := []testCase{
{
name: "NoMetricConfigsErrors",
Expand Down Expand Up @@ -916,6 +929,18 @@ func TestLoadConfigMetricConfigs(t *testing.T) {
expectedCfg: expectedConfigMultipleKeysOnResourceAttribute,
expectedErr: fmt.Sprintf(errMsgMultipleKeysSetOnResourceAttribute, "ra1"),
},
{
name: "ScalarOIDResourceAttributeEndsInNonzeroDigit",
nameVal: "scalar_oid_resource_attribute_ends_in_nonzero_digit",
expectedCfg: expectedConfigScalarOIDResourceAttributeEndsInNonzeroDigit,
expectedErr: fmt.Sprintf(errScalarOIDResourceAttributeEndsInNonzeroDigit, "ra1", "1"),
},
{
name: "ColumnOIDResourceAttributeEndsInZero",
nameVal: "column_oid_resource_attribute_ends_in_zero",
expectedCfg: expectedConfigColumnOIDResourceAttributeEndsInZero,
expectedErr: fmt.Sprintf(errColumnOIDResourceAttributeEndsInZero, "ra1", "0"),
},
}

for _, test := range testCases {
Expand Down
37 changes: 37 additions & 0 deletions receiver/snmpreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,40 @@ snmp/multiple_keys_on_resource_attribute:
- oid: "1"
resource_attributes:
- ra1
snmp/scalar_oid_resource_attribute_ends_in_nonzero_digit:
collection_interval: 10s
endpoint: udp://localhost:161
version: v2c
community: public
resource_attributes:
ra1:
scalar_oid: "1"
ra2:
oid: "1"
metrics:
m3:
unit: "By"
gauge:
value_type: "double"
column_oids:
- oid: "1"
resource_attributes:
- ra1
- ra2
snmp/column_oid_resource_attribute_ends_in_zero:
collection_interval: 10s
endpoint: udp://localhost:161
version: v2c
community: public
resource_attributes:
ra1:
oid: "0"
metrics:
m3:
unit: "By"
gauge:
value_type: "double"
column_oids:
- oid: "1"
resource_attributes:
- ra1

0 comments on commit 4b7532c

Please sign in to comment.