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

Commit

Permalink
add zero bucket if not present. (#89)
Browse files Browse the repository at this point in the history
* add zero bucket if not present.

* rename functions.
  • Loading branch information
rghetia committed Feb 27, 2019
1 parent d2e06c1 commit 14131f5
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 12 deletions.
6 changes: 3 additions & 3 deletions equivalence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func TestEquivalenceStatsVsMetricsUploads(t *testing.T) {
View: &view.View{
Name: "ocagent.io/latency",
Description: "The latency of the various methods",
Aggregation: view.Distribution(0, 100, 500, 1000, 2000, 4000, 8000, 16000),
Aggregation: view.Distribution(100, 500, 1000, 2000, 4000, 8000, 16000),
Measure: mLatencyMs,
},
Rows: []*view.Row{
Expand All @@ -192,9 +192,9 @@ func TestEquivalenceStatsVsMetricsUploads(t *testing.T) {
Min: 100,
Max: 500,
Mean: 125.9,
CountPerBucket: []int64{0, 0, 1, 0, 0, 0, 0, 0},
CountPerBucket: []int64{0, 1, 0, 0, 0, 0, 0},
ExemplarsPerBucket: []*exemplar.Exemplar{
nil, nil,
nil,
{
Value: 125.9, Timestamp: startTime.Add(time.Duration(1+i) * time.Second),
},
Expand Down
10 changes: 8 additions & 2 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,22 +461,28 @@ func protoToMetricPoint(value interface{}) (*monitoringpb.TypedValue, error) {
Count: dv.Count,
Mean: mean,
SumOfSquaredDeviation: dv.SumOfSquaredDeviation,
BucketCounts: bucketCounts(dv.Buckets),
},
}

insertZeroBound := false
if bopts := dv.BucketOptions; bopts != nil && bopts.Type != nil {
bexp, ok := bopts.Type.(*metricspb.DistributionValue_BucketOptions_Explicit_)
if ok && bexp != nil && bexp.Explicit != nil {
insertZeroBound = shouldInsertZeroBound(bexp.Explicit.Bounds...)
mv.DistributionValue.BucketOptions = &distributionpb.Distribution_BucketOptions{
Options: &distributionpb.Distribution_BucketOptions_ExplicitBuckets{
ExplicitBuckets: &distributionpb.Distribution_BucketOptions_Explicit{
Bounds: bexp.Explicit.Bounds[:],
// The first bucket bound should be 0.0 because the Metrics first bucket is
// [0, first_bound) but Stackdriver monitoring bucket bounds begin with -infinity
// (first bucket is (-infinity, 0))
Bounds: addZeroBoundOnCondition(insertZeroBound, bexp.Explicit.Bounds...),
},
},
}
}
}
mv.DistributionValue.BucketCounts = addZeroBucketCountOnCondition(insertZeroBound, bucketCounts(dv.Buckets)...)

}
tval = &monitoringpb.TypedValue{Value: mv}
}
Expand Down
6 changes: 4 additions & 2 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,13 @@ func TestProtoMetricToCreateTimeSeriesRequest(t *testing.T) {
Sum: 11.9,
SumOfSquaredDeviation: 0,
Buckets: []*metricspb.DistributionValue_Bucket{
{}, {Count: 1}, {}, {}, {},
{Count: 1}, {}, {}, {},
},
BucketOptions: &metricspb.DistributionValue_BucketOptions{
Type: &metricspb.DistributionValue_BucketOptions_Explicit_{
Explicit: &metricspb.DistributionValue_BucketOptions_Explicit{
Bounds: []float64{0, 10, 20, 30, 40},
// Without zero bucket in
Bounds: []float64{10, 20, 30, 40},
},
},
},
Expand Down Expand Up @@ -322,6 +323,7 @@ func TestProtoMetricsToMonitoringMetrics_fromProtoPoint(t *testing.T) {
BucketOptions: &metricspb.DistributionValue_BucketOptions{
Type: &metricspb.DistributionValue_BucketOptions_Explicit_{
Explicit: &metricspb.DistributionValue_BucketOptions_Explicit{
// With zero bucket in
Bounds: []float64{0, 10, 20, 30, 40},
},
},
Expand Down
26 changes: 24 additions & 2 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ func newTypedValue(vd *view.View, r *view.Row) *monitoringpb.TypedValue {
}}
}
case *view.DistributionData:
insertZeroBound := shouldInsertZeroBound(vd.Aggregation.Buckets...)
return &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DistributionValue{
DistributionValue: &distributionpb.Distribution{
Count: v.Count,
Expand All @@ -436,11 +437,11 @@ func newTypedValue(vd *view.View, r *view.Row) *monitoringpb.TypedValue {
BucketOptions: &distributionpb.Distribution_BucketOptions{
Options: &distributionpb.Distribution_BucketOptions_ExplicitBuckets{
ExplicitBuckets: &distributionpb.Distribution_BucketOptions_Explicit{
Bounds: vd.Aggregation.Buckets,
Bounds: addZeroBoundOnCondition(insertZeroBound, vd.Aggregation.Buckets...),
},
},
},
BucketCounts: v.CountPerBucket,
BucketCounts: addZeroBucketCountOnCondition(insertZeroBound, v.CountPerBucket...),
},
}}
case *view.LastValueData:
Expand All @@ -458,6 +459,27 @@ func newTypedValue(vd *view.View, r *view.Row) *monitoringpb.TypedValue {
return nil
}

func shouldInsertZeroBound(bounds ...float64) bool {
if len(bounds) > 0 && bounds[0] != 0.0 {
return true
}
return false
}

func addZeroBucketCountOnCondition(insert bool, counts ...int64) []int64 {
if insert {
return append([]int64{0}, counts...)
}
return counts
}

func addZeroBoundOnCondition(insert bool, bounds ...float64) []float64 {
if insert {
return append([]float64{0.0}, bounds...)
}
return bounds
}

func (e *statsExporter) metricType(v *view.View) string {
if formatter := e.o.GetMetricType; formatter != nil {
return formatter(v)
Expand Down
70 changes: 67 additions & 3 deletions stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func TestExporter_makeReq(t *testing.T) {
},
},
{
name: "dist agg + time window",
name: "dist agg + time window - without zero bucket",
projID: "proj-id",
vd: newTestDistViewData(distView, start, end),
want: []*monitoringpb.CreateTimeSeriesRequest{{
Expand Down Expand Up @@ -435,8 +435,54 @@ func TestExporter_makeReq(t *testing.T) {
BucketOptions: &distribution.Distribution_BucketOptions{
Options: &distribution.Distribution_BucketOptions_ExplicitBuckets{
ExplicitBuckets: &distribution.Distribution_BucketOptions_Explicit{
Bounds: []float64{2.0, 4.0, 7.0}}}},
BucketCounts: []int64{2, 2, 1}},
Bounds: []float64{0.0, 2.0, 4.0, 7.0}}}},
BucketCounts: []int64{0, 2, 2, 1}},
}},
},
},
},
},
}},
},
{
name: "dist agg + time window + zero bucket",
projID: "proj-id",
vd: newTestDistViewData(distView, start, end),
want: []*monitoringpb.CreateTimeSeriesRequest{{
Name: monitoring.MetricProjectPath("proj-id"),
TimeSeries: []*monitoringpb.TimeSeries{
{
Metric: &metricpb.Metric{
Type: "custom.googleapis.com/opencensus/distview",
Labels: map[string]string{
opencensusTaskKey: taskValue,
},
},
Resource: &monitoredrespb.MonitoredResource{
Type: "global",
},
Points: []*monitoringpb.Point{
{
Interval: &monitoringpb.TimeInterval{
StartTime: &timestamp.Timestamp{
Seconds: start.Unix(),
Nanos: int32(start.Nanosecond()),
},
EndTime: &timestamp.Timestamp{
Seconds: end.Unix(),
Nanos: int32(end.Nanosecond()),
},
},
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DistributionValue{
DistributionValue: &distribution.Distribution{
Count: 5,
Mean: 3.0,
SumOfSquaredDeviation: 1.5,
BucketOptions: &distribution.Distribution_BucketOptions{
Options: &distribution.Distribution_BucketOptions_ExplicitBuckets{
ExplicitBuckets: &distribution.Distribution_BucketOptions_Explicit{
Bounds: []float64{0.0, 2.0, 4.0, 7.0}}}},
BucketCounts: []int64{0, 2, 2, 1}},
}},
},
},
Expand Down Expand Up @@ -1411,3 +1457,21 @@ func newTestDistViewData(v *view.View, start, end time.Time) *view.Data {
End: end,
}
}

func newTestDistViewDataWithZeroBucket(v *view.View, start, end time.Time) *view.Data {
return &view.Data{
View: v,
Rows: []*view.Row{
{Data: &view.DistributionData{
Count: 5,
Min: 1,
Max: 7,
Mean: 3,
SumOfSquaredDev: 1.5,
CountPerBucket: []int64{0, 2, 2, 1},
}},
},
Start: start,
End: end,
}
}

0 comments on commit 14131f5

Please sign in to comment.