fault-injection
Name#
Fault injection plugin, this plugin can be used with other plugins and will be executed before other plugins. The abort attribute will directly return the user-specified http code to the client and terminate the subsequent plugins. The delay attribute will delay a request and execute subsequent plugins.
Attributes#
| Name | Type | Requirement | Default | Valid | Description |
|---|---|---|---|---|---|
| abort.http_status | integer | required | [200, ...] | user-specified http code returned to the client. | |
| abort.body | string | optional | response data returned to the client. Nginx variable can be used inside, like client addr: $remote_addr\n | ||
| abort.percentage | integer | optional | [0, 100] | percentage of requests to be aborted. | |
| abort.vars | array[] | optional | The rules for executing fault injection will only be executed when the rules are matched. vars is a list of expressions, which is from the lua-resty-expr. | ||
| delay.duration | number | required | delay time (can be decimal). | ||
| delay.percentage | integer | optional | [0, 100] | percentage of requests to be delayed. | |
| delay.vars | array[] | optional | Execute the request delay rule, and the request will be delayed only after the rule matches. vars is a list of expressions, which is from the lua-resty-expr. |
Note: One of abort and delay must be specified.
The vars is a list of expression which is from the lua-resty-expr, which can flexibly implement the and/or relationship between rules. Example:
This means that the relationship between the first two expressions is and, and the relationship between the first two expressions and the third expression is or.
How To Enable#
Enable the plugin#
1: enable the fault-injection plugin for a specific route and specify the abort attribute:
Test plugin:
http status is 200 and the response body is "Fault Injection! " indicate that the plugin is enabled.
2: Enable the fault-injection plugin for a specific route and specify the delay attribute:
Test plugin:
Example 3: Enable the fault-injection plugin for a specific route and specify the vars rule of the abort parameter.
Test plugin:
- The vars rule fails to match, and the request returns upstream response data:
- The vars rule is successfully matched and fault injection is performed:
Example 4: Enable the fault-injection plugin for a specific route and specify the vars rule for the delay parameter.
Test plugin:
- The vars rule fails to match and the request is not delayed:
- The vars rule is successfully matched, and the request is delayed for two seconds:
Example 5: Enable the fault-injection plugin for a specific route, and specify the vars rules for the abort and delay parameters.
Test plugin:
- The vars rules of abort and delay fail to match:
- The abort vars rule fails to match, no fault injection is performed, but the request is delayed:
- The vars rule of delay fails to match, the request is not delayed, but fault injection is performed:
- The vars rules of abort and delay parameters match successfully, perform fault injection, and delay the request:
Example 6: Enable the fault-injection plugin for a specific route, and specify the vars rule of the abort parameter (the relationship of or).
Indicates that when the request parameters name and age satisfy both name == "jack" and age >= 18, fault injection is performed. Or when the request header apikey satisfies apikey == "apisix-key", fault injection is performed.
Test plugin:
- The request parameter name and age match successfully, and the request header
apikeyis missing, and fault injection is performed:
- The request header
apikeyis successfully matched, and the request parameters are missing, and fault injection is performed:
- Both request parameters and request headers fail to match, and fault injection is not performed:
Disable Plugin#
Remove the corresponding JSON in the plugin configuration to disable the plugin immediately without restarting the service:
The plugin has been disabled now.