Skip to content

Commit

Permalink
added ScalarOID to config and began testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kuiperda committed Aug 21, 2023
1 parent 48b9fe5 commit 9e728c3
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 48 deletions.
18 changes: 11 additions & 7 deletions receiver/snmpreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var (
errMsgInvalidEndpointWError = `invalid endpoint '%s': must be in '[scheme]://[host]:[port]' format: %w`
errMsgInvalidEndpoint = `invalid endpoint '%s': must be in '[scheme]://[host]:[port]' format`
errMsgAttributeConfigNoEnumOIDOrPrefix = `attribute '%s' must contain one of either an enum, oid, or indexed_value_prefix`
errMsgResourceAttributeNoOIDOrPrefix = `resource_attribute '%s' must contain one of either an oid or indexed_value_prefix`
errMsgResourceAttributeNoOIDOrScalarOIDOrPrefix = `resource_attribute '%s' must contain one of either an oid or scalar_oid or indexed_value_prefix`
errMsgMetricNoUnit = `metric '%s' must have a unit`
errMsgMetricNoGaugeOrSum = `metric '%s' must have one of either a gauge or sum`
errMsgMetricNoOIDs = `metric '%s' must have one of either scalar_oids or indexed_oids`
Expand Down Expand Up @@ -133,17 +133,21 @@ 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 IndexedValuePrefix is 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"`
// IndexedValuePrefix is required only if OID is 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"`
// IndexedValuePrefix is required only if OID and scalarOID are not defined.
// This will be used alongside indexed metric values for this resource attribute. The prefix value concatenated with
// specific indexes of metric indexed values (Ex: prefix.1.2) will ultimately each be associated with a different "resource"
// as an attribute on that resource. The related indexed metric values will then be used to associate metric datapoints to
// those resources.
IndexedValuePrefix string `mapstructure:"indexed_value_prefix"` // required and valid if no oid field
IndexedValuePrefix string `mapstructure:"indexed_value_prefix"` // required and valid if no oid or scalar_oid field
}

// AttributeConfig contains config info about all of the metric attributes that will be used by this receiver.
Expand Down Expand Up @@ -562,10 +566,10 @@ func validateResourceAttributeConfigs(cfg *Config) error {
return nil
}

// Make sure each Resource Attribute has either an OID or IndexedValuePrefix
// Make sure each Resource Attribute has either an OID or scalarOID or IndexedValuePrefix
for attrName, attrCfg := range resourceAttributes {
if attrCfg.OID == "" && attrCfg.IndexedValuePrefix == "" {
combinedErr = multierr.Append(combinedErr, fmt.Errorf(errMsgResourceAttributeNoOIDOrPrefix, attrName))
if attrCfg.OID == "" && attrCfg.ScalarOID == "" && attrCfg.IndexedValuePrefix == "" {
combinedErr = multierr.Append(combinedErr, fmt.Errorf(errMsgResourceAttributeNoOIDOrScalarOIDOrPrefix, attrName))
}
}

Expand Down
41 changes: 32 additions & 9 deletions receiver/snmpreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ func getBaseResourceAttrConfig(attrType string) map[string]*ResourceAttributeCon
OID: "2",
},
}
case "scalar_oid":
return map[string]*ResourceAttributeConfig{
"ra1": {
ScalarOID: "0",
},
}
default:
return map[string]*ResourceAttributeConfig{
"ra1": {
Expand Down Expand Up @@ -480,11 +486,11 @@ func TestLoadConfigMetricConfigs(t *testing.T) {
},
}

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

expectedConfigComplexGood := factory.CreateDefaultConfig().(*Config)
expectedConfigComplexGood.ResourceAttributes = getBaseResourceAttrConfig("prefix")
Expand Down Expand Up @@ -684,6 +690,17 @@ func TestLoadConfigMetricConfigs(t *testing.T) {
},
}

expectedConfigScalarOIDPresentWithIndexedAttribute := factory.CreateDefaultConfig().(*Config)
expectedConfigScalarOIDPresentWithIndexedAttribute.Metrics = getBaseMetricConfig(true, false)
expectedConfigScalarOIDPresentWithIndexedAttribute.Attributes = getBaseAttrConfig("oid")
expectedConfigScalarOIDPresentWithIndexedAttribute.ResourceAttributes = getBaseResourceAttrConfig("scalar_oid")
expectedConfigScalarOIDPresentWithIndexedAttribute.Metrics["m3"].ColumnOIDs[0].Attributes = []Attribute{
{
Name: "a2",
},
}
expectedConfigScalarOIDPresentWithIndexedAttribute.Metrics["m3"].ColumnOIDs[0].ResourceAttributes = []string{"ra1"}

testCases := []testCase{
{
name: "NoMetricConfigsErrors",
Expand Down Expand Up @@ -818,17 +835,23 @@ func TestLoadConfigMetricConfigs(t *testing.T) {
expectedErr: fmt.Sprintf(errMsgColumnIndexedAttributeRequired, "m3"),
},
{
name: "NoResourceAttributeConfigOIDOrPrefixErrors",
nameVal: "no_resource_attribute_oid_or_prefix",
expectedCfg: expectedConfigNoResourceAttributeOIDOrPrefix,
expectedErr: fmt.Sprintf(errMsgResourceAttributeNoOIDOrPrefix, "ra1"),
name: "NoResourceAttributeConfigOIDOrScalarOIDOrPrefixErrors",
nameVal: "no_resource_attribute_oid_or_scalar_oid_or_prefix",
expectedCfg: expectedConfigNoResourceAttributeOIDOrScalarOIDOrPrefix,
expectedErr: fmt.Sprintf(errMsgResourceAttributeNoOIDOrScalarOIDOrPrefix, "ra1"),
},
{
name: "ComplexConfigGood",
nameVal: "complex_good",
expectedCfg: expectedConfigComplexGood,
expectedErr: "",
},
{
name: "ScalarOIDPresentWithIndexedAttribute",
nameVal: "scalar_oid_present_with_indexed_attribute",
expectedCfg: expectedConfigScalarOIDPresentWithIndexedAttribute,
expectedErr: "",
},
}

for _, test := range testCases {
Expand Down
Loading

0 comments on commit 9e728c3

Please sign in to comment.