diff --git a/exporter/signalfxexporter/go.mod b/exporter/signalfxexporter/go.mod index a461b68a504e..fe3040ca16b3 100644 --- a/exporter/signalfxexporter/go.mod +++ b/exporter/signalfxexporter/go.mod @@ -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 diff --git a/exporter/signalfxexporter/internal/utils/histogram_utils_test.go b/exporter/signalfxexporter/internal/utils/histogram_utils_test.go index e47a1f55b4d1..03d7f366a2c9 100644 --- a/exporter/signalfxexporter/internal/utils/histogram_utils_test.go +++ b/exporter/signalfxexporter/internal/utils/histogram_utils_test.go @@ -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) { @@ -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, @@ -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 }}, { @@ -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 @@ -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 @@ -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) + } }) } }