From 193e77d509cbc3a546722b0f85d6ca0540a21f21 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Mon, 26 Jul 2021 17:29:54 -0700 Subject: [PATCH] Convert testbed to new Number metrics (#3719) * Convert goldentest and filterproc to new Number metrics Signed-off-by: Bogdan Drutu * Convert testbed to new Number metrics Signed-off-by: Bogdan Drutu --- internal/goldendataset/metrics_gen.go | 40 ++--- internal/goldendataset/metrics_gen_test.go | 8 +- internal/goldendataset/pict_metrics_gen.go | 14 +- .../goldendataset/pict_metrics_gen_test.go | 14 +- internal/processor/filterexpr/matcher.go | 32 ---- internal/processor/filterexpr/matcher_test.go | 161 +++++------------- processor/filterprocessor/expr_test.go | 33 ++-- testbed/correctness/metrics/metric_diff.go | 73 +++----- .../correctness/metrics/metric_diff_test.go | 6 +- testbed/tests/resource_processor_test.go | 8 +- 10 files changed, 130 insertions(+), 259 deletions(-) diff --git a/internal/goldendataset/metrics_gen.go b/internal/goldendataset/metrics_gen.go index 1a95d81055c..879bbb065e8 100644 --- a/internal/goldendataset/metrics_gen.go +++ b/internal/goldendataset/metrics_gen.go @@ -27,6 +27,8 @@ import ( type MetricsCfg struct { // The type of metric to generate MetricDescriptorType pdata.MetricDataType + // MetricValueType is the type of the numeric value: int or double. + MetricValueType pdata.MetricValueType // If MetricDescriptorType is one of the Sum, this describes if the sum is monotonic or not. IsMonotonicSum bool // A prefix for every metric name @@ -55,7 +57,8 @@ type MetricsCfg struct { // (but boring) metrics, and can be used as a starting point for making alterations. func DefaultCfg() MetricsCfg { return MetricsCfg{ - MetricDescriptorType: pdata.MetricDataTypeIntGauge, + MetricDescriptorType: pdata.MetricDataTypeGauge, + MetricValueType: pdata.MetricValueTypeInt, MetricNamePrefix: "", NumILMPerResource: 1, NumMetricsPerILM: 1, @@ -117,24 +120,15 @@ func (g *metricGenerator) populateMetrics(cfg MetricsCfg, ilm pdata.Instrumentat metric := metrics.AppendEmpty() g.populateMetricDesc(cfg, metric) switch cfg.MetricDescriptorType { - case pdata.MetricDataTypeIntGauge: - metric.SetDataType(pdata.MetricDataTypeIntGauge) - populateIntPoints(cfg, metric.IntGauge().DataPoints()) case pdata.MetricDataTypeGauge: metric.SetDataType(pdata.MetricDataTypeGauge) - populateDoublePoints(cfg, metric.Gauge().DataPoints()) - case pdata.MetricDataTypeIntSum: - metric.SetDataType(pdata.MetricDataTypeIntSum) - sum := metric.IntSum() - sum.SetIsMonotonic(cfg.IsMonotonicSum) - sum.SetAggregationTemporality(pdata.AggregationTemporalityCumulative) - populateIntPoints(cfg, sum.DataPoints()) + populateNumberPoints(cfg, metric.Gauge().DataPoints()) case pdata.MetricDataTypeSum: metric.SetDataType(pdata.MetricDataTypeSum) sum := metric.Sum() sum.SetIsMonotonic(cfg.IsMonotonicSum) sum.SetAggregationTemporality(pdata.AggregationTemporalityCumulative) - populateDoublePoints(cfg, sum.DataPoints()) + populateNumberPoints(cfg, sum.DataPoints()) case pdata.MetricDataTypeHistogram: metric.SetDataType(pdata.MetricDataTypeHistogram) histo := metric.Histogram() @@ -151,24 +145,20 @@ func (g *metricGenerator) populateMetricDesc(cfg MetricsCfg, metric pdata.Metric metric.SetUnit("my-md-units") } -func populateIntPoints(cfg MetricsCfg, pts pdata.IntDataPointSlice) { - pts.EnsureCapacity(cfg.NumPtsPerMetric) - for i := 0; i < cfg.NumPtsPerMetric; i++ { - pt := pts.AppendEmpty() - pt.SetStartTimestamp(pdata.Timestamp(cfg.StartTime)) - pt.SetTimestamp(getTimestamp(cfg.StartTime, cfg.StepSize, i)) - pt.SetValue(int64(cfg.PtVal + i)) - populatePtLabels(cfg, pt.LabelsMap()) - } -} - -func populateDoublePoints(cfg MetricsCfg, pts pdata.NumberDataPointSlice) { +func populateNumberPoints(cfg MetricsCfg, pts pdata.NumberDataPointSlice) { pts.EnsureCapacity(cfg.NumPtsPerMetric) for i := 0; i < cfg.NumPtsPerMetric; i++ { pt := pts.AppendEmpty() pt.SetStartTimestamp(pdata.Timestamp(cfg.StartTime)) pt.SetTimestamp(getTimestamp(cfg.StartTime, cfg.StepSize, i)) - pt.SetDoubleVal(float64(cfg.PtVal + i)) + switch cfg.MetricValueType { + case pdata.MetricValueTypeInt: + pt.SetIntVal(int64(cfg.PtVal + i)) + case pdata.MetricValueTypeDouble: + pt.SetDoubleVal(float64(cfg.PtVal + i)) + default: + panic("Should not happen") + } populatePtLabels(cfg, pt.LabelsMap()) } } diff --git a/internal/goldendataset/metrics_gen_test.go b/internal/goldendataset/metrics_gen_test.go index bc30ff757cb..6020d2ed074 100644 --- a/internal/goldendataset/metrics_gen_test.go +++ b/internal/goldendataset/metrics_gen_test.go @@ -43,8 +43,8 @@ func TestGenDefault(t *testing.T) { require.Equal(t, "my-md-description", pdm.Description()) require.Equal(t, "my-md-units", pdm.Unit()) - require.Equal(t, pdata.MetricDataTypeIntGauge, pdm.DataType()) - pts := pdm.IntGauge().DataPoints() + require.Equal(t, pdata.MetricDataTypeGauge, pdm.DataType()) + pts := pdm.Gauge().DataPoints() require.Equal(t, 1, pts.Len()) pt := pts.At(0) @@ -54,7 +54,7 @@ func TestGenDefault(t *testing.T) { require.EqualValues(t, 940000000000000000, pt.StartTimestamp()) require.EqualValues(t, 940000000000000042, pt.Timestamp()) - require.EqualValues(t, 1, pt.Value()) + require.EqualValues(t, 1, pt.IntVal()) } func TestDoubleHistogramFunctions(t *testing.T) { @@ -99,7 +99,7 @@ func TestGenDoubleGauge(t *testing.T) { pts := metric.Gauge().DataPoints() require.Equal(t, 1, pts.Len()) pt := pts.At(0) - require.EqualValues(t, 1, pt.DoubleVal()) + require.EqualValues(t, float64(1), pt.IntVal()) } func getMetric(md pdata.Metrics) pdata.Metric { diff --git a/internal/goldendataset/pict_metrics_gen.go b/internal/goldendataset/pict_metrics_gen.go index 8cd5e20a351..ca67e062e76 100644 --- a/internal/goldendataset/pict_metrics_gen.go +++ b/internal/goldendataset/pict_metrics_gen.go @@ -65,25 +65,33 @@ func pictToCfg(inputs PICTMetricInputs) MetricsCfg { switch inputs.MetricType { case MetricTypeIntGauge: - cfg.MetricDescriptorType = pdata.MetricDataTypeIntGauge + cfg.MetricDescriptorType = pdata.MetricDataTypeGauge + cfg.MetricValueType = pdata.MetricValueTypeInt case MetricTypeMonotonicIntSum: - cfg.MetricDescriptorType = pdata.MetricDataTypeIntSum + cfg.MetricDescriptorType = pdata.MetricDataTypeSum + cfg.MetricValueType = pdata.MetricValueTypeInt cfg.IsMonotonicSum = true case MetricTypeNonMonotonicIntSum: - cfg.MetricDescriptorType = pdata.MetricDataTypeIntSum + cfg.MetricDescriptorType = pdata.MetricDataTypeSum + cfg.MetricValueType = pdata.MetricValueTypeInt cfg.IsMonotonicSum = false case MetricTypeDoubleGauge: cfg.MetricDescriptorType = pdata.MetricDataTypeGauge + cfg.MetricValueType = pdata.MetricValueTypeDouble case MetricTypeMonotonicDoubleSum: cfg.MetricDescriptorType = pdata.MetricDataTypeSum + cfg.MetricValueType = pdata.MetricValueTypeDouble cfg.IsMonotonicSum = true case MetricTypeNonMonotonicDoubleSum: cfg.MetricDescriptorType = pdata.MetricDataTypeSum + cfg.MetricValueType = pdata.MetricValueTypeDouble cfg.IsMonotonicSum = false case MetricTypeDoubleExemplarsHistogram: cfg.MetricDescriptorType = pdata.MetricDataTypeHistogram + cfg.MetricValueType = pdata.MetricValueTypeNone case MetricTypeIntExemplarsHistogram: cfg.MetricDescriptorType = pdata.MetricDataTypeHistogram + cfg.MetricValueType = pdata.MetricValueTypeNone default: panic("Should not happen, unsupported type " + string(inputs.MetricType)) } diff --git a/internal/goldendataset/pict_metrics_gen_test.go b/internal/goldendataset/pict_metrics_gen_test.go index 67dbd0a2c3c..345b2cb8ec5 100644 --- a/internal/goldendataset/pict_metrics_gen_test.go +++ b/internal/goldendataset/pict_metrics_gen_test.go @@ -17,6 +17,7 @@ package goldendataset import ( "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/model/pdata" @@ -45,7 +46,8 @@ func TestPICTtoCfg(t *testing.T) { cfg: MetricsCfg{ NumResourceAttrs: 0, NumPtsPerMetric: 1, - MetricDescriptorType: pdata.MetricDataTypeIntGauge, + MetricDescriptorType: pdata.MetricDataTypeGauge, + MetricValueType: pdata.MetricValueTypeInt, NumPtLabels: 0, }, }, @@ -61,6 +63,7 @@ func TestPICTtoCfg(t *testing.T) { NumResourceAttrs: 1, NumPtsPerMetric: 1, MetricDescriptorType: pdata.MetricDataTypeGauge, + MetricValueType: pdata.MetricValueTypeDouble, NumPtLabels: 1, }, }, @@ -84,10 +87,11 @@ func TestPICTtoCfg(t *testing.T) { t.Run(test.name, func(t *testing.T) { actual := pictToCfg(test.inputs) expected := test.cfg - require.Equal(t, expected.NumResourceAttrs, actual.NumResourceAttrs) - require.Equal(t, expected.NumPtsPerMetric, actual.NumPtsPerMetric) - require.Equal(t, expected.MetricDescriptorType, actual.MetricDescriptorType) - require.Equal(t, expected.NumPtLabels, actual.NumPtLabels) + assert.Equal(t, expected.NumResourceAttrs, actual.NumResourceAttrs) + assert.Equal(t, expected.NumPtsPerMetric, actual.NumPtsPerMetric) + assert.Equal(t, expected.MetricDescriptorType, actual.MetricDescriptorType) + assert.Equal(t, expected.MetricValueType, actual.MetricValueType) + assert.Equal(t, expected.NumPtLabels, actual.NumPtLabels) }) } } diff --git a/internal/processor/filterexpr/matcher.go b/internal/processor/filterexpr/matcher.go index 54a39278cef..d3ae381ac28 100644 --- a/internal/processor/filterexpr/matcher.go +++ b/internal/processor/filterexpr/matcher.go @@ -44,12 +44,8 @@ func NewMatcher(expression string) (*Matcher, error) { func (m *Matcher) MatchMetric(metric pdata.Metric) (bool, error) { metricName := metric.Name() switch metric.DataType() { - case pdata.MetricDataTypeIntGauge: - return m.matchIntGauge(metricName, metric.IntGauge()) case pdata.MetricDataTypeGauge: return m.matchGauge(metricName, metric.Gauge()) - case pdata.MetricDataTypeIntSum: - return m.matchIntSum(metricName, metric.IntSum()) case pdata.MetricDataTypeSum: return m.matchSum(metricName, metric.Sum()) case pdata.MetricDataTypeHistogram: @@ -59,20 +55,6 @@ func (m *Matcher) MatchMetric(metric pdata.Metric) (bool, error) { } } -func (m *Matcher) matchIntGauge(metricName string, gauge pdata.IntGauge) (bool, error) { - pts := gauge.DataPoints() - for i := 0; i < pts.Len(); i++ { - matched, err := m.matchEnv(metricName, pts.At(i).LabelsMap()) - if err != nil { - return false, err - } - if matched { - return true, nil - } - } - return false, nil -} - func (m *Matcher) matchGauge(metricName string, gauge pdata.Gauge) (bool, error) { pts := gauge.DataPoints() for i := 0; i < pts.Len(); i++ { @@ -101,20 +83,6 @@ func (m *Matcher) matchSum(metricName string, sum pdata.Sum) (bool, error) { return false, nil } -func (m *Matcher) matchIntSum(metricName string, sum pdata.IntSum) (bool, error) { - pts := sum.DataPoints() - for i := 0; i < pts.Len(); i++ { - matched, err := m.matchEnv(metricName, pts.At(i).LabelsMap()) - if err != nil { - return false, err - } - if matched { - return true, nil - } - } - return false, nil -} - func (m *Matcher) matchDoubleHistogram(metricName string, histogram pdata.Histogram) (bool, error) { pts := histogram.DataPoints() for i := 0; i < pts.Len(); i++ { diff --git a/internal/processor/filterexpr/matcher_test.go b/internal/processor/filterexpr/matcher_test.go index 92159b5b80f..38e73e644a6 100644 --- a/internal/processor/filterexpr/matcher_test.go +++ b/internal/processor/filterexpr/matcher_test.go @@ -46,32 +46,19 @@ func TestUnknownDataType(t *testing.T) { assert.False(t, matched) } -func TestNilIntGauge(t *testing.T) { - dataType := pdata.MetricDataTypeIntGauge - testNilValue(t, dataType) +func TestEmptyGauge(t *testing.T) { + testEmptyValue(t, pdata.MetricDataTypeGauge) } -func TestNilDoubleGauge(t *testing.T) { - dataType := pdata.MetricDataTypeGauge - testNilValue(t, dataType) +func TestEmptySum(t *testing.T) { + testEmptyValue(t, pdata.MetricDataTypeSum) } -func TestNilSum(t *testing.T) { - dataType := pdata.MetricDataTypeSum - testNilValue(t, dataType) +func TestEmptyHistogram(t *testing.T) { + testEmptyValue(t, pdata.MetricDataTypeHistogram) } -func TestNilIntSum(t *testing.T) { - dataType := pdata.MetricDataTypeIntSum - testNilValue(t, dataType) -} - -func TestNilDoubleHistogram(t *testing.T) { - dataType := pdata.MetricDataTypeHistogram - testNilValue(t, dataType) -} - -func testNilValue(t *testing.T, dataType pdata.MetricDataType) { +func testEmptyValue(t *testing.T, dataType pdata.MetricDataType) { matcher, err := NewMatcher(`MetricName == 'my.metric'`) require.NoError(t, err) m := pdata.NewMetric() @@ -82,19 +69,7 @@ func testNilValue(t *testing.T, dataType pdata.MetricDataType) { assert.False(t, matched) } -func TestIntGaugeEmptyDataPoint(t *testing.T) { - matcher, err := NewMatcher(`MetricName == 'my.metric'`) - require.NoError(t, err) - m := pdata.NewMetric() - m.SetName("my.metric") - m.SetDataType(pdata.MetricDataTypeIntGauge) - m.IntGauge().DataPoints().AppendEmpty() - matched, err := matcher.MatchMetric(m) - assert.NoError(t, err) - assert.True(t, matched) -} - -func TestDoubleGaugeEmptyDataPoint(t *testing.T) { +func TestGaugeEmptyDataPoint(t *testing.T) { matcher, err := NewMatcher(`MetricName == 'my.metric'`) require.NoError(t, err) m := pdata.NewMetric() @@ -118,67 +93,73 @@ func TestSumEmptyDataPoint(t *testing.T) { assert.True(t, matched) } -func TestIntSumEmptyDataPoint(t *testing.T) { +func TestHistogramEmptyDataPoint(t *testing.T) { matcher, err := NewMatcher(`MetricName == 'my.metric'`) require.NoError(t, err) m := pdata.NewMetric() m.SetName("my.metric") - m.SetDataType(pdata.MetricDataTypeIntSum) - m.IntSum().DataPoints().AppendEmpty() + m.SetDataType(pdata.MetricDataTypeHistogram) + m.Histogram().DataPoints().AppendEmpty() matched, err := matcher.MatchMetric(m) assert.NoError(t, err) assert.True(t, matched) } -func TestDoubleHistogramEmptyDataPoint(t *testing.T) { - matcher, err := NewMatcher(`MetricName == 'my.metric'`) +func TestMatchIntGaugeDataPointByMetricAndSecondPointLabelValue(t *testing.T) { + matcher, err := NewMatcher( + `MetricName == 'my.metric' && Label("baz") == "glarch"`, + ) require.NoError(t, err) m := pdata.NewMetric() m.SetName("my.metric") - m.SetDataType(pdata.MetricDataTypeHistogram) - m.Histogram().DataPoints().AppendEmpty() + m.SetDataType(pdata.MetricDataTypeGauge) + dps := m.Gauge().DataPoints() + + dps.AppendEmpty().LabelsMap().Insert("foo", "bar") + dps.AppendEmpty().LabelsMap().Insert("baz", "glarch") + matched, err := matcher.MatchMetric(m) assert.NoError(t, err) assert.True(t, matched) } -func TestMatchIntGaugeByMetricName(t *testing.T) { +func TestMatchGaugeByMetricName(t *testing.T) { expression := `MetricName == 'my.metric'` - assert.True(t, testMatchIntGauge(t, "my.metric", expression, nil)) + assert.True(t, testMatchGauge(t, "my.metric", expression, nil)) } -func TestNonMatchIntGaugeByMetricName(t *testing.T) { +func TestNonMatchGaugeByMetricName(t *testing.T) { expression := `MetricName == 'my.metric'` - assert.False(t, testMatchIntGauge(t, "foo.metric", expression, nil)) + assert.False(t, testMatchGauge(t, "foo.metric", expression, nil)) } -func TestNonMatchIntGaugeDataPointByMetricAndHasLabel(t *testing.T) { +func TestNonMatchGaugeDataPointByMetricAndHasLabel(t *testing.T) { expression := `MetricName == 'my.metric' && HasLabel("foo")` - assert.False(t, testMatchIntGauge(t, "foo.metric", expression, nil)) + assert.False(t, testMatchGauge(t, "foo.metric", expression, nil)) } -func TestMatchIntGaugeDataPointByMetricAndHasLabel(t *testing.T) { +func TestMatchGaugeDataPointByMetricAndHasLabel(t *testing.T) { expression := `MetricName == 'my.metric' && HasLabel("foo")` - assert.True(t, testMatchIntGauge(t, "my.metric", expression, map[string]string{"foo": ""})) + assert.True(t, testMatchGauge(t, "my.metric", expression, map[string]string{"foo": ""})) } -func TestMatchIntGaugeDataPointByMetricAndLabelValue(t *testing.T) { +func TestMatchGaugeDataPointByMetricAndLabelValue(t *testing.T) { expression := `MetricName == 'my.metric' && Label("foo") == "bar"` - assert.False(t, testMatchIntGauge(t, "my.metric", expression, map[string]string{"foo": ""})) + assert.False(t, testMatchGauge(t, "my.metric", expression, map[string]string{"foo": ""})) } -func TestNonMatchIntGaugeDataPointByMetricAndLabelValue(t *testing.T) { +func TestNonMatchGaugeDataPointByMetricAndLabelValue(t *testing.T) { expression := `MetricName == 'my.metric' && Label("foo") == "bar"` - assert.False(t, testMatchIntGauge(t, "my.metric", expression, map[string]string{"foo": ""})) + assert.False(t, testMatchGauge(t, "my.metric", expression, map[string]string{"foo": ""})) } -func testMatchIntGauge(t *testing.T, metricName, expression string, lbls map[string]string) bool { +func testMatchGauge(t *testing.T, metricName, expression string, lbls map[string]string) bool { matcher, err := NewMatcher(expression) require.NoError(t, err) m := pdata.NewMetric() m.SetName(metricName) - m.SetDataType(pdata.MetricDataTypeIntGauge) - dps := m.IntGauge().DataPoints() + m.SetDataType(pdata.MetricDataTypeGauge) + dps := m.Gauge().DataPoints() pt := dps.AppendEmpty() if lbls != nil { pt.LabelsMap().InitFromMap(lbls) @@ -188,45 +169,6 @@ func testMatchIntGauge(t *testing.T, metricName, expression string, lbls map[str return match } -func TestMatchIntGaugeDataPointByMetricAndSecondPointLabelValue(t *testing.T) { - matcher, err := NewMatcher( - `MetricName == 'my.metric' && Label("baz") == "glarch"`, - ) - require.NoError(t, err) - m := pdata.NewMetric() - m.SetName("my.metric") - m.SetDataType(pdata.MetricDataTypeIntGauge) - dps := m.IntGauge().DataPoints() - - dps.AppendEmpty().LabelsMap().Insert("foo", "bar") - dps.AppendEmpty().LabelsMap().Insert("baz", "glarch") - - matched, err := matcher.MatchMetric(m) - assert.NoError(t, err) - assert.True(t, matched) -} - -func TestMatchDoubleGaugeByMetricName(t *testing.T) { - assert.True(t, testMatchDoubleGauge(t, "my.metric")) -} - -func TestNonMatchDoubleGaugeByMetricName(t *testing.T) { - assert.False(t, testMatchDoubleGauge(t, "foo.metric")) -} - -func testMatchDoubleGauge(t *testing.T, metricName string) bool { - matcher, err := NewMatcher(`MetricName == 'my.metric'`) - require.NoError(t, err) - m := pdata.NewMetric() - m.SetName(metricName) - m.SetDataType(pdata.MetricDataTypeGauge) - dps := m.Gauge().DataPoints() - dps.AppendEmpty() - match, err := matcher.MatchMetric(m) - assert.NoError(t, err) - return match -} - func TestMatchSumByMetricName(t *testing.T) { assert.True(t, matchSum(t, "my.metric")) } @@ -248,36 +190,15 @@ func matchSum(t *testing.T, metricName string) bool { return matched } -func TestMatchIntSumByMetricName(t *testing.T) { - assert.True(t, matchIntSum(t, "my.metric")) -} - -func TestNonMatchIntSumByMetricName(t *testing.T) { - assert.False(t, matchIntSum(t, "foo.metric")) -} - -func matchIntSum(t *testing.T, metricName string) bool { - matcher, err := NewMatcher(`MetricName == 'my.metric'`) - require.NoError(t, err) - m := pdata.NewMetric() - m.SetName(metricName) - m.SetDataType(pdata.MetricDataTypeIntSum) - dps := m.IntSum().DataPoints() - dps.AppendEmpty() - matched, err := matcher.MatchMetric(m) - assert.NoError(t, err) - return matched -} - -func TestMatchDoubleHistogramByMetricName(t *testing.T) { - assert.True(t, matchDoubleHistogram(t, "my.metric")) +func TestMatchHistogramByMetricName(t *testing.T) { + assert.True(t, matchHistogram(t, "my.metric")) } -func TestNonMatchDoubleHistogramByMetricName(t *testing.T) { - assert.False(t, matchDoubleHistogram(t, "foo.metric")) +func TestNonMatchHistogramByMetricName(t *testing.T) { + assert.False(t, matchHistogram(t, "foo.metric")) } -func matchDoubleHistogram(t *testing.T, metricName string) bool { +func matchHistogram(t *testing.T, metricName string) bool { matcher, err := NewMatcher(`MetricName == 'my.metric'`) require.NoError(t, err) m := pdata.NewMetric() diff --git a/processor/filterprocessor/expr_test.go b/processor/filterprocessor/expr_test.go index b926ae46eab..db34e407d0e 100644 --- a/processor/filterprocessor/expr_test.go +++ b/processor/filterprocessor/expr_test.go @@ -38,15 +38,17 @@ const filteredLblKey = "pt-label-key-1" const filteredLblVal = "pt-label-val-1" func TestExprError(t *testing.T) { - for mdType := pdata.MetricDataTypeIntGauge; mdType <= pdata.MetricDataTypeHistogram; mdType++ { - testMatchError(t, mdType) - } + testMatchError(t, pdata.MetricDataTypeGauge, pdata.MetricValueTypeInt) + testMatchError(t, pdata.MetricDataTypeGauge, pdata.MetricValueTypeDouble) + testMatchError(t, pdata.MetricDataTypeSum, pdata.MetricValueTypeInt) + testMatchError(t, pdata.MetricDataTypeSum, pdata.MetricValueTypeDouble) + testMatchError(t, pdata.MetricDataTypeHistogram, pdata.MetricValueTypeNone) } -func testMatchError(t *testing.T, mdType pdata.MetricDataType) { +func testMatchError(t *testing.T, mdType pdata.MetricDataType, mvType pdata.MetricValueType) { // the "foo" expr expression will cause expr Run() to return an error proc, next, logs := testProcessor(t, nil, []string{"foo"}) - pdm := testData("", 1, mdType) + pdm := testData("", 1, mdType, mvType) err := proc.ConsumeMetrics(context.Background(), pdm) assert.NoError(t, err) // assert that metrics not be filtered as a result @@ -56,18 +58,18 @@ func testMatchError(t *testing.T, mdType pdata.MetricDataType) { } func TestExprProcessor(t *testing.T) { - testFilter(t, pdata.MetricDataTypeIntGauge) - testFilter(t, pdata.MetricDataTypeGauge) - testFilter(t, pdata.MetricDataTypeIntSum) - testFilter(t, pdata.MetricDataTypeSum) - testFilter(t, pdata.MetricDataTypeHistogram) + testFilter(t, pdata.MetricDataTypeGauge, pdata.MetricValueTypeInt) + testFilter(t, pdata.MetricDataTypeGauge, pdata.MetricValueTypeDouble) + testFilter(t, pdata.MetricDataTypeSum, pdata.MetricValueTypeInt) + testFilter(t, pdata.MetricDataTypeSum, pdata.MetricValueTypeDouble) + testFilter(t, pdata.MetricDataTypeHistogram, pdata.MetricValueTypeNone) } -func testFilter(t *testing.T, mdType pdata.MetricDataType) { +func testFilter(t *testing.T, mdType pdata.MetricDataType, mvType pdata.MetricValueType) { format := "MetricName == '%s' && Label('%s') == '%s'" q := fmt.Sprintf(format, filteredMetric, filteredLblKey, filteredLblVal) - mds := testDataSlice(2, mdType) + mds := testDataSlice(2, mdType, mvType) totMetricCount := 0 for _, md := range mds { totMetricCount += md.MetricCount() @@ -178,17 +180,18 @@ func exprConfig(factory component.ProcessorFactory, include []string, exclude [] return cfg } -func testDataSlice(size int, mdType pdata.MetricDataType) []pdata.Metrics { +func testDataSlice(size int, mdType pdata.MetricDataType, mvType pdata.MetricValueType) []pdata.Metrics { var out []pdata.Metrics for i := 0; i < 16; i++ { - out = append(out, testData(fmt.Sprintf("p%d_", i), size, mdType)) + out = append(out, testData(fmt.Sprintf("p%d_", i), size, mdType, mvType)) } return out } -func testData(prefix string, size int, mdType pdata.MetricDataType) pdata.Metrics { +func testData(prefix string, size int, mdType pdata.MetricDataType, mvType pdata.MetricValueType) pdata.Metrics { c := goldendataset.MetricsCfg{ MetricDescriptorType: mdType, + MetricValueType: mvType, MetricNamePrefix: prefix, NumILMPerResource: size, NumMetricsPerILM: size, diff --git a/testbed/correctness/metrics/metric_diff.go b/testbed/correctness/metrics/metric_diff.go index 913d07a3b17..705d4df96f7 100644 --- a/testbed/correctness/metrics/metric_diff.go +++ b/testbed/correctness/metrics/metric_diff.go @@ -104,21 +104,15 @@ func DiffMetric(diffs []*MetricDiff, expected pdata.Metric, actual pdata.Metric) return diffs } switch actual.DataType() { - case pdata.MetricDataTypeIntGauge: - diffs = diffIntPts(diffs, expected.IntGauge().DataPoints(), actual.IntGauge().DataPoints()) case pdata.MetricDataTypeGauge: - diffs = diffDoublePts(diffs, expected.Gauge().DataPoints(), actual.Gauge().DataPoints()) - case pdata.MetricDataTypeIntSum: - diffs = diff(diffs, expected.IntSum().IsMonotonic(), actual.IntSum().IsMonotonic(), "IntSum IsMonotonic") - diffs = diff(diffs, expected.IntSum().AggregationTemporality(), actual.IntSum().AggregationTemporality(), "IntSum AggregationTemporality") - diffs = diffIntPts(diffs, expected.IntSum().DataPoints(), actual.IntSum().DataPoints()) + diffs = diffNumberPts(diffs, expected.Gauge().DataPoints(), actual.Gauge().DataPoints()) case pdata.MetricDataTypeSum: diffs = diff(diffs, expected.Sum().IsMonotonic(), actual.Sum().IsMonotonic(), "Sum IsMonotonic") diffs = diff(diffs, expected.Sum().AggregationTemporality(), actual.Sum().AggregationTemporality(), "Sum AggregationTemporality") - diffs = diffDoublePts(diffs, expected.Sum().DataPoints(), actual.Sum().DataPoints()) + diffs = diffNumberPts(diffs, expected.Sum().DataPoints(), actual.Sum().DataPoints()) case pdata.MetricDataTypeHistogram: diffs = diff(diffs, expected.Histogram().AggregationTemporality(), actual.Histogram().AggregationTemporality(), "Histogram AggregationTemporality") - diffs = diffDoubleHistogramPts(diffs, expected.Histogram().DataPoints(), actual.Histogram().DataPoints()) + diffs = diffHistogramPts(diffs, expected.Histogram().DataPoints(), actual.Histogram().DataPoints()) } return diffs } @@ -134,7 +128,7 @@ func diffMetricDescriptor( return diffValues(diffs, expected.DataType(), actual.DataType(), "Metric Type") } -func diffDoublePts( +func diffNumberPts( diffs []*MetricDiff, expected pdata.NumberDataPointSlice, actual pdata.NumberDataPointSlice, @@ -145,21 +139,22 @@ func diffDoublePts( return diffs } for i := 0; i < expected.Len(); i++ { - diffs = diffDoublePt(diffs, expected.At(i), actual.At(i)) + diffs, mismatch = diffValues(diffs, expected.At(i).Type(), actual.At(i).Type(), "NumberDataPoint Value Type") + if mismatch { + return diffs + } + switch expected.At(i).Type() { + case pdata.MetricValueTypeInt: + diffs = diff(diffs, expected.At(i).IntVal(), actual.At(i).IntVal(), "NumberDataPoint Value") + case pdata.MetricValueTypeDouble: + diffs = diff(diffs, expected.At(i).DoubleVal(), actual.At(i).DoubleVal(), "NumberDataPoint Value") + } + diffExemplars(diffs, expected.At(i).Exemplars(), actual.At(i).Exemplars()) } return diffs } -func diffDoublePt( - diffs []*MetricDiff, - expected pdata.NumberDataPoint, - actual pdata.NumberDataPoint, -) []*MetricDiff { - diffs = diff(diffs, expected.DoubleVal(), actual.DoubleVal(), "NumberDataPoint value") - return diffDoubleExemplars(diffs, expected.Exemplars(), actual.Exemplars()) -} - -func diffDoubleHistogramPts( +func diffHistogramPts( diffs []*MetricDiff, expected pdata.HistogramDataPointSlice, actual pdata.HistogramDataPointSlice, @@ -185,10 +180,10 @@ func diffDoubleHistogramPt( diffs = diff(diffs, expected.BucketCounts(), actual.BucketCounts(), "HistogramDataPoint BucketCounts") diffs = diff(diffs, expected.ExplicitBounds(), actual.ExplicitBounds(), "HistogramDataPoint ExplicitBounds") // todo LabelsMap() - return diffDoubleExemplars(diffs, expected.Exemplars(), actual.Exemplars()) + return diffExemplars(diffs, expected.Exemplars(), actual.Exemplars()) } -func diffDoubleExemplars( +func diffExemplars( diffs []*MetricDiff, expected pdata.ExemplarSlice, actual pdata.ExemplarSlice, @@ -199,35 +194,17 @@ func diffDoubleExemplars( return diffs } for i := 0; i < expected.Len(); i++ { - diffs = diff(diffs, expected.At(i).DoubleVal(), actual.At(i).DoubleVal(), "Exemplar Value") + diffs = diff(diffs, expected.At(i).Type(), actual.At(i).Type(), "Exemplar Value Type") + switch expected.At(i).Type() { + case pdata.MetricValueTypeInt: + diffs = diff(diffs, expected.At(i).IntVal(), actual.At(i).IntVal(), "Exemplar Value") + case pdata.MetricValueTypeDouble: + diffs = diff(diffs, expected.At(i).DoubleVal(), actual.At(i).DoubleVal(), "Exemplar Value") + } } return diffs } -func diffIntPts( - diffs []*MetricDiff, - expected pdata.IntDataPointSlice, - actual pdata.IntDataPointSlice, -) []*MetricDiff { - var mismatch bool - diffs, mismatch = diffValues(diffs, expected.Len(), actual.Len(), "IntDataPointSlice len") - if mismatch { - return diffs - } - for i := 0; i < expected.Len(); i++ { - diffs = diffIntPt(diffs, expected.At(i), actual.At(i)) - } - return diffs -} - -func diffIntPt( - diffs []*MetricDiff, - expected pdata.IntDataPoint, - actual pdata.IntDataPoint, -) []*MetricDiff { - return diff(diffs, expected.Value(), actual.Value(), "IntDataPoint value") -} - func diffResource(diffs []*MetricDiff, expected pdata.Resource, actual pdata.Resource) []*MetricDiff { return diffAttrs(diffs, expected.Attributes(), actual.Attributes()) } diff --git a/testbed/correctness/metrics/metric_diff_test.go b/testbed/correctness/metrics/metric_diff_test.go index 1398fc4431b..125b3d3faff 100644 --- a/testbed/correctness/metrics/metric_diff_test.go +++ b/testbed/correctness/metrics/metric_diff_test.go @@ -61,16 +61,16 @@ func TestDifferentNumPts(t *testing.T) { assert.Len(t, diffs, 1) } -func TestDifferentPtTypes(t *testing.T) { +func TestDifferentPtValueTypes(t *testing.T) { expected := goldendataset.MetricsFromCfg(goldendataset.DefaultCfg()) cfg := goldendataset.DefaultCfg() - cfg.MetricDescriptorType = pdata.MetricDataTypeGauge + cfg.MetricValueType = pdata.MetricValueTypeDouble actual := goldendataset.MetricsFromCfg(cfg) diffs := diffMetricData(expected, actual) assert.Len(t, diffs, 1) } -func TestDoubleHistogram(t *testing.T) { +func TestHistogram(t *testing.T) { cfg1 := goldendataset.DefaultCfg() cfg1.MetricDescriptorType = pdata.MetricDataTypeHistogram expected := goldendataset.MetricsFromCfg(cfg1) diff --git a/testbed/tests/resource_processor_test.go b/testbed/tests/resource_processor_test.go index d6bc7a88edf..d8ad251a932 100644 --- a/testbed/tests/resource_processor_test.go +++ b/testbed/tests/resource_processor_test.go @@ -36,8 +36,8 @@ var ( m.SetName("metric-name") m.SetDescription("metric-description") m.SetUnit("metric-unit") - m.SetDataType(pdata.MetricDataTypeIntGauge) - m.IntGauge().DataPoints().AppendEmpty().SetValue(0) + m.SetDataType(pdata.MetricDataTypeGauge) + m.Gauge().DataPoints().AppendEmpty().SetIntVal(0) return md }() @@ -48,8 +48,8 @@ var ( m.SetName("metric-name") m.SetDescription("metric-description") m.SetUnit("metric-unit") - m.SetDataType(pdata.MetricDataTypeIntGauge) - m.IntGauge().DataPoints().AppendEmpty().SetValue(0) + m.SetDataType(pdata.MetricDataTypeGauge) + m.Gauge().DataPoints().AppendEmpty().SetIntVal(0) return md }() )