Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Make CountData and SumData struct (#669)
Browse files Browse the repository at this point in the history
It makes it easier for the user to use these types,
because they don't have to do type casting to work
with the values.
  • Loading branch information
rakyll authored Apr 4, 2018
1 parent 202f755 commit 2a1d4f6
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 66 deletions.
4 changes: 2 additions & 2 deletions exporter/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
func (c *collector) toMetric(desc *prometheus.Desc, v *view.View, row *view.Row) (prometheus.Metric, error) {
switch data := row.Data.(type) {
case *view.CountData:
return prometheus.NewConstMetric(desc, prometheus.CounterValue, float64(*data), tagValues(row.Tags)...)
return prometheus.NewConstMetric(desc, prometheus.CounterValue, float64(data.Value), tagValues(row.Tags)...)

case *view.DistributionData:
points := make(map[float64]uint64)
Expand Down Expand Up @@ -254,7 +254,7 @@ func (c *collector) toMetric(desc *prometheus.Desc, v *view.View, row *view.Row)
return prometheus.NewConstHistogram(desc, uint64(data.Count), data.Sum(), points, tagValues(row.Tags)...)

case *view.SumData:
return prometheus.NewConstMetric(desc, prometheus.UntypedValue, float64(*data), tagValues(row.Tags)...)
return prometheus.NewConstMetric(desc, prometheus.UntypedValue, data.Value, tagValues(row.Tags)...)

case *view.LastValueData:
return prometheus.NewConstMetric(desc, prometheus.UntypedValue, data.Value, tagValues(row.Tags)...)
Expand Down
8 changes: 4 additions & 4 deletions exporter/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func newView(measureName string, agg *view.Aggregation) *view.View {

func TestOnlyCumulativeWindowSupported(t *testing.T) {
// See Issue https://github.com/census-instrumentation/opencensus-go/issues/214.
count1 := view.CountData(1)
count1 := &view.CountData{Value: 1}
tests := []struct {
vds *view.Data
want int
Expand All @@ -59,7 +59,7 @@ func TestOnlyCumulativeWindowSupported(t *testing.T) {
vds: &view.Data{
View: newView("TestOnlyCumulativeWindowSupported/m2", view.Count()),
Rows: []*view.Row{
{Data: &count1},
{Data: count1},
},
},
want: 1,
Expand Down Expand Up @@ -126,9 +126,9 @@ func TestCollectNonRacy(t *testing.T) {
}()

for i := 0; i < 1e3; i++ {
count1 := view.CountData(1)
count1 := &view.CountData{Value: 1}
vds := []*view.Data{
{View: newView(fmt.Sprintf("TestCollectNonRacy/m2-%d", i), view.Count()), Rows: []*view.Row{{Data: &count1}}},
{View: newView(fmt.Sprintf("TestCollectNonRacy/m2-%d", i), view.Count()), Rows: []*view.Row{{Data: count1}}},
}
for _, v := range vds {
exp.ExportView(v)
Expand Down
6 changes: 3 additions & 3 deletions exporter/stackdriver/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,17 @@ func newTypedValue(vd *view.View, r *view.Row) *monitoringpb.TypedValue {
switch v := r.Data.(type) {
case *view.CountData:
return &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{
Int64Value: int64(*v),
Int64Value: v.Value,
}}
case *view.SumData:
switch vd.Measure.(type) {
case *stats.Int64Measure:
return &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{
Int64Value: int64(*v),
Int64Value: int64(v.Value),
}}
case *stats.Float64Measure:
return &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{
DoubleValue: float64(*v),
DoubleValue: v.Value,
}}
}
case *view.DistributionData:
Expand Down
32 changes: 16 additions & 16 deletions exporter/stackdriver/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ func TestExporter_makeReq(t *testing.T) {

start := time.Now()
end := start.Add(time.Minute)
count1 := view.CountData(10)
count2 := view.CountData(16)
sum1 := view.SumData(5.5)
sum2 := view.SumData(-11.1)
count1 := &view.CountData{Value: 10}
count2 := &view.CountData{Value: 16}
sum1 := &view.SumData{Value: 5.5}
sum2 := &view.SumData{Value: -11.1}
last1 := view.LastValueData{Value: 100}
last2 := view.LastValueData{Value: 200}
taskValue := getTaskValue()
Expand All @@ -110,7 +110,7 @@ func TestExporter_makeReq(t *testing.T) {
{
name: "count agg + timeline",
projID: "proj-id",
vd: newTestViewData(v, start, end, &count1, &count2),
vd: newTestViewData(v, start, end, count1, count2),
want: []*monitoringpb.CreateTimeSeriesRequest{{
Name: monitoring.MetricProjectPath("proj-id"),
TimeSeries: []*monitoringpb.TimeSeries{
Expand Down Expand Up @@ -178,7 +178,7 @@ func TestExporter_makeReq(t *testing.T) {
{
name: "sum agg + timeline",
projID: "proj-id",
vd: newTestViewData(v, start, end, &sum1, &sum2),
vd: newTestViewData(v, start, end, sum1, sum2),
want: []*monitoringpb.CreateTimeSeriesRequest{{
Name: monitoring.MetricProjectPath("proj-id"),
TimeSeries: []*monitoringpb.TimeSeries{
Expand Down Expand Up @@ -387,13 +387,13 @@ func TestExporter_makeReq_batching(t *testing.T) {
},
}

count1 := view.CountData(10)
count2 := view.CountData(16)
count1 := &view.CountData{Value: 10}
count2 := &view.CountData{Value: 16}

for _, tt := range tests {
var vds []*view.Data
for i := 0; i < tt.iter; i++ {
vds = append(vds, newTestViewData(v, time.Now(), time.Now(), &count1, &count2))
vds = append(vds, newTestViewData(v, time.Now(), time.Now(), count1, count2))
}

e := &statsExporter{}
Expand Down Expand Up @@ -540,8 +540,8 @@ func TestExporter_createMeasure(t *testing.T) {
Aggregation: view.Sum(),
}

data := view.CountData(0)
vd := newTestViewData(v, time.Now(), time.Now(), &data, &data)
data := &view.CountData{Value: 0}
vd := newTestViewData(v, time.Now(), time.Now(), data, data)

e := &statsExporter{
createdViews: make(map[string]*metricpb.MetricDescriptor),
Expand Down Expand Up @@ -616,8 +616,8 @@ func TestExporter_createMeasure_CountAggregation(t *testing.T) {
Aggregation: view.Count(),
}

data := view.CountData(0)
vd := newTestViewData(v, time.Now(), time.Now(), &data, &data)
data := &view.CountData{Value: 0}
vd := newTestViewData(v, time.Now(), time.Now(), data, data)

e := &statsExporter{
createdViews: make(map[string]*metricpb.MetricDescriptor),
Expand Down Expand Up @@ -684,8 +684,8 @@ func TestExporter_makeReq_withCustomMonitoredResource(t *testing.T) {

start := time.Now()
end := start.Add(time.Minute)
count1 := view.CountData(10)
count2 := view.CountData(16)
count1 := &view.CountData{Value: 10}
count2 := &view.CountData{Value: 16}
taskValue := getTaskValue()

resource := &monitoredrespb.MonitoredResource{
Expand All @@ -702,7 +702,7 @@ func TestExporter_makeReq_withCustomMonitoredResource(t *testing.T) {
{
name: "count agg timeline",
projID: "proj-id",
vd: newTestViewData(v, start, end, &count1, &count2),
vd: newTestViewData(v, start, end, count1, count2),
want: []*monitoringpb.CreateTimeSeriesRequest{{
Name: monitoring.MetricProjectPath("proj-id"),
TimeSeries: []*monitoringpb.TimeSeries{
Expand Down
3 changes: 1 addition & 2 deletions plugin/ocgrpc/server_stats_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,7 @@ func TestServerDefaultCollections(t *testing.T) {
}

func newCountData(v int) *view.CountData {
cav := view.CountData(v)
return &cav
return &view.CountData{Value: int64(v)}
}

func newDistributionData(countPerBucket []int64, count int64, min, max, mean, sumOfSquaredDev float64) *view.DistributionData {
Expand Down
2 changes: 1 addition & 1 deletion plugin/ochttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestClient(t *testing.T) {
var count int64
switch data := data.(type) {
case *view.CountData:
count = *(*int64)(data)
count = data.Value
case *view.DistributionData:
count = data.Count
default:
Expand Down
2 changes: 1 addition & 1 deletion plugin/ochttp/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestHandlerStatsCollection(t *testing.T) {
var sum float64
switch data := data.(type) {
case *view.CountData:
count = int(*data)
count = int(data.Value)
case *view.DistributionData:
count = int(data.Count)
sum = data.Sum()
Expand Down
4 changes: 2 additions & 2 deletions stats/view/aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ var (
aggCount = &Aggregation{
Type: AggTypeCount,
newData: func() AggregationData {
return newCountData(0)
return &CountData{}
},
}
aggSum = &Aggregation{
Type: AggTypeSum,
newData: func() AggregationData {
return newSumData(0)
return &SumData{}
},
}
)
Expand Down
26 changes: 10 additions & 16 deletions stats/view/aggregation_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,18 @@ const epsilon = 1e-9
// A count aggregation processes data and counts the recordings.
//
// Most users won't directly access count data.
type CountData int64

func newCountData(v int64) *CountData {
tmp := CountData(v)
return &tmp
type CountData struct {
Value int64
}

func (a *CountData) isAggregationData() bool { return true }

func (a *CountData) addSample(v float64) {
*a = *a + 1
a.Value = a.Value + 1
}

func (a *CountData) clone() AggregationData {
return newCountData(int64(*a))
return &CountData{Value: a.Value}
}

func (a *CountData) equal(other AggregationData) bool {
Expand All @@ -58,36 +55,33 @@ func (a *CountData) equal(other AggregationData) bool {
return false
}

return int64(*a) == int64(*a2)
return a.Value == a2.Value
}

// SumData is the aggregated data for the Sum aggregation.
// A sum aggregation processes data and sums up the recordings.
//
// Most users won't directly access sum data.
type SumData float64

func newSumData(v float64) *SumData {
tmp := SumData(v)
return &tmp
type SumData struct {
Value float64
}

func (a *SumData) isAggregationData() bool { return true }

func (a *SumData) addSample(f float64) {
*a += SumData(f)
a.Value += f
}

func (a *SumData) clone() AggregationData {
return newSumData(float64(*a))
return &SumData{Value: a.Value}
}

func (a *SumData) equal(other AggregationData) bool {
a2, ok := other.(*SumData)
if !ok {
return false
}
return math.Pow(float64(*a)-float64(*a2), 2) < epsilon
return math.Pow(a.Value-a2.Value, 2) < epsilon
}

// DistributionData is the aggregated data for the
Expand Down
4 changes: 2 additions & 2 deletions stats/view/aggregation_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func TestDataClone(t *testing.T) {
}{
{
name: "count data",
src: newCountData(5),
src: &CountData{Value: 5},
},
{
name: "distribution data",
src: dist,
},
{
name: "sum data",
src: newSumData(65.7),
src: &SumData{Value: 65.7},
},
}
for _, tt := range tests {
Expand Down
4 changes: 2 additions & 2 deletions stats/view/view_measure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func TestMeasureFloat64AndInt64(t *testing.T) {

// We expect both views to return 7.2, as though we recorded on a single measure.

if got, want := float64(*sum1), 7.2; got != want {
if got, want := sum1.Value, 7.2; got != want {
t.Errorf("sum1 = %v; want %v", got, want)
}
if got, want := float64(*sum2), 7.2; got != want {
if got, want := sum2.Value, 7.2; got != want {
t.Errorf("sum2 = %v; want %v", got, want)
}
}
14 changes: 7 additions & 7 deletions stats/view/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func Test_View_MeasureFloat64_AggregationSum(t *testing.T) {
[]*Row{
{
[]tag.Tag{{Key: k1, Value: "v1"}},
newSumData(6),
&SumData{Value: 6},
},
},
},
Expand All @@ -239,11 +239,11 @@ func Test_View_MeasureFloat64_AggregationSum(t *testing.T) {
[]*Row{
{
[]tag.Tag{{Key: k1, Value: "v1"}},
newSumData(1),
&SumData{Value: 1},
},
{
[]tag.Tag{{Key: k2, Value: "v2"}},
newSumData(5),
&SumData{Value: 5},
},
},
},
Expand All @@ -259,19 +259,19 @@ func Test_View_MeasureFloat64_AggregationSum(t *testing.T) {
[]*Row{
{
[]tag.Tag{{Key: k1, Value: "v1"}},
newSumData(6),
&SumData{Value: 6},
},
{
[]tag.Tag{{Key: k1, Value: "v1 other"}},
newSumData(1),
&SumData{Value: 1},
},
{
[]tag.Tag{{Key: k2, Value: "v2"}},
newSumData(5),
&SumData{Value: 5},
},
{
[]tag.Tag{{Key: k1, Value: "v1"}, {Key: k2, Value: "v2"}},
newSumData(5),
&SumData{Value: 5},
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions stats/view/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func Test_Worker_RecordFloat64(t *testing.T) {
[]*Row{
{
[]tag.Tag{{Key: k1, Value: "v1"}, {Key: k2, Value: "v2"}},
newCountData(2),
&CountData{Value: 2},
},
},
nil,
Expand All @@ -182,7 +182,7 @@ func Test_Worker_RecordFloat64(t *testing.T) {
[]*Row{
{
[]tag.Tag{{Key: k1, Value: "v1"}, {Key: k2, Value: "v2"}},
newCountData(2),
&CountData{Value: 2},
},
},
nil,
Expand All @@ -192,7 +192,7 @@ func Test_Worker_RecordFloat64(t *testing.T) {
[]*Row{
{
[]tag.Tag{{Key: k1, Value: "v1"}, {Key: k2, Value: "v2"}},
newCountData(2),
&CountData{Value: 2},
},
},
nil,
Expand Down Expand Up @@ -375,7 +375,7 @@ func (e *countExporter) ExportView(vd *Data) {

e.Lock()
defer e.Unlock()
e.count = int64(*d)
e.count = d.Value
}

type vdExporter struct {
Expand Down
Loading

0 comments on commit 2a1d4f6

Please sign in to comment.