Skip to content
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(proxy-mirror): support prefix mode #8261

Merged
merged 7 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions apisix/plugins/proxy-mirror.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ local schema = {
type = "string",
pattern = [[^/[^?&]+$]],
},
path_concat_mode = {
type = "string",
default = "replace",
enum = {"replace", "prefix"},
description = "the concatenation mode for custom path"
},
sample_ratio = {
type = "number",
minimum = 0.00001,
Expand Down Expand Up @@ -83,8 +89,13 @@ end


local function enable_mirror(ctx, conf)
ctx.var.upstream_mirror_uri = resolver_host(conf.host) .. (conf.path or ctx.var.uri) ..
ctx.var.is_args .. (ctx.var.args or '')
if conf.path and conf.path_concat_mode == "prefix" then
ctx.var.upstream_mirror_uri = resolver_host(conf.host) .. conf.path .. ctx.var.uri ..
ctx.var.is_args .. (ctx.var.args or '')
else
ctx.var.upstream_mirror_uri = resolver_host(conf.host) .. (conf.path or ctx.var.uri) ..
ctx.var.is_args .. (ctx.var.args or '')
end

if has_mod then
apisix_ngx_client.enable_mirror()
Expand Down
1 change: 1 addition & 0 deletions docs/en/latest/plugins/proxy-mirror.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The response returned by the mirror request is ignored.
|--------------|--------|----------|---------|--------------|---------------------------------------------------------------------------------------------------------------------------|
| host | string | True | | | Address of the mirror service. It needs to contain the scheme but without the path. For example, `http://127.0.0.1:9797`. |
| path | string | False | | | Path of the mirror request. If unspecified, current path will be used. |
| path_concat_mode | string | False | replace | ["replace", "prefix"] | If the path of a mirror request is specified, set the concatenation mode of request paths. The `replace` mode will directly use `path` as the path of the mirror request. The `prefix` mode will use the `path` + `source request URI` as the path to the mirror request. |
| sample_ratio | number | False | 1 | [0.00001, 1] | Ratio of the requests that will be mirrored. |

You can customize the proxy timeouts for the mirrored sub-requests by configuring the `plugin_attr` key in your configuration file (`conf/config.yaml`). This can be used for mirroring traffic to a slow backend.
Expand Down
1 change: 1 addition & 0 deletions docs/zh/latest/plugins/proxy-mirror.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ description: 本文介绍了 Apache APISIX proxy-mirror 插件的相关操作,
| ---- | ------ | ------ | ------ | ------ | ------------------------------------------------------------------------------------------------------- |
| host | string | 是 | | | 指定镜像服务的地址,地址中需要包含 `schema`(`http` 或 `https`),但不能包含 `path` 部分。例如 `http://127.0.0.1:9797`。 |
| path | string | 否 | | | 指定镜像请求的路径。如果不指定,则默认会使用当前路径。 |
| path_concat_mode | string | 否 | replace | ["replace", "prefix"] | 当指定镜像请求的路径时,设置请求路径的拼接模式。`replace` 模式将会直接使用 `path` 作为镜像请求的路径。`prefix` 模式将会使用 `path` + `来源请求 URI` 作为镜像请求的路径。 |
| sample_ratio | number | 否 | 1 | [0.00001, 1] | 镜像请求的采样率。当设置为 `1` 时为全采样。 |

## 启用插件
Expand Down
56 changes: 56 additions & 0 deletions t/plugin/proxy-mirror.t
Original file line number Diff line number Diff line change
Expand Up @@ -786,3 +786,59 @@ GET /hello
hello world
--- error_log
dns resolver resolves domain: not-find-domian.notfind error:



=== TEST 27: custom path with prefix path_concat_mode
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-mirror": {
"host": "http://127.0.0.1:1986",
"path": "/a",
"path_concat_mode": "prefix"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed



=== TEST 28: hit route with prefix path_concat_mode
--- request
GET /hello
--- response_body
hello world
--- error_log
uri: /a/hello,



=== TEST 29: hit route with args and prefix path_concat_mode
--- request
GET /hello?a=1
--- response_body
hello world
--- error_log
uri: /a/hello?a=1