Skip to content

Commit 48bf341

Browse files
authored
chore(tests): pretty print actual metrics (#897)
Enhance EXPECT_METRICS_{DONT_}_EXIST by pretty printing actual metrics on test failure.
1 parent d76a34b commit 48bf341

File tree

1 file changed

+10
-6
lines changed
  • daemon/internal/newrelic/integration

1 file changed

+10
-6
lines changed

daemon/internal/newrelic/integration/test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -686,19 +686,23 @@ func (t *Test) Compare(harvest *newrelic.Harvest) {
686686
if nil != t.metricsExist {
687687
for _, name := range strings.Split(strings.TrimSpace(string(t.subEnvVars(t.metricsExist))), "\n") {
688688
name = strings.TrimSpace(name)
689-
actual := strings.Replace(name, "__FILE__", t.Path, -1)
690-
if !harvest.Metrics.Has(actual) {
691-
t.Fail(fmt.Errorf("metric does not exist: %s\n\nactual metric table: %s", actual, harvest.Metrics.DebugJSON()))
689+
expected := strings.Replace(name, "__FILE__", t.Path, -1)
690+
if !harvest.Metrics.Has(expected) {
691+
actualPretty := bytes.Buffer{}
692+
json.Indent(&actualPretty, []byte(harvest.Metrics.DebugJSON()), "", " ")
693+
t.Fail(fmt.Errorf("metric does not exist: %s\n\nactual metric table: %s", expected, actualPretty.String()))
692694
}
693695
}
694696
}
695697

696698
if nil != t.metricsDontExist {
697699
for _, name := range strings.Split(strings.TrimSpace(string(t.subEnvVars(t.metricsDontExist))), "\n") {
698700
name = strings.TrimSpace(name)
699-
actual := strings.Replace(name, "__FILE__", t.Path, -1)
700-
if harvest.Metrics.Has(actual) {
701-
t.Fail(fmt.Errorf("unexpected metric in harvest: %s\n\nactual metric table: %s", actual, harvest.Metrics.DebugJSON()))
701+
expected := strings.Replace(name, "__FILE__", t.Path, -1)
702+
if harvest.Metrics.Has(expected) {
703+
actualPretty := bytes.Buffer{}
704+
json.Indent(&actualPretty, []byte(harvest.Metrics.DebugJSON()), "", " ")
705+
t.Fail(fmt.Errorf("unexpected metric in harvest: %s\n\nactual metric table: %s", expected, actualPretty.String()))
702706
}
703707
}
704708
}

0 commit comments

Comments
 (0)