Support gRPC and gRPC-Web protocol in metrics

This commit is contained in:
Ngọc Long 2022-11-08 16:52:09 +07:00 committed by GitHub
parent d131ef57da
commit 240fb871b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 142 additions and 2 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/go-kit/kit/metrics"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
)
// CollectingCounter is a metrics.Counter implementation that enables access to the CounterValue and LastLabelValues.
@ -129,3 +130,102 @@ func Test_getMethod(t *testing.T) {
})
}
}
func Test_getRequestProtocol(t *testing.T) {
testCases := []struct {
desc string
headers http.Header
expected string
}{
{
desc: "default",
expected: protoHTTP,
},
{
desc: "websocket",
headers: http.Header{
"Connection": []string{"upgrade"},
"Upgrade": []string{"websocket"},
},
expected: protoWebsocket,
},
{
desc: "SSE",
headers: http.Header{
"Accept": []string{"text/event-stream"},
},
expected: protoSSE,
},
{
desc: "grpc web",
headers: http.Header{
"Content-Type": []string{"application/grpc-web-text"},
},
expected: protoGRPCWeb,
},
{
desc: "grpc",
headers: http.Header{
"Content-Type": []string{"application/grpc-text"},
},
expected: protoGRPC,
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
req := httptest.NewRequest(http.MethodGet, "https://localhost", http.NoBody)
req.Header = test.headers
protocol := getRequestProtocol(req)
assert.Equal(t, test.expected, protocol)
})
}
}
func Test_grpcStatusCode(t *testing.T) {
testCases := []struct {
desc string
status string
expected codes.Code
}{
{
desc: "invalid number",
status: "foo",
expected: codes.Unknown,
},
{
desc: "number",
status: "1",
expected: codes.Canceled,
},
{
desc: "invalid string",
status: `"foo"`,
expected: codes.Unknown,
},
{
desc: "string",
status: `"OK"`,
expected: codes.OK,
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
rw := httptest.NewRecorder()
rw.Header().Set("Grpc-Status", test.status)
code := grpcStatusCode(rw)
assert.EqualValues(t, test.expected, code)
})
}
}