-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
gRPC server metrics #14922
Merged
Merged
gRPC server metrics #14922
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1461b1c
Move stats.go from grpc-internal to grpc-middleware
5caf1ab
Update grpc server metrics with server type label
3d0e4e8
Add stats test to grpc-external
7312a37
Update changelog
05f715b
Fix changelog
cc4de56
Remove global metrics instance for tests
7bd406b
Cleanup global metrics instance
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:feature | ||
grpc: Added metrics for external gRPC server. Added `server_type=internal|external` label to gRPC metrics. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package external | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"sort" | ||
"testing" | ||
|
||
"github.com/armon/go-metrics" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/sync/errgroup" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/hashicorp/go-hclog" | ||
|
||
"github.com/hashicorp/consul/agent/grpc-middleware/testutil" | ||
"github.com/hashicorp/consul/agent/grpc-middleware/testutil/testservice" | ||
"github.com/hashicorp/consul/proto/prototest" | ||
) | ||
|
||
func TestServer_EmitsStats(t *testing.T) { | ||
sink, metricsObj := testutil.NewFakeSink(t) | ||
|
||
srv := NewServer(hclog.Default(), metricsObj) | ||
|
||
testservice.RegisterSimpleServer(srv, &testservice.Simple{}) | ||
|
||
lis, err := net.Listen("tcp", "127.0.0.1:0") | ||
require.NoError(t, err) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
t.Cleanup(cancel) | ||
|
||
g, ctx := errgroup.WithContext(ctx) | ||
g.Go(func() error { | ||
return srv.Serve(lis) | ||
}) | ||
t.Cleanup(func() { | ||
srv.Stop() | ||
if err := g.Wait(); err != nil { | ||
t.Logf("grpc server error: %v", err) | ||
} | ||
}) | ||
|
||
conn, err := grpc.DialContext(ctx, lis.Addr().String(), grpc.WithInsecure()) | ||
require.NoError(t, err) | ||
t.Cleanup(func() { conn.Close() }) | ||
|
||
client := testservice.NewSimpleClient(conn) | ||
fClient, err := client.Flow(ctx, &testservice.Req{Datacenter: "mine"}) | ||
require.NoError(t, err) | ||
|
||
// Wait for the first event so that we know the stream is sending. | ||
_, err = fClient.Recv() | ||
require.NoError(t, err) | ||
|
||
cancel() | ||
conn.Close() | ||
srv.GracefulStop() | ||
// Wait for the server to stop so that active_streams is predictable. | ||
require.NoError(t, g.Wait()) | ||
|
||
// Occasionally the active_stream=0 metric may be emitted before the | ||
// active_conns=0 metric. The order of those metrics is not really important | ||
// so we sort the calls to match the expected. | ||
sort.Slice(sink.GaugeCalls, func(i, j int) bool { | ||
if i < 2 || j < 2 { | ||
return i < j | ||
} | ||
if len(sink.GaugeCalls[i].Key) < 4 || len(sink.GaugeCalls[j].Key) < 4 { | ||
return i < j | ||
} | ||
return sink.GaugeCalls[i].Key[3] < sink.GaugeCalls[j].Key[3] | ||
}) | ||
|
||
cmpMetricCalls := cmp.AllowUnexported(testutil.MetricCall{}) | ||
expLabels := []metrics.Label{{ | ||
Name: "server_type", | ||
Value: "external", | ||
}} | ||
expectedGauge := []testutil.MetricCall{ | ||
{Key: []string{"testing", "grpc", "server", "connections"}, Val: 1, Labels: expLabels}, | ||
{Key: []string{"testing", "grpc", "server", "streams"}, Val: 1, Labels: expLabels}, | ||
{Key: []string{"testing", "grpc", "server", "connections"}, Val: 0, Labels: expLabels}, | ||
{Key: []string{"testing", "grpc", "server", "streams"}, Val: 0, Labels: expLabels}, | ||
} | ||
prototest.AssertDeepEqual(t, expectedGauge, sink.GaugeCalls, cmpMetricCalls) | ||
|
||
expectedCounter := []testutil.MetricCall{ | ||
{Key: []string{"testing", "grpc", "server", "connection", "count"}, Val: 1, Labels: expLabels}, | ||
{Key: []string{"testing", "grpc", "server", "request", "count"}, Val: 1, Labels: expLabels}, | ||
{Key: []string{"testing", "grpc", "server", "stream", "count"}, Val: 1, Labels: expLabels}, | ||
} | ||
prototest.AssertDeepEqual(t, expectedCounter, sink.IncrCounterCalls, cmpMetricCalls) | ||
} | ||
|
||
func logError(t *testing.T, f func() error) func() { | ||
return func() { | ||
if err := f(); err != nil { | ||
t.Logf(err.Error()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This copies the
TestHandler_EmitStats
test. I did some deduplication of the helpers by moving things over togrpc-middleware/testutil
. (I could do more deduplication but kind of wanted a sanity check before I go too far.)