From 0226fa2de302dcee4b15ba60aa0702d4bf042cbf Mon Sep 17 00:00:00 2001 From: Dan Jaglowski Date: Wed, 13 Nov 2024 12:58:09 -0700 Subject: [PATCH] [chore][pmetricutiltest] Use native structs in options --- .../internal/pmetricutil/metrics_test.go | 78 ++++++++++++++----- .../internal/pmetricutiltest/metrics.go | 76 ++++++++---------- .../internal/pmetricutiltest/metrics_test.go | 77 +++++++----------- connector/routingconnector/metrics_test.go | 19 +++-- 4 files changed, 133 insertions(+), 117 deletions(-) diff --git a/connector/routingconnector/internal/pmetricutil/metrics_test.go b/connector/routingconnector/internal/pmetricutil/metrics_test.go index 8c23b4232246..1eff9bc21201 100644 --- a/connector/routingconnector/internal/pmetricutil/metrics_test.go +++ b/connector/routingconnector/internal/pmetricutil/metrics_test.go @@ -130,12 +130,21 @@ func TestMoveMetricsWithContextIf(t *testing.T) { from: pmetricutiltest.NewMetrics("AB", "CD", "EF", "GH"), to: pmetric.NewMetrics(), expectFrom: pmetricutiltest.NewMetricsFromOpts( - pmetricutiltest.WithResource('A', - pmetricutiltest.WithScope('C', pmetricutiltest.WithMetric('E', "GH"), pmetricutiltest.WithMetric('F', "GH")), - pmetricutiltest.WithScope('D', pmetricutiltest.WithMetric('E', "GH"), pmetricutiltest.WithMetric('F', "GH")), + pmetricutiltest.Resource("A", + pmetricutiltest.Scope("C", + pmetricutiltest.Metric("E", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + pmetricutiltest.Metric("F", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + ), + pmetricutiltest.Scope("D", + pmetricutiltest.Metric("E", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + pmetricutiltest.Metric("F", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + ), ), - pmetricutiltest.WithResource('B', - pmetricutiltest.WithScope('D', pmetricutiltest.WithMetric('E', "GH"), pmetricutiltest.WithMetric('F', "GH")), + pmetricutiltest.Resource("B", + pmetricutiltest.Scope("D", + pmetricutiltest.Metric("E", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + pmetricutiltest.Metric("F", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + ), ), ), expectTo: pmetricutiltest.NewMetrics("B", "C", "EF", "GH"), @@ -159,13 +168,24 @@ func TestMoveMetricsWithContextIf(t *testing.T) { from: pmetricutiltest.NewMetrics("AB", "CD", "EF", "GH"), to: pmetric.NewMetrics(), expectFrom: pmetricutiltest.NewMetricsFromOpts( - pmetricutiltest.WithResource('A', - pmetricutiltest.WithScope('C', pmetricutiltest.WithMetric('E', "GH"), pmetricutiltest.WithMetric('F', "GH")), - pmetricutiltest.WithScope('D', pmetricutiltest.WithMetric('E', "GH")), + pmetricutiltest.Resource("A", + pmetricutiltest.Scope("C", + pmetricutiltest.Metric("E", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + pmetricutiltest.Metric("F", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + ), + pmetricutiltest.Scope("D", + pmetricutiltest.Metric("E", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + ), ), - pmetricutiltest.WithResource('B', - pmetricutiltest.WithScope('C', pmetricutiltest.WithMetric('E', "GH"), pmetricutiltest.WithMetric('F', "GH")), - pmetricutiltest.WithScope('D', pmetricutiltest.WithMetric('E', "GH"), pmetricutiltest.WithMetric('F', "GH")), + pmetricutiltest.Resource("B", + pmetricutiltest.Scope("C", + pmetricutiltest.Metric("E", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + pmetricutiltest.Metric("F", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + ), + pmetricutiltest.Scope("D", + pmetricutiltest.Metric("E", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + pmetricutiltest.Metric("F", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + ), ), ), expectTo: pmetricutiltest.NewMetrics("A", "D", "F", "GH"), @@ -189,13 +209,23 @@ func TestMoveMetricsWithContextIf(t *testing.T) { from: pmetricutiltest.NewMetrics("AB", "CD", "EF", "GH"), to: pmetric.NewMetrics(), expectFrom: pmetricutiltest.NewMetricsFromOpts( - pmetricutiltest.WithResource('A', - pmetricutiltest.WithScope('C', pmetricutiltest.WithMetric('E', "GH"), pmetricutiltest.WithMetric('F', "GH")), - pmetricutiltest.WithScope('D', pmetricutiltest.WithMetric('E', "GH"), pmetricutiltest.WithMetric('F', "GH")), + pmetricutiltest.Resource("A", + pmetricutiltest.Scope("C", + pmetricutiltest.Metric("E", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + pmetricutiltest.Metric("F", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + ), + pmetricutiltest.Scope("D", + pmetricutiltest.Metric("E", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + pmetricutiltest.Metric("F", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + ), ), - pmetricutiltest.WithResource('B', - pmetricutiltest.WithScope('C', pmetricutiltest.WithMetric('F', "GH")), - pmetricutiltest.WithScope('D', pmetricutiltest.WithMetric('F', "GH")), + pmetricutiltest.Resource("B", + pmetricutiltest.Scope("C", + pmetricutiltest.Metric("F", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + ), + pmetricutiltest.Scope("D", + pmetricutiltest.Metric("F", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + ), ), ), expectTo: pmetricutiltest.NewMetrics("B", "CD", "E", "GH"), @@ -209,9 +239,17 @@ func TestMoveMetricsWithContextIf(t *testing.T) { to: pmetricutiltest.NewMetrics("1", "2", "3", "4"), expectFrom: pmetricutiltest.NewMetrics("AB", "C", "EF", "GH"), expectTo: pmetricutiltest.NewMetricsFromOpts( - pmetricutiltest.WithResource('1', pmetricutiltest.WithScope('2', pmetricutiltest.WithMetric('3', "4"))), - pmetricutiltest.WithResource('A', pmetricutiltest.WithScope('D', pmetricutiltest.WithMetric('E', "GH"), pmetricutiltest.WithMetric('F', "GH"))), - pmetricutiltest.WithResource('B', pmetricutiltest.WithScope('D', pmetricutiltest.WithMetric('E', "GH"), pmetricutiltest.WithMetric('F', "GH"))), + pmetricutiltest.Resource("1", pmetricutiltest.Scope("2", + pmetricutiltest.Metric("3", pmetricutiltest.NumberDataPoint("4")), + )), + pmetricutiltest.Resource("A", pmetricutiltest.Scope("D", + pmetricutiltest.Metric("E", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + pmetricutiltest.Metric("F", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + )), + pmetricutiltest.Resource("B", pmetricutiltest.Scope("D", + pmetricutiltest.Metric("E", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + pmetricutiltest.Metric("F", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + )), ), }, } diff --git a/connector/routingconnector/internal/pmetricutiltest/metrics.go b/connector/routingconnector/internal/pmetricutiltest/metrics.go index a908e1638e63..18b59eb29cf8 100644 --- a/connector/routingconnector/internal/pmetricutiltest/metrics.go +++ b/connector/routingconnector/internal/pmetricutiltest/metrics.go @@ -44,58 +44,44 @@ func NewMetrics(resourceIDs, scopeIDs, metricIDs, dataPointIDs string) pmetric.M return md } -type Resource struct { - id byte - scopes []Scope -} - -type Scope struct { - id byte - metrics []Metric -} - -type Metric struct { - id byte - dataPoints string +func NewMetricsFromOpts(resources ...pmetric.ResourceMetrics) pmetric.Metrics { + md := pmetric.NewMetrics() + for _, resource := range resources { + resource.CopyTo(md.ResourceMetrics().AppendEmpty()) + } + return md } -func WithResource(id byte, scopes ...Scope) Resource { - r := Resource{id: id} - r.scopes = append(r.scopes, scopes...) - return r +func Resource(id string, scopes ...pmetric.ScopeMetrics) pmetric.ResourceMetrics { + rm := pmetric.NewResourceMetrics() + rm.Resource().Attributes().PutStr("resourceName", "resource"+id) + for _, scope := range scopes { + scope.CopyTo(rm.ScopeMetrics().AppendEmpty()) + } + return rm } -func WithScope(id byte, metrics ...Metric) Scope { - s := Scope{id: id} - s.metrics = append(s.metrics, metrics...) +func Scope(id string, metrics ...pmetric.Metric) pmetric.ScopeMetrics { + s := pmetric.NewScopeMetrics() + s.Scope().SetName("scope" + id) + for _, metric := range metrics { + metric.CopyTo(s.Metrics().AppendEmpty()) + } return s } -func WithMetric(id byte, dataPoints string) Metric { - return Metric{id: id, dataPoints: dataPoints} +func Metric(id string, dps ...pmetric.NumberDataPoint) pmetric.Metric { + m := pmetric.NewMetric() + m.SetName("metric" + id) + g := m.SetEmptyGauge() + for _, dp := range dps { + dp.CopyTo(g.DataPoints().AppendEmpty()) + } + return m } -// NewMetricsFromOpts creates a pmetric.Metrics with the specified resources, scopes, metrics, -// and data points. The general idea is the same as NewMetrics, but this function allows for -// more flexibility in creating non-uniform structures. -func NewMetricsFromOpts(resources ...Resource) pmetric.Metrics { - md := pmetric.NewMetrics() - for _, resource := range resources { - r := md.ResourceMetrics().AppendEmpty() - r.Resource().Attributes().PutStr("resourceName", "resource"+string(resource.id)) - for _, scope := range resource.scopes { - s := r.ScopeMetrics().AppendEmpty() - s.Scope().SetName("scope" + string(scope.id)) - for _, metric := range scope.metrics { - m := s.Metrics().AppendEmpty() - m.SetName("metric" + string(metric.id)) - dps := m.SetEmptyGauge().DataPoints() - for i := 0; i < len(metric.dataPoints); i++ { - dp := dps.AppendEmpty() - dp.Attributes().PutStr("dpName", "dp"+string(metric.dataPoints[i])) - } - } - } - } - return md +func NumberDataPoint(id string) pmetric.NumberDataPoint { + dp := pmetric.NewNumberDataPoint() + dp.Attributes().PutStr("dpName", "dp"+id) + return dp } diff --git a/connector/routingconnector/internal/pmetricutiltest/metrics_test.go b/connector/routingconnector/internal/pmetricutiltest/metrics_test.go index abfcde29d72c..9fcd2edebef6 100644 --- a/connector/routingconnector/internal/pmetricutiltest/metrics_test.go +++ b/connector/routingconnector/internal/pmetricutiltest/metrics_test.go @@ -34,15 +34,12 @@ func TestNewMetrics(t *testing.T) { dp.Attributes().PutStr("dpName", "dpD") // resourceA.scopeB.metricC.dpD return md }() - fromOpts := pmetricutiltest.NewMetricsFromOpts( - pmetricutiltest.WithResource('A', - pmetricutiltest.WithScope('B', - pmetricutiltest.WithMetric('C', "D"), - ), - ), - ) assert.NoError(t, pmetrictest.CompareMetrics(expected, pmetricutiltest.NewMetrics("A", "B", "C", "D"))) - assert.NoError(t, pmetrictest.CompareMetrics(expected, fromOpts)) + assert.NoError(t, pmetrictest.CompareMetrics(expected, pmetricutiltest.NewMetricsFromOpts( + pmetricutiltest.Resource("A", + pmetricutiltest.Scope("B", pmetricutiltest.Metric("C", pmetricutiltest.NumberDataPoint("D"))), + ), + ))) }) t.Run("two_resources", func(t *testing.T) { @@ -68,20 +65,15 @@ func TestNewMetrics(t *testing.T) { dp.Attributes().PutStr("dpName", "dpE") // resourceB.scopeC.metricD.dpE return md }() - fromOpts := pmetricutiltest.NewMetricsFromOpts( - pmetricutiltest.WithResource('A', - pmetricutiltest.WithScope('C', - pmetricutiltest.WithMetric('D', "E"), - ), + assert.NoError(t, pmetrictest.CompareMetrics(expected, pmetricutiltest.NewMetrics("AB", "C", "D", "E"))) + assert.NoError(t, pmetrictest.CompareMetrics(expected, pmetricutiltest.NewMetricsFromOpts( + pmetricutiltest.Resource("A", + pmetricutiltest.Scope("C", pmetricutiltest.Metric("D", pmetricutiltest.NumberDataPoint("E"))), ), - pmetricutiltest.WithResource('B', - pmetricutiltest.WithScope('C', - pmetricutiltest.WithMetric('D', "E"), - ), + pmetricutiltest.Resource("B", + pmetricutiltest.Scope("C", pmetricutiltest.Metric("D", pmetricutiltest.NumberDataPoint("E"))), ), - ) - assert.NoError(t, pmetrictest.CompareMetrics(expected, pmetricutiltest.NewMetrics("AB", "C", "D", "E"))) - assert.NoError(t, pmetrictest.CompareMetrics(expected, fromOpts)) + ))) }) t.Run("two_scopes", func(t *testing.T) { @@ -105,18 +97,13 @@ func TestNewMetrics(t *testing.T) { dp.Attributes().PutStr("dpName", "dpE") // resourceA.scopeC.metricD.dpE return md }() - fromOpts := pmetricutiltest.NewMetricsFromOpts( - pmetricutiltest.WithResource('A', - pmetricutiltest.WithScope('B', - pmetricutiltest.WithMetric('D', "E"), - ), - pmetricutiltest.WithScope('C', - pmetricutiltest.WithMetric('D', "E"), - ), - ), - ) assert.NoError(t, pmetrictest.CompareMetrics(expected, pmetricutiltest.NewMetrics("A", "BC", "D", "E"))) - assert.NoError(t, pmetrictest.CompareMetrics(expected, fromOpts)) + assert.NoError(t, pmetrictest.CompareMetrics(expected, pmetricutiltest.NewMetricsFromOpts( + pmetricutiltest.Resource("A", + pmetricutiltest.Scope("B", pmetricutiltest.Metric("D", pmetricutiltest.NumberDataPoint("E"))), + pmetricutiltest.Scope("C", pmetricutiltest.Metric("D", pmetricutiltest.NumberDataPoint("E"))), + ), + ))) }) t.Run("two_metrics", func(t *testing.T) { @@ -138,16 +125,15 @@ func TestNewMetrics(t *testing.T) { dp.Attributes().PutStr("dpName", "dpE") // resourceA.scopeB.metricD.dpE return md }() - fromOpts := pmetricutiltest.NewMetricsFromOpts( - pmetricutiltest.WithResource('A', - pmetricutiltest.WithScope('B', - pmetricutiltest.WithMetric('C', "E"), - pmetricutiltest.WithMetric('D', "E"), + assert.NoError(t, pmetrictest.CompareMetrics(expected, pmetricutiltest.NewMetrics("A", "B", "CD", "E"))) + assert.NoError(t, pmetrictest.CompareMetrics(expected, pmetricutiltest.NewMetricsFromOpts( + pmetricutiltest.Resource("A", + pmetricutiltest.Scope("B", + pmetricutiltest.Metric("C", pmetricutiltest.NumberDataPoint("E")), + pmetricutiltest.Metric("D", pmetricutiltest.NumberDataPoint("E")), ), ), - ) - assert.NoError(t, pmetrictest.CompareMetrics(expected, pmetricutiltest.NewMetrics("A", "B", "CD", "E"))) - assert.NoError(t, pmetrictest.CompareMetrics(expected, fromOpts)) + ))) }) t.Run("two_datapoints", func(t *testing.T) { @@ -166,14 +152,11 @@ func TestNewMetrics(t *testing.T) { dp.Attributes().PutStr("dpName", "dpE") // resourceA.scopeB.metricC.dpE return md }() - fromOpts := pmetricutiltest.NewMetricsFromOpts( - pmetricutiltest.WithResource('A', - pmetricutiltest.WithScope('B', - pmetricutiltest.WithMetric('C', "DE"), - ), - ), - ) assert.NoError(t, pmetrictest.CompareMetrics(expected, pmetricutiltest.NewMetrics("A", "B", "C", "DE"))) - assert.NoError(t, pmetrictest.CompareMetrics(expected, fromOpts)) + assert.NoError(t, pmetrictest.CompareMetrics(expected, pmetricutiltest.NewMetricsFromOpts( + pmetricutiltest.Resource("A", + pmetricutiltest.Scope("B", pmetricutiltest.Metric("C", pmetricutiltest.NumberDataPoint("D"), pmetricutiltest.NumberDataPoint("E"))), + ), + ))) }) } diff --git a/connector/routingconnector/metrics_test.go b/connector/routingconnector/metrics_test.go index f87a15ff613c..fbbf7b383381 100644 --- a/connector/routingconnector/metrics_test.go +++ b/connector/routingconnector/metrics_test.go @@ -842,12 +842,21 @@ func TestMetricsConnectorDetailed(t *testing.T) { expectSink0: pmetricutiltest.NewMetrics("B", "D", "EF", "GH"), expectSink1: pmetric.Metrics{}, expectSinkD: pmetricutiltest.NewMetricsFromOpts( - pmetricutiltest.WithResource('A', - pmetricutiltest.WithScope('C', pmetricutiltest.WithMetric('E', "GH"), pmetricutiltest.WithMetric('F', "GH")), - pmetricutiltest.WithScope('D', pmetricutiltest.WithMetric('E', "GH"), pmetricutiltest.WithMetric('F', "GH")), + pmetricutiltest.Resource("A", + pmetricutiltest.Scope("C", + pmetricutiltest.Metric("E", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + pmetricutiltest.Metric("F", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + ), + pmetricutiltest.Scope("D", + pmetricutiltest.Metric("E", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + pmetricutiltest.Metric("F", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + ), ), - pmetricutiltest.WithResource('B', - pmetricutiltest.WithScope('C', pmetricutiltest.WithMetric('E', "GH"), pmetricutiltest.WithMetric('F', "GH")), + pmetricutiltest.Resource("B", + pmetricutiltest.Scope("C", + pmetricutiltest.Metric("E", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + pmetricutiltest.Metric("F", pmetricutiltest.NumberDataPoint("G"), pmetricutiltest.NumberDataPoint("H")), + ), ), ), },