Skip to content

Commit

Permalink
[vtadmin] log response errors (#12844)
Browse files Browse the repository at this point in the history
* log any error responses from the http api

Signed-off-by: Andrew Mason <andrew@planetscale.com>

* also install grpc interceptors to log the grpc api

Signed-off-by: Andrew Mason <andrew@planetscale.com>

---------

Signed-off-by: Andrew Mason <andrew@planetscale.com>
  • Loading branch information
Andrew Mason committed Apr 5, 2023
1 parent a919022 commit 8137d92
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
18 changes: 18 additions & 0 deletions go/vt/vtadmin/grpcserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package grpcserver

import (
"context"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -118,6 +119,23 @@ func New(name string, opts Options) *Server {
unaryInterceptors = append(unaryInterceptors, otgrpc.UnaryServerInterceptor(otgrpc.WithTracer(tracer)))
}

streamInterceptors = append(streamInterceptors, func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
err := handler(srv, ss)
if err != nil {
log.Errorf("%s error: %s", info.FullMethod, err)
}

return err
})
unaryInterceptors = append(unaryInterceptors, func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
resp, err = handler(ctx, req)
if err != nil {
log.Errorf("%s error: %s", info.FullMethod, err)
}

return resp, err
})

recoveryHandler := grpc_recovery.WithRecoveryHandler(func(p any) (err error) {
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "panic triggered: %v", p)
})
Expand Down
3 changes: 3 additions & 0 deletions go/vt/vtadmin/http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"net/http"

"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/vtadmin/errors"
)

Expand All @@ -44,6 +45,8 @@ type errorBody struct {
// 500 unknown.
func NewJSONResponse(value any, err error) *JSONResponse {
if err != nil {
log.Errorf(err.Error())

switch e := err.(type) {
case errors.TypedError:
return typedErrorJSONResponse(e)
Expand Down

0 comments on commit 8137d92

Please sign in to comment.