Skip to content

Commit

Permalink
[chore][exporter/signalfx] fix tests (#31522)
Browse files Browse the repository at this point in the history
Update test cases to ignore the order of resource/scope metrics since
the returned histograms-only pmetrics can have different ordering for
resource and scope metrics

Fixes
#31493
  • Loading branch information
jinja2 authored Mar 1, 2024
1 parent 5f63c2c commit 3401d8a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 34 deletions.
1 change: 1 addition & 0 deletions exporter/signalfxexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr v0.95.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata v0.95.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden v0.95.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.95.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/signalfx v0.95.0
github.com/shirou/gopsutil/v3 v3.24.1
github.com/signalfx/com_signalfx_metrics_protobuf v0.0.3
Expand Down
95 changes: 61 additions & 34 deletions exporter/signalfxexporter/internal/utils/histogram_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
)

func initMetric(m pmetric.Metric, name string, ty pmetric.MetricType) {
Expand Down Expand Up @@ -168,30 +170,30 @@ func TestHistogramsAreRetrieved(t *testing.T) {
res.Attributes().PutStr("kr0", "vr0")
rm.ScopeMetrics().AppendEmpty()
ilm0 := rm.ScopeMetrics().At(0)
ilm0.SetSchemaUrl("Scope SchemaUrl")
ilm0.SetSchemaUrl("Scope SchemaUrl s0")
ilm0.Scope().Attributes().PutStr("ks0", "vs0")
ilm0.Scope().SetName("Scope name")
ilm0.Scope().SetVersion("Scope version")
ilm0.Scope().SetName("Scope name s0")
ilm0.Scope().SetVersion("Scope version s0")
ilm0.Metrics().EnsureCapacity(2)
ilm0.Metrics().AppendEmpty()
buildHistogram(ilm0.Metrics().At(0), "histogram_1", ts, 1)
buildHistogram(ilm0.Metrics().At(0), "histogram_1_s0", ts, 1)
ilm0.Metrics().AppendEmpty()
buildGauge(ilm0.Metrics().At(1), "gauge", ts, 2)
buildGauge(ilm0.Metrics().At(1), "gauge_s0", ts, 2)

rm.ScopeMetrics().AppendEmpty()
ilm1 := rm.ScopeMetrics().At(1)
ilm1.Metrics().AppendEmpty()
buildSum(ilm1.Metrics().At(0), "gauge", ts, 2)
buildSum(ilm1.Metrics().At(0), "gauge_s1", ts, 2)

rm.ScopeMetrics().AppendEmpty()
ilm2 := rm.ScopeMetrics().At(2)
ilm2.SetSchemaUrl("Scope SchemaUrl")
ilm2.Scope().Attributes().PutStr("ks0", "vs0")
ilm2.SetSchemaUrl("Scope SchemaUrl s2")
ilm2.Scope().Attributes().PutStr("ks2", "vs2")
ilm2.Metrics().EnsureCapacity(2)
ilm2.Metrics().AppendEmpty()
buildHistogram(ilm2.Metrics().At(0), "histogram_1", ts, 1)
buildHistogram(ilm2.Metrics().At(0), "histogram_1_s2", ts, 1)
ilm2.Metrics().AppendEmpty()
buildHistogram(ilm2.Metrics().At(1), "histogram_2", ts, 2)
buildHistogram(ilm2.Metrics().At(1), "histogram_2_s2", ts, 2)
return out
},
wantMetricCount: 3,
Expand All @@ -202,71 +204,93 @@ func TestHistogramsAreRetrieved(t *testing.T) {
res.Attributes().PutStr("kr0", "vr0")
rm.ScopeMetrics().AppendEmpty()
ilm0 := rm.ScopeMetrics().At(0)
ilm0.SetSchemaUrl("Scope SchemaUrl")
ilm0.SetSchemaUrl("Scope SchemaUrl s0")
ilm0.Scope().Attributes().PutStr("ks0", "vs0")
ilm0.Scope().SetName("Scope name")
ilm0.Scope().SetVersion("Scope version")
buildHistogram(ilm0.Metrics().AppendEmpty(), "histogram_1", ts, 1)
ilm0.Scope().SetName("Scope name s0")
ilm0.Scope().SetVersion("Scope version s0")
buildHistogram(ilm0.Metrics().AppendEmpty(), "histogram_1_s0", ts, 1)

rm.ScopeMetrics().AppendEmpty()
ilm1 := rm.ScopeMetrics().At(1)
ilm1.SetSchemaUrl("Scope SchemaUrl")
ilm1.Scope().Attributes().PutStr("ks0", "vs0")
ilm1.SetSchemaUrl("Scope SchemaUrl s2")
ilm1.Scope().Attributes().PutStr("ks2", "vs2")
ilm1.Metrics().EnsureCapacity(2)
ilm1.Metrics().AppendEmpty()
buildHistogram(ilm1.Metrics().At(0), "histogram_1", ts, 1)
buildHistogram(ilm1.Metrics().At(0), "histogram_1_s2", ts, 1)
ilm1.Metrics().AppendEmpty()
buildHistogram(ilm1.Metrics().At(1), "histogram_2", ts, 2)
buildHistogram(ilm1.Metrics().At(1), "histogram_2_s2", ts, 2)
return out
}},
{
name: "mixed_type_multiple_resources",
inMetricsFunc: func() pmetric.Metrics {
out := pmetric.NewMetrics()
out.ResourceMetrics().EnsureCapacity(2)
out.ResourceMetrics().EnsureCapacity(3)
out.ResourceMetrics().AppendEmpty()
rm0 := out.ResourceMetrics().At(0)
rm0.SetSchemaUrl("Resource SchemaUrl")
rm0.SetSchemaUrl("Resource SchemaUrl r0")
rm0.Resource().Attributes().PutStr("kr0", "vr0")
rm0.ScopeMetrics().AppendEmpty()
ilm0r0 := rm0.ScopeMetrics().At(0)
ilm0r0.SetSchemaUrl("Scope SchemaUrl")
ilm0r0.SetSchemaUrl("Scope SchemaUrl s0")
ilm0r0.Scope().Attributes().PutStr("ks0", "vs0")
ilm0r0.Metrics().EnsureCapacity(2)
ilm0r0.Metrics().AppendEmpty()
buildHistogram(ilm0r0.Metrics().At(0), "histogram_1", ts, 1)
buildHistogram(ilm0r0.Metrics().At(0), "histogram_1_s0_r0", ts, 1)
ilm0r0.Metrics().AppendEmpty()
buildGauge(ilm0r0.Metrics().At(1), "gauge", ts, 1)
buildGauge(ilm0r0.Metrics().At(1), "gauge_s0_r0", ts, 1)
rm0.ScopeMetrics().AppendEmpty()
ilm1r0 := rm0.ScopeMetrics().At(1)
ilm1r0.Metrics().AppendEmpty()
buildGauge(ilm1r0.Metrics().At(0), "gauge", ts, 1)
buildGauge(ilm1r0.Metrics().At(0), "gauge_s1_r0", ts, 1)

out.ResourceMetrics().AppendEmpty()
rm1 := out.ResourceMetrics().At(1)
rm1.Resource().Attributes().PutStr("kr1", "vr1")
ilm0r1 := rm1.ScopeMetrics().AppendEmpty()
ilm0r1.SetSchemaUrl("Scope SchemaUrl")
ilm0r1.SetSchemaUrl("Scope SchemaUrl s0")
ilm0r1.Scope().Attributes().PutStr("ks0", "vs0")
ilm0r1.Metrics().AppendEmpty()
buildGauge(ilm0r1.Metrics().At(0), "gauge", ts, 1)
buildGauge(ilm0r1.Metrics().At(0), "gauge_s0_r1", ts, 1)

out.ResourceMetrics().AppendEmpty()
rm2 := out.ResourceMetrics().At(2)
rm2.Resource().Attributes().PutStr("kr2", "vr2")
ilm0r2 := rm2.ScopeMetrics().AppendEmpty()
ilm0r2.SetSchemaUrl("Scope SchemaUrl s0")
ilm0r2.Scope().Attributes().PutStr("ks0", "vs0")
ilm0r2.Metrics().AppendEmpty()
ilm0r2.Metrics().EnsureCapacity(2)
buildGauge(ilm0r2.Metrics().At(0), "gauge_s0_r2", ts, 1)
ilm0r2.Metrics().AppendEmpty()
buildHistogram(ilm0r2.Metrics().At(1), "histogram_s0_r2", ts, 1)

return out
},
wantMetricCount: 1,
wantMetricCount: 2,
wantMetrics: func() pmetric.Metrics {
out := pmetric.NewMetrics()
out.ResourceMetrics().AppendEmpty()
rm := out.ResourceMetrics().At(0)
rm.SetSchemaUrl("Resource SchemaUrl")
rm.SetSchemaUrl("Resource SchemaUrl r0")
rm.Resource().Attributes().PutStr("kr0", "vr0")
rm.ScopeMetrics().AppendEmpty()
ilm0 := rm.ScopeMetrics().At(0)
ilm0.SetSchemaUrl("Scope SchemaUrl")
ilm0.SetSchemaUrl("Scope SchemaUrl s0")
ilm0.Scope().Attributes().PutStr("ks0", "vs0")
ilm0.Metrics().EnsureCapacity(1)
ilm0.Metrics().AppendEmpty()
buildHistogram(ilm0.Metrics().At(0), "histogram_1", ts, 1)
buildHistogram(ilm0.Metrics().At(0), "histogram_1_s0_r0", ts, 1)

out.ResourceMetrics().AppendEmpty()
rm1 := out.ResourceMetrics().At(1)
rm1.Resource().Attributes().PutStr("kr2", "vr2")
ilm0r1 := rm1.ScopeMetrics().AppendEmpty()
ilm0r1.SetSchemaUrl("Scope SchemaUrl s0")
ilm0r1.Scope().Attributes().PutStr("ks0", "vs0")
ilm0r1.Metrics().AppendEmpty()
buildHistogram(ilm0r1.Metrics().At(0), "histogram_s0_r2", ts, 1)

return out
}},
{
Expand All @@ -278,7 +302,6 @@ func TestHistogramsAreRetrieved(t *testing.T) {
res.Attributes().PutStr("kr0", "vr0")
res.Attributes().PutStr("com.splunk.signalfx.access_token", "abcd")
ilms := rm.ScopeMetrics()
ilms.EnsureCapacity(3)
ilm := ilms.AppendEmpty()
buildHistogram(ilm.Metrics().AppendEmpty(), "histogram_1", ts, 1)
return out
Expand All @@ -290,7 +313,6 @@ func TestHistogramsAreRetrieved(t *testing.T) {
res := rm.Resource()
res.Attributes().PutStr("kr0", "vr0")
ilms := rm.ScopeMetrics()
ilms.EnsureCapacity(3)
ilm := ilms.AppendEmpty()
buildHistogram(ilm.Metrics().AppendEmpty(), "histogram_1", ts, 1)
return out
Expand All @@ -302,9 +324,14 @@ func TestHistogramsAreRetrieved(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
md := tt.inMetricsFunc()
gotMetrics, gotCount := GetHistograms(md)

assert.Equal(t, tt.wantMetricCount, gotCount)
assert.Equal(t, tt.wantMetrics(), gotMetrics)
if tt.wantMetricCount == 0 {
assert.Equal(t, tt.wantMetrics(), gotMetrics)
} else {
err := pmetrictest.CompareMetrics(tt.wantMetrics(), gotMetrics,
pmetrictest.IgnoreResourceMetricsOrder(), pmetrictest.IgnoreScopeMetricsOrder())
assert.NoError(t, err)
}
})
}
}

0 comments on commit 3401d8a

Please sign in to comment.