Skip to content

Commit

Permalink
[proto3] Move more protos to v3.
Browse files Browse the repository at this point in the history
Following packages are fully moved: metrics/..., and validators/...
  • Loading branch information
manugarg committed Aug 14, 2023
1 parent 4eba348 commit 7c8e51a
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 119 deletions.
12 changes: 12 additions & 0 deletions metrics/dist.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ func NewDistributionFromProto(distProto *distpb.Dist) (*Distribution, error) {

case *distpb.Dist_ExponentialBuckets:
expb := distProto.GetExponentialBuckets()
if expb.Base == 0 {
expb.Base = 2
}
if expb.Base < 1.01 {
return nil, fmt.Errorf("exponential distribution's base (%f) should be at least 1.01", expb.Base)
}
if expb.ScaleFactor == 0 {
expb.ScaleFactor = 1
}
if expb.NumBuckets == 0 {
expb.NumBuckets = 20
}
return NewExponentialDistribution(float64(expb.GetBase()), float64(expb.GetScaleFactor()), int(expb.GetNumBuckets()))
}

Expand Down
67 changes: 46 additions & 21 deletions metrics/dist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
"strings"
"testing"

"github.com/golang/protobuf/proto"
distpb "github.com/cloudprober/cloudprober/metrics/proto"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/encoding/prototext"
)

func verifyBucketCount(t *testing.T, d *Distribution, indices []int, counts []int64) {
Expand All @@ -33,28 +34,52 @@ func verifyBucketCount(t *testing.T, d *Distribution, indices []int, counts []in
}
}

func protoToDist(t *testing.T, testDistProtoText string) *Distribution {
testDistProto := &distpb.Dist{}
if err := proto.UnmarshalText(testDistProtoText, testDistProto); err != nil {
t.Errorf("Failed parsing distribution proto text: %s. Err: %v", testDistProtoText, err)
return nil
}
d, err := NewDistributionFromProto(testDistProto)
if err != nil {
t.Errorf("Error while creating distribution from the protobuf: %s. Err: %v", testDistProtoText, err)
return nil
func TestNewDistributionFromProto(t *testing.T) {
tests := []struct {
inputProto string
wantError bool
wantLowerBounds []float64
}{
{
inputProto: "explicit_buckets: \"1,2,4,8,16,32\"",
wantLowerBounds: []float64{math.Inf(-1), 1, 2, 4, 8, 16, 32},
},
{
inputProto: "exponential_buckets {}",
wantLowerBounds: []float64{math.Inf(-1), 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288},
},
{
inputProto: `exponential_buckets {
scale_factor: 0.5
num_buckets: 10
}`,
wantLowerBounds: []float64{math.Inf(-1), 0, 0.5, 1, 2, 4, 8, 16, 32, 64, 128, 256},
},
{
inputProto: `exponential_buckets {
scale_factor: 0.5
base: 1,
num_buckets: 10
}`,
wantError: true,
},
}
return d
}

func TestNewDistributionFromProto(t *testing.T) {
testDistProtoText := `
explicit_buckets: "1,2,4,8,16,32"
`
expectedLowerBounds := []float64{math.Inf(-1), 1, 2, 4, 8, 16, 32}
d := protoToDist(t, testDistProtoText)
if !reflect.DeepEqual(d.lowerBounds, expectedLowerBounds) {
t.Errorf("Unexpected lower bounds from proto. d.lowerBounds=%v, want=%v.", d.lowerBounds, expectedLowerBounds)
for _, test := range tests {
t.Run(test.inputProto, func(t *testing.T) {
testDistProto := &distpb.Dist{}
prototext.Unmarshal([]byte(test.inputProto), testDistProto)
d, err := NewDistributionFromProto(testDistProto)

if (err != nil) != test.wantError {
t.Errorf("NewDistributionFromProto() error = %v, wantErr %v", err, test.wantError)
}

if test.wantError {
return
}
assert.Equal(t, test.wantLowerBounds, d.lowerBounds)
})
}
}

Expand Down
57 changes: 25 additions & 32 deletions metrics/proto/dist.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions metrics/proto/dist.proto
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
syntax = "proto2";
syntax = "proto3";

package cloudprober.metrics;

Expand Down Expand Up @@ -26,7 +26,7 @@ message Dist {
// bucket[num_buckets+1] covers [scale_factor*base^(num_buckets−1), +Inf)
// NB: Base must be at least 1.01.
message ExponentialBuckets {
optional float scale_factor = 1 [default = 1.0];
optional float base = 2 [default = 2];
optional uint32 num_buckets = 3 [default = 20];
float scale_factor = 1; // default = 1.0
float base = 2; // default = 2
uint32 num_buckets = 3; //default = 20
}
6 changes: 3 additions & 3 deletions metrics/proto/dist_proto_gen.cue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package proto
// bucket[num_buckets+1] covers [scale_factor*base^(num_buckets−1), +Inf)
// NB: Base must be at least 1.01.
#ExponentialBuckets: {
scaleFactor?: float32 @protobuf(1,float,name=scale_factor,"default=1.0")
base?: float32 @protobuf(2,float,"default=2")
numBuckets?: uint32 @protobuf(3,uint32,name=num_buckets,"default=20")
scaleFactor?: float32 @protobuf(1,float,name=scale_factor) // default = 1.0
base?: float32 @protobuf(2,float) // default = 2
numBuckets?: uint32 @protobuf(3,uint32,name=num_buckets) //default = 20
}
2 changes: 1 addition & 1 deletion probes/dns/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestValidator(t *testing.T) {
} {
valPb := []*validatorpb.Validator{
{
Name: proto.String(tst.name),
Name: tst.name,
Type: &validatorpb.Validator_Regex{Regex: tst.pattern},
},
}
Expand Down
74 changes: 43 additions & 31 deletions validators/http/proto/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion validators/http/proto/config.proto
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
syntax = "proto2";
syntax = "proto3";

package cloudprober.validators.http;

Expand Down
Loading

0 comments on commit 7c8e51a

Please sign in to comment.