Skip to content

Commit

Permalink
Convert testbed to new Number metrics (#3719)
Browse files Browse the repository at this point in the history
* Convert goldentest and filterproc to new Number metrics

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>

* Convert testbed to new Number metrics

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Jul 27, 2021
1 parent 3fdb1de commit 193e77d
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 259 deletions.
40 changes: 15 additions & 25 deletions internal/goldendataset/metrics_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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())
}
}
Expand Down
8 changes: 4 additions & 4 deletions internal/goldendataset/metrics_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 11 additions & 3 deletions internal/goldendataset/pict_metrics_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
14 changes: 9 additions & 5 deletions internal/goldendataset/pict_metrics_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package goldendataset
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/model/pdata"
Expand Down Expand Up @@ -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,
},
},
Expand All @@ -61,6 +63,7 @@ func TestPICTtoCfg(t *testing.T) {
NumResourceAttrs: 1,
NumPtsPerMetric: 1,
MetricDescriptorType: pdata.MetricDataTypeGauge,
MetricValueType: pdata.MetricValueTypeDouble,
NumPtLabels: 1,
},
},
Expand All @@ -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)
})
}
}
32 changes: 0 additions & 32 deletions internal/processor/filterexpr/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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++ {
Expand Down Expand Up @@ -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++ {
Expand Down

0 comments on commit 193e77d

Please sign in to comment.