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

v2: Refactor metrics interceptor and fix tests #413

Merged
merged 1 commit into from
Apr 23, 2021

Conversation

ash2k
Copy link
Contributor

@ash2k ash2k commented Apr 21, 2021

I decided to have a look at the v2 branch as I'm interested in better Prometheus metrics. I found that stuff was in a very weird state so I tried to fix, simplify and clean it all up. See inline comments for some details. I have also found that tests fail and are not executed on macOS (#411).

Closes #412.

@google-cla
Copy link

google-cla bot commented Apr 21, 2021

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@codecov-commenter
Copy link

codecov-commenter commented Apr 21, 2021

Codecov Report

Merging #413 (3b0fd77) into v2 (a79558a) will decrease coverage by 27.52%.
The diff coverage is 38.77%.

❗ Current head 3b0fd77 differs from pull request most recent head 3bfba60. Consider uploading reports for the commit 3bfba60 to get more accurate results
Impacted file tree graph

@@             Coverage Diff             @@
##               v2     #413       +/-   ##
===========================================
- Coverage   83.58%   56.06%   -27.53%     
===========================================
  Files          30       38        +8     
  Lines         932     1336      +404     
===========================================
- Hits          779      749       -30     
- Misses        114      515      +401     
- Partials       39       72       +33     
Impacted Files Coverage Δ
chain.go 0.00% <ø> (-90.91%) ⬇️
interceptors/auth/auth.go 100.00% <ø> (ø)
interceptors/auth/metadata.go 100.00% <ø> (ø)
interceptors/ratelimit/ratelimit.go 50.00% <0.00%> (-50.00%) ⬇️
interceptors/tags/fieldextractor.go 13.79% <0.00%> (-71.51%) ⬇️
testing/testpb/interceptor_suite.go 0.00% <0.00%> (ø)
testing/testpb/test.manual_extractfields.pb.go 0.00% <0.00%> (ø)
testing/testpb/test.manual_validator.pb.go 0.00% <0.00%> (ø)
testing/testpb/test.pb.go 36.92% <36.92%> (ø)
interceptors/logging/payload.go 67.18% <42.30%> (-15.43%) ⬇️
... and 49 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0df73fc...3bfba60. Read the comment docs.

@ash2k
Copy link
Contributor Author

ash2k commented Apr 21, 2021

@googlebot I signed it!

interceptors/client.go Show resolved Hide resolved
@@ -50,7 +50,7 @@ func (m *mockReportable) Equal(t *testing.T, expected []*mockReport) {
require.NoError(t, err)
continue
}
require.Equal(t, expected[i].postCalls[k].Error(), err.Error(), "%v %v", i, k)
require.EqualError(t, err, expected[i].postCalls[k].Error(), "%v %v", i, k)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there is a bug and err is nil, then this line panics. Now it does not and fails the test cleanly.

Copy link
Collaborator

Choose a reason for hiding this comment

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

👍🏽 Thanks a lot

