proxy-mirror

Description

The proxy-mirror Plugin can be used to mirror client requests. Traffic mirroring duplicates the real online traffic to the mirroring service. This enables specific analysis of the online traffic or request content without interrupting the online service.

proxy-mirror - 图1note

The response returned by the mirror request is ignored.

Attributes

NameTypeRequiredDefaultValid valuesDescription
hoststringTrueAddress of the mirror service. It needs to contain the scheme (http(s) or grpc(s)) but without the path. For example, http://127.0.0.1:9797.
pathstringFalsePath of the mirror request. If unspecified, current path will be used. If it is for mirroring grpc traffic, this option is no longer applicable.
path_concat_modestringFalsereplace[“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. If it is for mirroring grpc traffic, this option is no longer applicable too.
sample_rationumberFalse1[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.

conf/config.yaml

  1. plugin_attr:
  2. proxy-mirror:
  3. timeout:
  4. connect: 2000ms
  5. read: 2000ms
  6. send: 2000ms
NameTypeDefaultDescription
connectstring60sConnect timeout to the mirrored Upstream.
readstring60sRead timeout to the mirrored Upstream.
sendstring60sSend timeout to the mirrored Upstream.

Enable Plugin

You can enable the Plugin on a specific Route as shown below:

proxy-mirror - 图2note

You can fetch the admin_key from config.yaml and save to an environment variable with the following command:

  1. admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H "X-API-KEY: $admin_key" -X PUT -d '
  3. {
  4. "plugins": {
  5. "proxy-mirror": {
  6. "host": "http://127.0.0.1:9797"
  7. }
  8. },
  9. "upstream": {
  10. "nodes": {
  11. "127.0.0.1:1999": 1
  12. },
  13. "type": "roundrobin"
  14. },
  15. "uri": "/hello"
  16. }'

Specify the timeout for mirror subrequests

We can specify the timeout for subrequests in plugin_attr in conf/config.yaml. This is useful in connection reuse scenarios when mirroring traffic to a very slow backend service.

Since mirror requests are implemented as sub-requests, delays in sub-requests will block the original request until the sub-requests are completed. So you can configure the timeout time to protect the sub-requests from excessive delays that affect the original requests.

NameTypeDefaultDescription
connectstring60sConnection timeout for mirror request to upstream.
readstring60sThe time that APISIX maintains the connection with the mirror server; if APISIX does not receive a response from the mirror server within this time, the connection is closed.
sendstring60sThe time that APISIX maintains the connection with the mirror server; if APISIX does not send a request within this time, the connection is closed.
  1. plugin_attr:
  2. proxy-mirror:
  3. timeout:
  4. connect: 2000ms
  5. read: 2000ms
  6. send: 2000ms

Example usage

proxy-mirror - 图3tip

For testing you can create a test server by running:

  1. python -m http.server 9797

Once you have configured the Plugin as shown above, the requests made will be mirrored to the configured host.

  1. curl http://127.0.0.1:9080/hello -i
  1. HTTP/1.1 200 OK
  2. ...
  3. hello world

Delete Plugin

To remove the proxy-mirror Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H "X-API-KEY: $admin_key" -X PUT -d '
  3. {
  4. "uri": "/hello",
  5. "plugins": {},
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:1999": 1
  10. }
  11. }
  12. }'