diff --git a/server/rpc/connecthelper/useragent.go b/server/rpc/connecthelper/useragent.go index 02b5e41ee..07116a414 100644 --- a/server/rpc/connecthelper/useragent.go +++ b/server/rpc/connecthelper/useragent.go @@ -27,10 +27,14 @@ import ( // metadata. func SDKTypeAndVersion(header http.Header) (string, string) { yorkieUserAgent := header.Get(types.UserAgentKey) - if len(yorkieUserAgent) == 0 { + if yorkieUserAgent == "" { return "", "" } - agent := strings.Split(yorkieUserAgent, "/") - return agent[0], agent[1] + split := strings.Split(yorkieUserAgent, "/") + if len(split) != 2 { + return "", "" + } + + return split[0], split[1] } diff --git a/server/rpc/interceptors/admin_auth.go b/server/rpc/interceptors/admin_auth.go index 4940b19ac..2855d308c 100644 --- a/server/rpc/interceptors/admin_auth.go +++ b/server/rpc/interceptors/admin_auth.go @@ -76,12 +76,14 @@ func (i *AdminAuthInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFu req.Spec().Procedure, ) - i.backend.Metrics.AddServerHandledCounter( - "unary", - strings.Split(req.Spec().Procedure, "/")[1], - strings.Split(req.Spec().Procedure, "/")[2], - connecthelper.ToRPCCodeString(err), - ) + if split := strings.Split(req.Spec().Procedure, "/"); len(split) == 3 { + i.backend.Metrics.AddServerHandledCounter( + "unary", + split[1], + split[2], + connecthelper.ToRPCCodeString(err), + ) + } return res, err } @@ -126,12 +128,14 @@ func (i *AdminAuthInterceptor) WrapStreamingHandler(next connect.StreamingHandle conn.Spec().Procedure, ) - i.backend.Metrics.AddServerHandledCounter( - "server_stream", - strings.Split(conn.Spec().Procedure, "/")[1], - strings.Split(conn.Spec().Procedure, "/")[2], - connecthelper.ToRPCCodeString(err), - ) + if split := strings.Split(conn.Spec().Procedure, "/"); len(split) == 3 { + i.backend.Metrics.AddServerHandledCounter( + "server_stream", + split[1], + split[2], + connecthelper.ToRPCCodeString(err), + ) + } return err } diff --git a/server/rpc/interceptors/context.go b/server/rpc/interceptors/context.go index 753112570..076b15a55 100644 --- a/server/rpc/interceptors/context.go +++ b/server/rpc/interceptors/context.go @@ -79,12 +79,14 @@ func (i *ContextInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc req.Spec().Procedure, ) - i.backend.Metrics.AddServerHandledCounter( - "unary", - strings.Split(req.Spec().Procedure, "/")[1], - strings.Split(req.Spec().Procedure, "/")[2], - connecthelper.ToRPCCodeString(err), - ) + if split := strings.Split(req.Spec().Procedure, "/"); len(split) == 3 { + i.backend.Metrics.AddServerHandledCounter( + "unary", + split[1], + split[2], + connecthelper.ToRPCCodeString(err), + ) + } return res, err } @@ -126,12 +128,14 @@ func (i *ContextInterceptor) WrapStreamingHandler(next connect.StreamingHandlerF conn.Spec().Procedure, ) - i.backend.Metrics.AddServerHandledCounter( - "server_stream", - strings.Split(conn.Spec().Procedure, "/")[1], - strings.Split(conn.Spec().Procedure, "/")[2], - connecthelper.ToRPCCodeString(err), - ) + if split := strings.Split(conn.Spec().Procedure, "/"); len(split) == 3 { + i.backend.Metrics.AddServerHandledCounter( + "server_stream", + split[1], + split[2], + connecthelper.ToRPCCodeString(err), + ) + } return err } @@ -155,8 +159,7 @@ func (i *ContextInterceptor) buildContext(ctx context.Context, header http.Heade md.APIKey = apiKey } - authorization := header.Get(types.AuthorizationKey) - if authorization != "" { + if authorization := header.Get(types.AuthorizationKey); authorization != "" { md.Authorization = authorization } ctx = metadata.With(ctx, md) diff --git a/test/integration/agent_test.go b/test/integration/server_test.go similarity index 85% rename from test/integration/agent_test.go rename to test/integration/server_test.go index 2f21c8df2..93c53eb99 100644 --- a/test/integration/agent_test.go +++ b/test/integration/server_test.go @@ -20,7 +20,6 @@ package integration import ( "context" - "io" "sync" "testing" @@ -56,7 +55,10 @@ func TestServer(t *testing.T) { assert.Fail(t, "unexpected ctx done") return case wr := <-wrch: - if wr.Err == io.EOF || connect.CodeOf(wr.Err) == connect.CodeCanceled { + // TODO(hackerwins): Define ClientError instead of using ConnectError later. + // For now, we use ConnectError to check whether the stream is closed. To + // simplify the interface, we will define ClientError later. + if connect.CodeOf(wr.Err) == connect.CodeCanceled { assert.Len(t, wr.Presences, 0) wg.Done() return