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

stats/opencensus: Fix flaky metrics test #6372

Merged
merged 4 commits into from
Jun 20, 2023
Merged
Changes from 1 commit
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
17 changes: 11 additions & 6 deletions stats/opencensus/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,21 @@ func distributionDataLatencyCount(vi *viewInformation, countWant int64, wantTags
}

// waitForServerCompletedRPCs waits until both Unary and Streaming metric rows
// appear for server completed RPC's view (by checking for length of rows to be
// 2). Returns an error if both the Unary and Streaming metric not found within
// the passed context's timeout.
func waitForServerCompletedRPCs(ctx context.Context, fe *fakeExporter) error {
// appear for server completed RPC's view. Returns an error if the Unary and
// Streaming metric are not found within the passed context's timeout.
func waitForServerCompletedRPCs(ctx context.Context) error {
for ; ctx.Err() == nil; <-time.After(time.Millisecond) {
arvindbr8 marked this conversation as resolved.
Show resolved Hide resolved
rows, err := view.RetrieveData("grpc.io/server/completed_rpcs")
arvindbr8 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
continue
}
if len(rows) == 2 {
m := make(map[string]bool)
for _, row := range rows {
for _, tag := range row.Tags {
m[tag.Value] = true
}
}
if m["grpc.testing.TestService/UnaryCall"] && m["grpc.testing.TestService/FullDuplexCall"] {
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @zasweq, but please note that this new implementation doesn't enforce anymore your initial thought of having the expected metrics in 2 different rows. I don't know if that makes a difference.

May I also suggest to return fast if the tags are found without looping through the entire arrays (well, the looping will not be expensive since the test data set here is small), with something similar to

	unaryMetricFound := false
	streamingMetricFound := false
	for _, row := range rows {
		for _, tag := range row.Tags {
			if tag.Value == "grpc.testing.TestService/UnaryCall" {
				unaryMetricFound = true
			} else if tag.Value == "grpc.testing.TestService/FullDuplexCall" {
				streamingMetricFound = true
			}
			if unaryMetricFound && streamingMetricFound {
				return nil
			}
		}
	}

or (if the metrics are expected to be in 2 different rows)

	unaryMetricFound := false
	streamingMetricFound := false
	for _, row := range rows {
		for _, tag := range row.Tags {
			if tag.Value == "grpc.testing.TestService/UnaryCall" {
				unaryMetricFound = true
				break
			} else if tag.Value == "grpc.testing.TestService/FullDuplexCall" {
				streamingMetricFound = true
				break
			}
		}
		if unaryMetricFound && streamingMetricFound {
			return nil
		}
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is verified right after by the want declared having only the two rows declared separately and checked in cmp.Diff. I went ahead and switched to your second solution though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for suggestion.

}
Expand Down Expand Up @@ -1008,7 +1013,7 @@ func (s) TestAllMetricsOneFunction(t *testing.T) {
// Streaming calls with respect to the RPC returning client side. Thus, add
// a sync point at the global view package level for these two rows to be
// recorded, which will be synchronously uploaded to exporters right after.
if err := waitForServerCompletedRPCs(ctx, fe); err != nil {
if err := waitForServerCompletedRPCs(ctx); err != nil {
t.Fatal(err)
}
view.Unregister(allViews...)
Expand Down