Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use generic Sum, Gauge, and DataPoint value removing Value, Int64, and Float64 from metricdata #3036

Merged
merged 4 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 8 additions & 24 deletions sdk/metric/metricdata/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,28 @@ type Aggregation interface {
}

// Gauge represents a measurement of the current value of an instrument.
type Gauge struct {
type Gauge[N int64 | float64] struct {
// DataPoints reprents individual aggregated measurements with unique Attributes.
DataPoints []DataPoint
DataPoints []DataPoint[N]
}

func (Gauge) privateAggregation() {}
func (Gauge[N]) privateAggregation() {}

// Sum represents the sum of all measurements of values from an instrument.
type Sum struct {
type Sum[N int64 | float64] struct {
// DataPoints reprents individual aggregated measurements with unique Attributes.
DataPoints []DataPoint
DataPoints []DataPoint[N]
// Temporality describes if the aggregation is reported as the change from the
// last report time, or the cumulative changes since a fixed start time.
Temporality Temporality
// IsMonotonic represents if this aggregation only increases or decreases.
IsMonotonic bool
}

func (Sum) privateAggregation() {}
func (Sum[N]) privateAggregation() {}

// DataPoint is a single data point in a timeseries.
type DataPoint struct {
type DataPoint[N int64 | float64] struct {
// Attributes is the set of key value pairs that uniquely identify the
// timeseries.
Attributes attribute.Set
Expand All @@ -95,25 +95,9 @@ type DataPoint struct {
// Time is the time when the timeseries was recorded. (optional)
Time time.Time
// Value is the value of this data point.
Value Value
Value N
}

// Value is a int64 or float64. All Values created by the sdk will be either
// Int64 or Float64.
type Value interface {
privateValue()
}

// Int64 is a container for an int64 value.
type Int64 int64

func (Int64) privateValue() {}

// Float64 is a container for a float64 value.
type Float64 float64

func (Float64) privateValue() {}

// Histogram represents the histogram of all measurements of values from an instrument.
type Histogram struct {
// DataPoints reprents individual aggregated measurements with unique Attributes.
Expand Down
44 changes: 23 additions & 21 deletions sdk/metric/metricdata/metricdatatest/assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ import (

// Datatypes are the concrete data-types the metricdata package provides.
type Datatypes interface {
metricdata.DataPoint | metricdata.Float64 | metricdata.Gauge | metricdata.Histogram | metricdata.HistogramDataPoint | metricdata.Int64 | metricdata.Metrics | metricdata.ResourceMetrics | metricdata.ScopeMetrics | metricdata.Sum
metricdata.DataPoint[float64] |
metricdata.DataPoint[int64] |
metricdata.Gauge[float64] |
metricdata.Gauge[int64] |
metricdata.Histogram |
metricdata.HistogramDataPoint |
metricdata.Metrics |
metricdata.ResourceMetrics |
metricdata.ScopeMetrics |
metricdata.Sum[float64] |
metricdata.Sum[int64]

// Interface types are not allowed in union types, therefore the
// Aggregation and Value type from metricdata are not included here.
Expand All @@ -44,26 +54,28 @@ func AssertEqual[T Datatypes](t *testing.T, expected, actual T) bool {

var r []string
switch e := interface{}(expected).(type) {
case metricdata.DataPoint:
r = equalDataPoints(e, aIface.(metricdata.DataPoint))
case metricdata.Float64:
r = equalFloat64(e, aIface.(metricdata.Float64))
case metricdata.Gauge:
r = equalGauges(e, aIface.(metricdata.Gauge))
case metricdata.DataPoint[int64]:
r = equalDataPoints(e, aIface.(metricdata.DataPoint[int64]))
case metricdata.DataPoint[float64]:
r = equalDataPoints(e, aIface.(metricdata.DataPoint[float64]))
case metricdata.Gauge[int64]:
r = equalGauges(e, aIface.(metricdata.Gauge[int64]))
case metricdata.Gauge[float64]:
r = equalGauges(e, aIface.(metricdata.Gauge[float64]))
case metricdata.Histogram:
r = equalHistograms(e, aIface.(metricdata.Histogram))
case metricdata.HistogramDataPoint:
r = equalHistogramDataPoints(e, aIface.(metricdata.HistogramDataPoint))
case metricdata.Int64:
r = equalInt64(e, aIface.(metricdata.Int64))
case metricdata.Metrics:
r = equalMetrics(e, aIface.(metricdata.Metrics))
case metricdata.ResourceMetrics:
r = equalResourceMetrics(e, aIface.(metricdata.ResourceMetrics))
case metricdata.ScopeMetrics:
r = equalScopeMetrics(e, aIface.(metricdata.ScopeMetrics))
case metricdata.Sum:
r = equalSums(e, aIface.(metricdata.Sum))
case metricdata.Sum[int64]:
r = equalSums(e, aIface.(metricdata.Sum[int64]))
case metricdata.Sum[float64]:
r = equalSums(e, aIface.(metricdata.Sum[float64]))
default:
// We control all types passed to this, panic to signal developers
// early they changed things in an incompatible way.
Expand All @@ -86,13 +98,3 @@ func AssertAggregationsEqual(t *testing.T, expected, actual metricdata.Aggregati
}
return true
}

// AssertValuesEqual asserts that two Values are equal.
func AssertValuesEqual(t *testing.T, expected, actual metricdata.Value) bool {
t.Helper()
if r := equalValues(expected, actual); len(r) > 0 {
t.Error(r)
return false
}
return true
}
30 changes: 13 additions & 17 deletions sdk/metric/metricdata/metricdatatest/assertion_fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,23 @@ func TestFailAssertEqual(t *testing.T) {
t.Run("ScopeMetrics", testFailDatatype(scopeMetricsA, scopeMetricsB))
t.Run("Metrics", testFailDatatype(metricsA, metricsB))
t.Run("Histogram", testFailDatatype(histogramA, histogramB))
t.Run("Sum", testFailDatatype(sumA, sumB))
t.Run("Gauge", testFailDatatype(gaugeA, gaugeB))
t.Run("SumInt64", testFailDatatype(sumInt64A, sumInt64B))
t.Run("SumFloat64", testFailDatatype(sumFloat64A, sumFloat64B))
t.Run("GaugeInt64", testFailDatatype(gaugeInt64A, gaugeInt64B))
t.Run("GaugeFloat64", testFailDatatype(gaugeFloat64A, gaugeFloat64B))
t.Run("HistogramDataPoint", testFailDatatype(histogramDataPointA, histogramDataPointB))
t.Run("DataPoint", testFailDatatype(dataPointsA, dataPointsB))
t.Run("Int64", testFailDatatype(int64A, int64B))
t.Run("Float64", testFailDatatype(float64A, float64B))
t.Run("DataPointInt64", testFailDatatype(dataPointInt64A, dataPointInt64B))
t.Run("DataPointFloat64", testFailDatatype(dataPointFloat64A, dataPointFloat64B))

}

func TestFailAssertAggregationsEqual(t *testing.T) {
AssertAggregationsEqual(t, sumA, nil)
AssertAggregationsEqual(t, sumA, gaugeA)
AssertAggregationsEqual(t, sumInt64A, nil)
AssertAggregationsEqual(t, sumFloat64A, gaugeFloat64A)
AssertAggregationsEqual(t, unknownAggregation{}, unknownAggregation{})
AssertAggregationsEqual(t, sumA, sumB)
AssertAggregationsEqual(t, gaugeA, gaugeB)
AssertAggregationsEqual(t, sumInt64A, sumInt64B)
AssertAggregationsEqual(t, sumFloat64A, sumFloat64B)
AssertAggregationsEqual(t, gaugeInt64A, gaugeInt64B)
AssertAggregationsEqual(t, gaugeFloat64A, gaugeFloat64B)
AssertAggregationsEqual(t, histogramA, histogramB)
}

func TestFailAssertValuesEqual(t *testing.T) {
AssertValuesEqual(t, int64A, nil)
AssertValuesEqual(t, int64A, float64A)
AssertValuesEqual(t, unknownValue{}, unknownValue{})
AssertValuesEqual(t, int64A, int64B)
AssertValuesEqual(t, float64A, float64B)
}
126 changes: 68 additions & 58 deletions sdk/metric/metricdata/metricdatatest/assertion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,34 @@ var (
attrA = attribute.NewSet(attribute.Bool("A", true))
attrB = attribute.NewSet(attribute.Bool("B", true))

float64A = metricdata.Float64(-1.0)
float64B = metricdata.Float64(2.0)

int64A = metricdata.Int64(-1)
int64B = metricdata.Int64(2)

startA = time.Now()
startB = startA.Add(time.Millisecond)
endA = startA.Add(time.Second)
endB = startB.Add(time.Second)

dataPointsA = metricdata.DataPoint{
dataPointInt64A = metricdata.DataPoint[int64]{
Attributes: attrA,
StartTime: startA,
Time: endA,
Value: int64A,
Value: -1,
}
dataPointFloat64A = metricdata.DataPoint[float64]{
Attributes: attrA,
StartTime: startA,
Time: endA,
Value: -1.0,
}
dataPointInt64B = metricdata.DataPoint[int64]{
Attributes: attrB,
StartTime: startB,
Time: endB,
Value: 2,
}
dataPointsB = metricdata.DataPoint{
dataPointFloat64B = metricdata.DataPoint[float64]{
Attributes: attrB,
StartTime: startB,
Time: endB,
Value: float64B,
Value: 2.0,
}

max, min = 99.0, 3.
Expand All @@ -80,18 +86,38 @@ var (
Sum: 3,
}

gaugeA = metricdata.Gauge{DataPoints: []metricdata.DataPoint{dataPointsA}}
gaugeB = metricdata.Gauge{DataPoints: []metricdata.DataPoint{dataPointsB}}
gaugeInt64A = metricdata.Gauge[int64]{
DataPoints: []metricdata.DataPoint[int64]{dataPointInt64A},
}
gaugeFloat64A = metricdata.Gauge[float64]{
DataPoints: []metricdata.DataPoint[float64]{dataPointFloat64A},
}
gaugeInt64B = metricdata.Gauge[int64]{
DataPoints: []metricdata.DataPoint[int64]{dataPointInt64B},
}
gaugeFloat64B = metricdata.Gauge[float64]{
DataPoints: []metricdata.DataPoint[float64]{dataPointFloat64B},
}

sumA = metricdata.Sum{
sumInt64A = metricdata.Sum[int64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint{dataPointsA},
DataPoints: []metricdata.DataPoint[int64]{dataPointInt64A},
}
sumB = metricdata.Sum{
Temporality: metricdata.DeltaTemporality,
IsMonotonic: false,
DataPoints: []metricdata.DataPoint{dataPointsB},
sumFloat64A = metricdata.Sum[float64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[float64]{dataPointFloat64A},
}
sumInt64B = metricdata.Sum[int64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[int64]{dataPointInt64B},
}
sumFloat64B = metricdata.Sum[float64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[float64]{dataPointFloat64B},
}

histogramA = metricdata.Histogram{
Expand All @@ -107,13 +133,13 @@ var (
Name: "A",
Description: "A desc",
Unit: unit.Dimensionless,
Data: sumA,
Data: sumInt64A,
}
metricsB = metricdata.Metrics{
Name: "B",
Description: "B desc",
Unit: unit.Bytes,
Data: gaugeB,
Data: gaugeFloat64B,
}

scopeMetricsA = metricdata.ScopeMetrics{
Expand Down Expand Up @@ -152,12 +178,13 @@ func TestAssertEqual(t *testing.T) {
t.Run("ScopeMetrics", testDatatype(scopeMetricsA, scopeMetricsB, equalScopeMetrics))
t.Run("Metrics", testDatatype(metricsA, metricsB, equalMetrics))
t.Run("Histogram", testDatatype(histogramA, histogramB, equalHistograms))
t.Run("Sum", testDatatype(sumA, sumB, equalSums))
t.Run("Gauge", testDatatype(gaugeA, gaugeB, equalGauges))
t.Run("SumInt64", testDatatype(sumInt64A, sumInt64B, equalSums[int64]))
t.Run("SumFloat64", testDatatype(sumFloat64A, sumFloat64B, equalSums[float64]))
t.Run("GaugeInt64", testDatatype(gaugeInt64A, gaugeInt64B, equalGauges[int64]))
t.Run("GaugeFloat64", testDatatype(gaugeFloat64A, gaugeFloat64B, equalGauges[float64]))
t.Run("HistogramDataPoint", testDatatype(histogramDataPointA, histogramDataPointB, equalHistogramDataPoints))
t.Run("DataPoint", testDatatype(dataPointsA, dataPointsB, equalDataPoints))
t.Run("Int64", testDatatype(int64A, int64B, equalInt64))
t.Run("Float64", testDatatype(float64A, float64B, equalFloat64))
t.Run("DataPointInt64", testDatatype(dataPointInt64A, dataPointInt64B, equalDataPoints[int64]))
t.Run("DataPointFloat64", testDatatype(dataPointFloat64A, dataPointFloat64B, equalDataPoints[float64]))
}

type unknownAggregation struct {
Expand All @@ -166,50 +193,33 @@ type unknownAggregation struct {

func TestAssertAggregationsEqual(t *testing.T) {
AssertAggregationsEqual(t, nil, nil)
AssertAggregationsEqual(t, sumA, sumA)
AssertAggregationsEqual(t, gaugeA, gaugeA)
AssertAggregationsEqual(t, sumInt64A, sumInt64A)
AssertAggregationsEqual(t, sumFloat64A, sumFloat64A)
AssertAggregationsEqual(t, gaugeInt64A, gaugeInt64A)
AssertAggregationsEqual(t, gaugeFloat64A, gaugeFloat64A)
AssertAggregationsEqual(t, histogramA, histogramA)

r := equalAggregations(sumA, nil)
r := equalAggregations(sumInt64A, nil)
assert.Len(t, r, 1, "should return nil comparison mismatch only")

r = equalAggregations(sumA, gaugeA)
r = equalAggregations(sumInt64A, gaugeInt64A)
assert.Len(t, r, 1, "should return with type mismatch only")

r = equalAggregations(unknownAggregation{}, unknownAggregation{})
assert.Len(t, r, 1, "should return with unknown aggregation only")

r = equalAggregations(sumA, sumB)
assert.Greaterf(t, len(r), 0, "%v == %v", sumA, sumB)

r = equalAggregations(gaugeA, gaugeB)
assert.Greaterf(t, len(r), 0, "%v == %v", gaugeA, gaugeB)

r = equalAggregations(histogramA, histogramB)
assert.Greaterf(t, len(r), 0, "%v == %v", histogramA, histogramB)
}

type unknownValue struct {
metricdata.Value
}
r = equalAggregations(sumInt64A, sumInt64B)
assert.Greaterf(t, len(r), 0, "%v == %v", sumInt64A, sumInt64B)

func TestAssertValuesEqual(t *testing.T) {
AssertValuesEqual(t, nil, nil)
AssertValuesEqual(t, int64A, int64A)
AssertValuesEqual(t, float64A, float64A)
r = equalAggregations(sumFloat64A, sumFloat64B)
assert.Greaterf(t, len(r), 0, "%v == %v", sumFloat64A, sumFloat64B)

r := equalValues(int64A, nil)
assert.Len(t, r, 1, "should return nil comparison mismatch only")
r = equalAggregations(gaugeInt64A, gaugeInt64B)
assert.Greaterf(t, len(r), 0, "%v == %v", gaugeInt64A, gaugeInt64B)

r = equalValues(int64A, float64A)
assert.Len(t, r, 1, "should return with type mismatch only")
r = equalAggregations(gaugeFloat64A, gaugeFloat64B)
assert.Greaterf(t, len(r), 0, "%v == %v", gaugeFloat64A, gaugeFloat64B)

r = equalValues(unknownValue{}, unknownValue{})
assert.Len(t, r, 1, "should return with unknown value only")

r = equalValues(int64A, int64B)
assert.Greaterf(t, len(r), 0, "%v == %v", int64A, int64B)

r = equalValues(float64A, float64B)
assert.Greaterf(t, len(r), 0, "%v == %v", float64A, float64B)
r = equalAggregations(histogramA, histogramB)
assert.Greaterf(t, len(r), 0, "%v == %v", histogramA, histogramB)
}
Loading