Skip to content

Commit

Permalink
[chore][pmetricutiltest] Use native structs in options (open-telemetr…
Browse files Browse the repository at this point in the history
…y#36380)

Same as open-telemetry#36354 but for metrics
  • Loading branch information
djaglowski authored Nov 14, 2024
1 parent 686d099 commit 40dbf0b
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func TestNewLogs(t *testing.T) {
expected := plog.NewLogs()
assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogs("", "", "")))
assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.New()))
assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.New()))
})

t.Run("simple", func(t *testing.T) {
Expand Down
78 changes: 58 additions & 20 deletions connector/routingconnector/internal/pmetricutil/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"),
Expand All @@ -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"),
Expand All @@ -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")),
)),
),
},
}
Expand Down
76 changes: 31 additions & 45 deletions connector/routingconnector/internal/pmetricutiltest/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
77 changes: 30 additions & 47 deletions connector/routingconnector/internal/pmetricutiltest/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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"))),
),
)))
})
}
19 changes: 14 additions & 5 deletions connector/routingconnector/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
),
),
),
},
Expand Down

0 comments on commit 40dbf0b

Please sign in to comment.