-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
feat: convert grpc-status-details-bin in header to HTTP response body… #7639
feat: convert grpc-status-details-bin in header to HTTP response body… #7639
Conversation
apisix/plugins/grpc-transcode.lua
Outdated
type = "boolean", | ||
default = false | ||
}, | ||
-- https://github.com/googleapis/googleapis/blob/master/google/rpc/status.proto#L46 |
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.
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.
Do you mean link after api7?
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.
You should use GitHub's permalink feature, not a link to the master.
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.
Done
local status_pb_state = grpc_proto.fetch_status_pb_state() | ||
local old_pb_state = pb.state(status_pb_state) | ||
|
||
local decoded_grpc_status = pb.decode("grpc.status.ErrorStatus", grpc_status) |
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.
Should be wrapped with pcall
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.
Done
|
||
local function handle_error_response(status_detail_type) | ||
local headers = ngx.resp.get_headers() | ||
local grpc_status = headers["grpc-status-details-bin"] |
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.
Use ngx.header will be better?
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.
Done
@@ -44,6 +44,9 @@ APISIX takes in an HTTP request, transcodes it and forwards it to a gRPC service | |||
| method | string | True | | Method name of the gRPC service. | | |||
| deadline | number | False | 0 | Deadline for the gRPC service in ms. | | |||
| pb_option | array[string([pb_option_def](#options-for-pb_option))] | False | | protobuf options. | | |||
| show_status_in_body | boolean | False | False | Whether to display the parsed `grpc-status-details-bin` in the response body | | |||
| status_detail_type | string | False | | The message type corresponding to the [details](https://github.com/googleapis/googleapis/blob/master/google/rpc/status.proto#L46) part of `grpc-status-details-bin`, if not specified, this part will not be decoded | |
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.
Better to use permalink
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.
Done
@@ -44,6 +44,9 @@ APISIX takes in an HTTP request, transcodes it and forwards it to a gRPC service | |||
| method | string | True | | Method name of the gRPC service. | | |||
| deadline | number | False | 0 | Deadline for the gRPC service in ms. | | |||
| pb_option | array[string([pb_option_def](#options-for-pb_option))] | False | | protobuf options. | | |||
| show_status_in_body | boolean | False | False | Whether to display the parsed `grpc-status-details-bin` in the response body | |
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.
| show_status_in_body | boolean | False | False | Whether to display the parsed `grpc-status-details-bin` in the response body | | |
| show_status_in_body | boolean | False | false | Whether to display the parsed `grpc-status-details-bin` in the response body | |
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.
Done
t/grpc_server_example/main.go
Outdated
// GetErrResp implements helloworld.GreeterServer | ||
func (s *server) GetErrResp(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { | ||
st := status.New(codes.Unavailable, "Out of service") | ||
st, err := st.WithDetails(&pb.ErrorDetail{ |
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.
Please use gofmt to format the code
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.
Done
t/plugin/grpc-transcode3.t
Outdated
ngx.say(body) | ||
} | ||
} | ||
--- request |
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.
These sections are defined at the top.
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.
What do you mean? section 3 should be merge into section 1, or section 3 should be after section 1?
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.
--- request
is set in
apisix/t/plugin/grpc-transcode3.t
Line 27 in c4d5f2f
$block->set_value("request", "GET /t"); |
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.
Done
t/plugin/grpc-transcode3.t
Outdated
|
||
|
||
|
||
=== TEST 10: set routes (id: 1, show error details in body) |
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.
What is the difference between TEST 89 and TEST 1011?
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.
They are duplicated, deleted.
t/plugin/grpc-transcode3.t
Outdated
|
||
|
||
|
||
=== TEST 4: set routes (id: 1, get error response from rpc) |
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.
We can merge TEST 3&4 into one, like the TEST 1?
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.
Done
Don't forget to install the proto file: Line 267 in ccc43ea
|
a2e935d
to
0b2999a
Compare
Please make the CI pass, thanks! |
I'm working on it. |
…o-http-response-body
…o-http-response-body
…se-body' of github.com:monkeyDluffy6017/apisix into covert-grpc-status-details-bin-in-header-to-http-response-body
-- initialize protoc compiler | ||
protoc.reload() | ||
local status_protoc = protoc.new() | ||
-- do not use loadfile here, it can not load proto file when use relative address |
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.
-- do not use loadfile here, it can not load proto file when use relative address | |
-- do not use loadfile here, it can not load the proto file when using a relative address |
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.
Done
grpc_status = ngx_decode_base64(grpc_status) | ||
if grpc_status == nil then | ||
ngx.arg[1] = "grpc-status-details-bin is not base64 format" | ||
return "grpc-status-details-bin is not base64 format" |
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.
We can exact the error message to a variable so we don't need to repeat them?
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.
Done
local status_pb_state = grpc_proto.fetch_status_pb_state() | ||
local old_pb_state = pb.state(status_pb_state) | ||
|
||
local ok, decoded_grpc_status = pcall(pb.decode, "grpc_status.ErrorStatus", grpc_status) |
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.
We should restore the pb_state here so the error won't pollute the pb_state
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.
Done
@@ -248,6 +250,150 @@ Trailer: grpc-message | |||
{"workflowKey":"#2251799813685260","workflowInstanceKey":"#2251799813688013","bpmnProcessId":"order-process","version":1} | |||
``` | |||
|
|||
## Show `grpc-status-details-bin` in response body | |||
|
|||
If the gRPC service returns an error, there may be a `grpc-status-details-bin` field in the return header describing the error, which you can decode and display in the return body. |
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.
If the gRPC service returns an error, there may be a `grpc-status-details-bin` field in the return header describing the error, which you can decode and display in the return body. | |
If the gRPC service returns an error, there may be a `grpc-status-details-bin` field in the response header describing the error, which you can decode and display in the response body. |
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.
Done
t/plugin/grpc-transcode3.t
Outdated
} | ||
--- response_body | ||
passed | ||
--- no_error_log |
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.
The no_error_log is already set at the top of this file
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.
Done
-- do not use loadfile here, it can not load the proto file when using a relative address | ||
-- after luarocks install apisix | ||
local ok, err = status_protoc:load(grpc_status_proto, "grpc_status.proto") | ||
if not ok then |
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.
We should set the old_pb_state back when the error happened?
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.
Done
t/plugin/plugin.t
Outdated
for file_name in lfs.dir(ngx.config.prefix() .. "/../../apisix/plugins/") do | ||
if string.match(file_name, ".lua$") then | ||
local expected = file_name:sub(1, #file_name - 4) | ||
--local lfs = require("lfs") |
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.
For debug?
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.
Yes, forget to recovery, i have checked that there is no same problem
…o-http-response-body
This pull request has been marked as stale due to 60 days of inactivity. It will be closed in 4 weeks if no further activity occurs. If you think that's incorrect or this pull request should instead be reviewed, please simply write any comment. Even if closed, you can still revive the PR at any time or discuss it on the dev@apisix.apache.org list. Thank you for your contributions. |
…o-http-response-body
Description
Convert
grpc-status-details-bin
in response header to response body, so that users could avoid parsing header to read error response.Fixes #6996
Checklist