-
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
Merged
spacewander
merged 24 commits into
apache:master
from
monkeyDluffy6017:covert-grpc-status-details-bin-in-header-to-http-response-body
Nov 21, 2022
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
5caf726
feat: convert grpc-status-details-bin in header to HTTP response body…
monkeyDluffy6017 cd98b50
change: minor tweaks.
levy001 0b2999a
test: change the proto uri
levy001 ebaf9a1
Merge branch 'master' into covert-grpc-status-details-bin-in-header-t…
levy001 2627139
doc: fix doc lint error.
monkeyDluffy6017 cf81984
change: minor tweaks.
monkeyDluffy6017 3680e65
Merge branch 'master' into covert-grpc-status-details-bin-in-header-t…
monkeyDluffy6017 eab7556
change: minor tweaks.
monkeyDluffy6017 14bb8fe
change: minor tweaks
monkeyDluffy6017 082748a
bugfix: recovery pb state after create a new one
levy001 993f52f
test: remove redundant --- LAST
levy001 43ba525
change: for test
levy001 13d4556
change: for test
levy001 f1f4fa2
change: use grpc_status.proto which is lua string
monkeyDluffy6017 ebb6ec2
Merge branch 'master' into covert-grpc-status-details-bin-in-header-t…
levy001 c631ef0
change: for test
levy001 3721e7d
change: use permalink
monkeyDluffy6017 da6ffef
Merge branch 'covert-grpc-status-details-bin-in-header-to-http-respon…
monkeyDluffy6017 2787472
change: resolve code lint error
monkeyDluffy6017 48871b7
change: resolve review comment
monkeyDluffy6017 ac34232
change: optimize error log format
monkeyDluffy6017 b7fd88b
fix: recovery old pb state after return error
monkeyDluffy6017 cdfd619
Merge branch 'master' into covert-grpc-status-details-bin-in-header-t…
monkeyDluffy6017 61b185e
Merge branch 'master' into covert-grpc-status-details-bin-in-header-t…
monkeyDluffy6017 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,18 +14,94 @@ | |
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
local util = require("apisix.plugins.grpc-transcode.util") | ||
local util = require("apisix.plugins.grpc-transcode.util") | ||
local grpc_proto = require("apisix.plugins.grpc-transcode.proto") | ||
local core = require("apisix.core") | ||
local pb = require("pb") | ||
local ngx = ngx | ||
local string = string | ||
local ngx_decode_base64 = ngx.decode_base64 | ||
local ipairs = ipairs | ||
local pcall = pcall | ||
|
||
return function(ctx, proto, service, method, pb_option) | ||
|
||
local function handle_error_response(status_detail_type) | ||
local err_msg | ||
|
||
local grpc_status = ngx.header["grpc-status-details-bin"] | ||
if grpc_status then | ||
grpc_status = ngx_decode_base64(grpc_status) | ||
if grpc_status == nil then | ||
err_msg = "grpc-status-details-bin is not base64 format" | ||
ngx.arg[1] = err_msg | ||
return err_msg | ||
end | ||
|
||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
pb.state(old_pb_state) | ||
if not ok then | ||
err_msg = "failed to call pb.decode to decode grpc-status-details-bin" | ||
ngx.arg[1] = err_msg | ||
return err_msg .. ", err: " .. decoded_grpc_status | ||
end | ||
|
||
if not decoded_grpc_status then | ||
err_msg = "failed to decode grpc-status-details-bin" | ||
ngx.arg[1] = err_msg | ||
return err_msg | ||
end | ||
|
||
local details = decoded_grpc_status.details | ||
if status_detail_type and details then | ||
local decoded_details = {} | ||
for _, detail in ipairs(details) do | ||
local ok, err_or_value = pcall(pb.decode, status_detail_type, detail.value) | ||
if not ok then | ||
err_msg = "failed to call pb.decode to decode details in " | ||
.. "grpc-status-details-bin" | ||
ngx.arg[1] = err_msg | ||
return err_msg .. ", err: " .. err_or_value | ||
end | ||
|
||
if not err_or_value then | ||
err_msg = "failed to decode details in grpc-status-details-bin" | ||
ngx.arg[1] = err_msg | ||
return err_msg | ||
end | ||
|
||
core.table.insert(decoded_details, err_or_value) | ||
end | ||
|
||
decoded_grpc_status.details = decoded_details | ||
end | ||
|
||
local resp_body = {error = decoded_grpc_status} | ||
local response, err = core.json.encode(resp_body) | ||
if not response then | ||
err_msg = "failed to json_encode response body" | ||
ngx.arg[1] = err_msg | ||
return err_msg .. ", error: " .. err | ||
end | ||
|
||
ngx.arg[1] = response | ||
end | ||
end | ||
|
||
|
||
return function(ctx, proto, service, method, pb_option, show_status_in_body, status_detail_type) | ||
local buffer = core.response.hold_body_chunk(ctx) | ||
if not buffer then | ||
return nil | ||
end | ||
|
||
-- handle error response after the last response chunk | ||
if ngx.status >= 300 and show_status_in_body then | ||
return handle_error_response(status_detail_type) | ||
end | ||
|
||
-- when body has already been read by other plugin | ||
-- the buffer is an empty string | ||
if buffer == "" and ctx.resp_body then | ||
|
@@ -44,17 +120,19 @@ return function(ctx, proto, service, method, pb_option) | |
|
||
util.set_options(proto, pb_option) | ||
|
||
local err_msg | ||
local decoded = pb.decode(m.output_type, buffer) | ||
if not decoded then | ||
ngx.arg[1] = "failed to decode response data by protobuf" | ||
return "failed to decode response data by protobuf" | ||
err_msg = "failed to decode response data by protobuf" | ||
ngx.arg[1] = err_msg | ||
return err_msg | ||
end | ||
|
||
local response, err = core.json.encode(decoded) | ||
if not response then | ||
core.log.error("failed to call json_encode data: ", err) | ||
response = "failed to json_encode response body" | ||
return response | ||
err_msg = "failed to json_encode response body" | ||
ngx.arg[1] = err_msg | ||
return err_msg .. ", err: " .. err | ||
end | ||
|
||
ngx.arg[1] = response | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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