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.
There was a problem hiding this comment.
Do you mean link after api7?
There was a problem hiding this comment.
You should use GitHub's permalink feature, not a link to the master.
| 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.
Should be wrapped with pcall
|
|
||
| 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.
Use ngx.header will be better?
| | 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 | |
| | 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.
| | 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 | |
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.
Please use gofmt to format the code
t/plugin/grpc-transcode3.t
Outdated
| ngx.say(body) | ||
| } | ||
| } | ||
| --- request |
There was a problem hiding this comment.
These sections are defined at the top.
There was a problem hiding this comment.
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.
--- request is set in
apisix/t/plugin/grpc-transcode3.t
Line 27 in c4d5f2f
t/plugin/grpc-transcode3.t
Outdated
|
|
||
|
|
||
|
|
||
| === TEST 10: set routes (id: 1, show error details in body) |
There was a problem hiding this comment.
What is the difference between TEST 89 and TEST 1011?
There was a problem hiding this comment.
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.
We can merge TEST 3&4 into one, like the TEST 1?
|
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.
| -- 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 |
| 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.
We can exact the error message to a variable so we don't need to repeat them?
| 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.
We should restore the pb_state here so the error won't pollute the pb_state
|
|
||
| ## 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.
| 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. |
t/plugin/grpc-transcode3.t
Outdated
| } | ||
| --- response_body | ||
| passed | ||
| --- no_error_log |
There was a problem hiding this comment.
The no_error_log is already set at the top of this file
| -- 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.
We should set the old_pb_state back when the error happened?
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.
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-binin response header to response body, so that users could avoid parsing header to read error response.Fixes #6996
Checklist