Skip to content

Commit

Permalink
Turn tag/mapping conflict into an actual error.
Browse files Browse the repository at this point in the history
  • Loading branch information
dplepage-dd committed Dec 2, 2024
1 parent be4b703 commit 206b60c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"fmt"
"regexp"

"github.com/DataDog/datadog-agent/pkg/util/log"

"github.com/DataDog/datadog-agent/pkg/networkdevice/profile/profiledefinition"
)

Expand Down Expand Up @@ -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 != "") {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@
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"
)

Expand All @@ -32,7 +26,6 @@ func Test_ValidateEnrichMetrics(t *testing.T) {
metrics []profiledefinition.MetricsConfig
expectedErrors []string
expectedMetrics []profiledefinition.MetricsConfig
expectedLogs []logCount
}{
{
name: "either table symbol or scalar symbol must be provided",
Expand Down Expand Up @@ -574,23 +567,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 {
Expand All @@ -599,13 +580,6 @@ 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)
}
})
}
}
Expand All @@ -621,7 +595,6 @@ func Test_ValidateEnrichMetricTags(t *testing.T) {
metrics []profiledefinition.MetricTagConfig
expectedErrors []string
expectedMetrics []profiledefinition.MetricTagConfig
expectedLogs []logCount
}{
{
name: "Move OID to Symbol",
Expand Down Expand Up @@ -691,12 +664,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 {
Expand All @@ -705,13 +672,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)
}
})
}
}
Expand Down

0 comments on commit 206b60c

Please sign in to comment.