Skip to content

Commit

Permalink
Turn missing "tag" field into an 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 7cda86a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 55 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,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",
Expand Down Expand Up @@ -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{
Expand All @@ -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 {
Expand All @@ -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",
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
})
}
}
Expand Down

0 comments on commit 7cda86a

Please sign in to comment.