@@ -276,7 +276,7 @@ func (s *ClientInterceptorTestSuite) TestListReporting() {
typ: ServerStream,
svcName: testpb.TestServiceFullName,
methodName: "PingList",
postCalls: []error{io.EOF},
postCalls: []error{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.

Because of the change in interceptors/client.go, it's called with nil now.

@@ -329,7 +329,9 @@ func (s *ClientInterceptorTestSuite) TestBiStreamingReporting() {
if err == io.EOF {
break
}
require.NoError(s.T(), err, "reading pingStream shouldn't fail")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is incorrect to use require in a goroutine other than the main test goroutine.

@@ -340,12 +342,12 @@ func (s *ClientInterceptorTestSuite) TestBiStreamingReporting() {
require.NoError(s.T(), ss.CloseSend())
wg.Wait()

require.EqualValues(s.T(), count, 100, "Number of received msg on the wire must match")
require.EqualValues(s.T(), 100, count, "Number of received msg on the wire must match")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Put actual and expected values where they should be.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Wow, thanks for those detailed comments (: Next time, no need for this it's very clear! 💪🏽

@@ -11,3 +11,5 @@ require (
google.golang.org/grpc v1.35.0
google.golang.org/protobuf v1.25.0
)

replace github.com/grpc-ecosystem/go-grpc-middleware/v2 => ../..
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure how to test it otherwise if changes are made to the outer module and to providers.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it'll have to be merged in two steps.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If this PR is approved I can split it into two - one updating the base and another one updating the provider. I also have a few more things I'd like to cleanup in follow ups.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sounds good to me, will give @bwplotka a few days to have a look but I'm otherwise happy to merge this on our own.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yup, this is the problem with multi-modules.

One thing we could add is to add some tooling that e.g makes commit with this PR on your fork, then creates commit on top of it, so it's at least not broken on master, if you just pull openmetrics module. Local path approach can break users on main. It's fine for now (dev part of v2) but we might want to improve this process. LGTM for now

}

func (rep *reportable) ServerReporter(ctx context.Context, _ interface{}, typ interceptors.GRPCType, service string, method string) (interceptors.Reporter, context.Context) {
m := NewServerMetrics(rep.registry)
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 super weird. So we're creating a new metrics holder here, we do not register it anywhere and just use it as is. This is just broken - there is no way these metrics are going to be scraped.

@@ -1,35 +1,21 @@
package metrics
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Server metrics and interceptor are refactored in the same way as client metrics and interceptor. All comments apply, not repeating them.

resp := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/", nil)
require.NoError(t, err, "failed creating request for Prometheus handler")

promhttp.Handler().ServeHTTP(resp, req)
promhttp.HandlerFor(reg, promhttp.HandlerOpts{}).ServeHTTP(resp, req)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use the explicitly passed registry here rather than a global one. Test should be self-contained and hermetic, it shouldn't depend on globals.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree, thanks for this

@@ -292,6 +293,7 @@ func toFloat64HistCount(h prometheus.Observer) uint64 {
}

func requireValue(t *testing.T, expect int, c prometheus.Collector) {
t.Helper()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

That way error traces when a test fails lead to the line where this function was invoked, not into this function. Convenience thing, helped me to navigate failing tests.

Copy link
Collaborator

@johanbrandhorst johanbrandhorst left a comment

Choose a reason for hiding this comment

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

This looks generally good to me, thanks for taking the time. I'll hold off on approving as this is @bwplotka's area of expertise.

interceptors/client.go Show resolved Hide resolved
requireValueHistCount(s.T(), 1, DefaultClientMetrics.clientHandledHistogram.WithLabelValues("unary", "mwitkow.testproto.TestService", "PingEmpty"))
requireValue(s.T(), 1, s.clientMetrics.clientStartedCounter.WithLabelValues("unary", "providers.openmetrics.testproto.v1.TestService", "PingEmpty"))
requireValue(s.T(), 1, s.clientMetrics.clientHandledCounter.WithLabelValues("unary", "providers.openmetrics.testproto.v1.TestService", "PingEmpty", "OK"))
requireValueHistCount(s.T(), 1, s.clientMetrics.clientHandledHistogram.WithLabelValues("unary", "providers.openmetrics.testproto.v1.TestService", "PingEmpty"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice find

@@ -11,3 +11,5 @@ require (
google.golang.org/grpc v1.35.0
google.golang.org/protobuf v1.25.0
)

replace github.com/grpc-ecosystem/go-grpc-middleware/v2 => ../..
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it'll have to be merged in two steps.

Copy link
Collaborator

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Thanks for this, LGTM

I agree that the openmetrics PR might have been prematurely merged.. Yet it was kind of stated to merge a bigger chunk of work for later to iterate... yet iteration never happened and the code on main was unusable. It's totally on me, sorry for not paying close attention during the review process. Cc @yashrsharma44 for learning too on this (:

image

Let's make sure to keep the quality bar much higher next time.

@@ -50,7 +50,7 @@ func (m *mockReportable) Equal(t *testing.T, expected []*mockReport) {
require.NoError(t, err)
continue
}
require.Equal(t, expected[i].postCalls[k].Error(), err.Error(), "%v %v", i, k)
require.EqualError(t, err, expected[i].postCalls[k].Error(), "%v %v", i, k)
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍🏽 Thanks a lot

// UnaryClientInterceptor is a gRPC client-side interceptor that provides Prometheus monitoring for Unary RPCs.
func UnaryClientInterceptor(clientRegister openmetrics.Registerer) grpc.UnaryClientInterceptor {
func UnaryClientInterceptor(clientMetrics *ClientMetrics) grpc.UnaryClientInterceptor {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Agree, cc @yashrsharma44 for learning experience (:

@@ -340,12 +342,12 @@ func (s *ClientInterceptorTestSuite) TestBiStreamingReporting() {
require.NoError(s.T(), ss.CloseSend())
wg.Wait()

require.EqualValues(s.T(), count, 100, "Number of received msg on the wire must match")
require.EqualValues(s.T(), 100, count, "Number of received msg on the wire must match")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wow, thanks for those detailed comments (: Next time, no need for this it's very clear! 💪🏽

@@ -11,3 +11,5 @@ require (
google.golang.org/grpc v1.35.0
google.golang.org/protobuf v1.25.0
)

replace github.com/grpc-ecosystem/go-grpc-middleware/v2 => ../..
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yup, this is the problem with multi-modules.

One thing we could add is to add some tooling that e.g makes commit with this PR on your fork, then creates commit on top of it, so it's at least not broken on master, if you just pull openmetrics module. Local path approach can break users on main. It's fine for now (dev part of v2) but we might want to improve this process. LGTM for now

resp := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/", nil)
require.NoError(t, err, "failed creating request for Prometheus handler")

promhttp.Handler().ServeHTTP(resp, req)
promhttp.HandlerFor(reg, promhttp.HandlerOpts{}).ServeHTTP(resp, req)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree, thanks for this

@bwplotka bwplotka merged commit f60016a into grpc-ecosystem:v2 Apr 23, 2021
@ash2k ash2k deleted the refactor-metrics branch April 23, 2021 22:59
@yashrsharma44
Copy link
Collaborator

Thanks, @ash2k for such great work!

I was the original author for introducing the changes for openmetrics and I agree that the changes were not suitable to be merged. Thanks for such a comprehensive review and this PR might be a good opportunity for me to write good Go code 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants