Skip to content

Commit

Permalink
Dump error codes as strings instead of integers (#25)
Browse files Browse the repository at this point in the history
* Add RPC with error code to integration test

* Change error code to string

* Change unknown service name

* revert test changes

* Add changelog entry
  • Loading branch information
bradleyjkemp authored Jun 25, 2019
1 parent d4f7079 commit af89291
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Changelog

## v0.1.3 (unreleased)
## v0.2.0 (unreleased)
* Added proxy loop detection so that misconfiguration (e.g. missing/incorrent `--destination` flag) do not cause infinite loops and connection exhaustion.
* `grpc-proxy` now supports requests with gzip compression (however requests are still proxied uncompressed).
* `grpc-dump` now includes a timestamp along with each message sent/received.
* **Breaking Change**: `grpc-dump` now reports RPC error codes as their human-readable string form instead of a raw integer code.

## [v0.1.2](https://github.com/bradleyjkemp/grpc-tools/releases/tag/v0.1.2)
* Fixed bug where the `--destination` flag didn't work (issue [#13](https://github.com/bradleyjkemp/grpc-tools/issues/13)).
Expand Down
2 changes: 1 addition & 1 deletion grpc-dump/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Each message has the format:
}
],
"error" : { // present if the gRPC status is not OK
"code" : 2,
"code" : "Status code string",
"message" : "the gRPC error message"
},
"metadata" : { // the metadata present in the gRPC context
Expand Down
12 changes: 8 additions & 4 deletions grpc-dump/dump_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@ func dumpInterceptor(knownMethods map[string]*desc.MethodDescriptor) grpc.Stream
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
dss := &recordedServerStream{ServerStream: ss}
err := handler(srv, dss)
var rpcStatus *status.Status
var rpcStatus *internal.Status
if err != nil {
rpcStatus, _ = status.FromError(err)
grpcStatus, _ := status.FromError(err)
rpcStatus = &internal.Status{
Code: grpcStatus.Code().String(),
Message: grpcStatus.Message(),
}
}

fullMethod := strings.Split(info.FullMethod, "/")
md, _ := metadata.FromIncomingContext(ss.Context())
rpc := internal.RPC{
Metadata: md,
Service: fullMethod[1],
Method: fullMethod[2],
Messages: dss.events,
Status: rpcStatus.Proto(),
Status: rpcStatus,
Metadata: md,
}

knownMethod := knownMethods[info.FullMethod]
Expand Down
8 changes: 6 additions & 2 deletions internal/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package internal

import (
"fmt"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/metadata"
"time"
)
Expand All @@ -11,10 +10,15 @@ type RPC struct {
Service string `json:"service"`
Method string `json:"method"`
Messages []*StreamEvent `json:"messages"`
Status *spb.Status `json:"error,omitempty"`
Status *Status `json:"error,omitempty"`
Metadata metadata.MD `json:"metadata"`
}

type Status struct {
Code string `json:"code"`
Message string `json:"message"`
}

func (r RPC) StreamName() string {
return fmt.Sprintf("/%s/%s", r.Service, r.Method)
}
Expand Down

0 comments on commit af89291

Please sign in to comment.