From 7cda86a8f9ba98b88c7ade3035b4c918f11118e9 Mon Sep 17 00:00:00 2001 From: Daniel Lepage Date: Mon, 2 Dec 2024 14:22:26 -0500 Subject: [PATCH] Turn missing "tag" field into an error. --- .../config_validate_enrich.go | 5 +- .../config_validate_enrich_test.go | 54 +------------------ 2 files changed, 4 insertions(+), 55 deletions(-) diff --git a/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich.go b/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich.go index 5268345d8744e..499b93287d9d1 100644 --- a/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich.go +++ b/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich.go @@ -10,8 +10,6 @@ import ( "fmt" "regexp" - "github.com/DataDog/datadog-agent/pkg/util/log" - "github.com/DataDog/datadog-agent/pkg/networkdevice/profile/profiledefinition" ) @@ -174,6 +172,7 @@ func validateEnrichSymbol(symbol *profiledefinition.SymbolConfig, symbolContext } return errors } + func validateEnrichMetricTag(metricTag *profiledefinition.MetricTagConfig) []string { var errors []string if (metricTag.Column.OID != "" || metricTag.Column.Name != "") && (metricTag.Symbol.OID != "" || metricTag.Symbol.Name != "") { @@ -217,7 +216,7 @@ func validateEnrichMetricTag(metricTag *profiledefinition.MetricTagConfig) []str } } if len(metricTag.Mapping) > 0 && metricTag.Tag == "" { - log.Warnf("``tag` must be provided if `mapping` (`%s`) is defined", metricTag.Mapping) + errors = append(errors, fmt.Sprintf("``tag` must be provided if `mapping` (`%s`) is defined", metricTag.Mapping)) } for _, transform := range metricTag.IndexTransform { if transform.Start > transform.End { diff --git a/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich_test.go b/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich_test.go index 0047af78cc366..1e966e743614b 100644 --- a/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich_test.go +++ b/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich_test.go @@ -6,33 +6,21 @@ package configvalidation import ( - "bufio" - "bytes" "fmt" "regexp" - "strings" "testing" - "github.com/cihub/seelog" "github.com/stretchr/testify/assert" - "github.com/DataDog/datadog-agent/pkg/util/log" - "github.com/DataDog/datadog-agent/pkg/networkdevice/profile/profiledefinition" ) func Test_ValidateEnrichMetrics(t *testing.T) { - type logCount struct { - log string - count int - } - tests := []struct { name string metrics []profiledefinition.MetricsConfig expectedErrors []string expectedMetrics []profiledefinition.MetricsConfig - expectedLogs []logCount }{ { name: "either table symbol or scalar symbol must be provided", @@ -551,7 +539,7 @@ func Test_ValidateEnrichMetrics(t *testing.T) { }, }, { - name: "mapping used without tag should raise a warning", + name: "mapping used without tag", metrics: []profiledefinition.MetricsConfig{ { Symbols: []profiledefinition.SymbolConfig{ @@ -574,23 +562,11 @@ func Test_ValidateEnrichMetrics(t *testing.T) { }, }, }, - expectedErrors: []string{}, - expectedLogs: []logCount{ - { - "[WARN] validateEnrichMetricTag: ``tag` must be provided if `mapping` (`map[1:abc 2:def]`) is defined", - 1, - }, - }, + expectedErrors: []string{"``tag` must be provided if `mapping` (`map[1:abc 2:def]`) is defined"}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - var b bytes.Buffer - w := bufio.NewWriter(&b) - l, err := seelog.LoggerFromWriterWithMinLevelAndFormat(w, seelog.DebugLvl, "[%LEVEL] %FuncShort: %Msg") - assert.Nil(t, err) - log.SetupLogger(l, "debug") - errors := ValidateEnrichMetrics(tt.metrics) assert.Equal(t, len(tt.expectedErrors), len(errors), fmt.Sprintf("ERRORS: %v", errors)) for i := range errors { @@ -599,29 +575,16 @@ func Test_ValidateEnrichMetrics(t *testing.T) { if tt.expectedMetrics != nil { assert.Equal(t, tt.expectedMetrics, tt.metrics) } - - w.Flush() - logs := b.String() - - for _, aLogCount := range tt.expectedLogs { - assert.Equal(t, aLogCount.count, strings.Count(logs, aLogCount.log), logs) - } }) } } func Test_ValidateEnrichMetricTags(t *testing.T) { - type logCount struct { - log string - count int - } - tests := []struct { name string metrics []profiledefinition.MetricTagConfig expectedErrors []string expectedMetrics []profiledefinition.MetricTagConfig - expectedLogs []logCount }{ { name: "Move OID to Symbol", @@ -691,12 +654,6 @@ func Test_ValidateEnrichMetricTags(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - var b bytes.Buffer - w := bufio.NewWriter(&b) - l, err := seelog.LoggerFromWriterWithMinLevelAndFormat(w, seelog.DebugLvl, "[%LEVEL] %FuncShort: %Msg") - assert.Nil(t, err) - log.SetupLogger(l, "debug") - errors := ValidateEnrichMetricTags(tt.metrics) assert.Equal(t, len(tt.expectedErrors), len(errors), fmt.Sprintf("ERRORS: %v", errors)) for i := range errors { @@ -705,13 +662,6 @@ func Test_ValidateEnrichMetricTags(t *testing.T) { if tt.expectedMetrics != nil { assert.Equal(t, tt.expectedMetrics, tt.metrics) } - - w.Flush() - logs := b.String() - - for _, aLogCount := range tt.expectedLogs { - assert.Equal(t, aLogCount.count, strings.Count(logs, aLogCount.log), logs) - } }) } }