Skip to main content
Version: Next

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#

NameTypeRequirementDefaultValidDescription
abort.http_statusintegerrequired[200, ...]user-specified http code returned to the client.
abort.bodystringoptionalresponse data returned to the client. Nginx variable can be used inside, like client addr: $remote_addr\n
abort.percentageintegeroptional[0, 100]percentage of requests to be aborted.
abort.varsarray[]optionalThe 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.durationnumberrequireddelay time (can be decimal).
delay.percentageintegeroptional[0, 100]percentage of requests to be delayed.
delay.varsarray[]optionalExecute 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:

[    [        [ "arg_name","==","jack" ],        [ "arg_age","==",18 ]    ],    [        [ "arg_name2","==","allen" ]    ]]

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:

curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "plugins": {       "fault-injection": {           "abort": {              "http_status": 200,              "body": "Fault Injection!"           }       }    },    "upstream": {       "nodes": {           "127.0.0.1:1980": 1       },       "type": "roundrobin"    },    "uri": "/hello"}'

Test plugin:

$ curl http://127.0.0.1:9080/hello -iHTTP/1.1 200 OKDate: Mon, 13 Jan 2020 13:50:04 GMTContent-Type: text/plainTransfer-Encoding: chunkedConnection: keep-aliveServer: APISIX web server
Fault Injection!

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:

curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "plugins": {       "fault-injection": {           "delay": {              "duration": 3           }       }    },    "upstream": {       "nodes": {           "127.0.0.1:1980": 1       },       "type": "roundrobin"    },    "uri": "/hello"}'

Test plugin:

$ time curl http://127.0.0.1:9080/hello -iHTTP/1.1 200 OKContent-Type: application/octet-streamContent-Length: 6Connection: keep-aliveServer: APISIX web serverDate: Tue, 14 Jan 2020 14:30:54 GMTLast-Modified: Sat, 11 Jan 2020 12:46:21 GMT
hello
real    0m3.034suser    0m0.007ssys     0m0.010s

Example 3: Enable the fault-injection plugin for a specific route and specify the vars rule of the abort parameter.

curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "plugins": {        "fault-injection": {            "abort": {                    "http_status": 403,                    "body": "Fault Injection!\n",                    "vars": [                        [                            [ "arg_name","==","jack" ]                        ]                    ]            }        }    },    "upstream": {        "nodes": {            "127.0.0.1:1980": 1        },        "type": "roundrobin"    },    "uri": "/hello"}'

Test plugin:

  1. The vars rule fails to match, and the request returns upstream response data:
$ curl "http://127.0.0.1:9080/hello?name=allen" -iHTTP/1.1 200 OKContent-Type: application/octet-streamTransfer-Encoding: chunkedConnection: keep-aliveDate: Wed, 20 Jan 2021 07:21:57 GMTServer: APISIX/2.2
hello
  1. The vars rule is successfully matched and fault injection is performed:
$ curl "http://127.0.0.1:9080/hello?name=jack" -iHTTP/1.1 403 ForbiddenDate: Wed, 20 Jan 2021 07:23:37 GMTContent-Type: text/plain; charset=utf-8Transfer-Encoding: chunkedConnection: keep-aliveServer: APISIX/2.2
Fault Injection!

Example 4: Enable the fault-injection plugin for a specific route and specify the vars rule for the delay parameter.

curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "plugins": {        "fault-injection": {            "delay": {                "duration": 2,                "vars": [                    [                        [ "arg_name","==","jack" ]                    ]                ]            }        }    },    "upstream": {        "nodes": {            "127.0.0.1:1980": 1        },        "type": "roundrobin"    },    "uri": "/hello"}'

Test plugin:

  1. The vars rule fails to match and the request is not delayed:
$ time curl "http://127.0.0.1:9080/hello?name=allen" -iHTTP/1.1 200 OKContent-Type: application/octet-streamTransfer-Encoding: chunkedConnection: keep-aliveDate: Wed, 20 Jan 2021 07:26:17 GMTServer: APISIX/2.2
hello
real    0m0.007suser    0m0.003ssys     0m0.003s
  1. The vars rule is successfully matched, and the request is delayed for two seconds:
$ time curl "http://127.0.0.1:9080/hello?name=jack" -iHTTP/1.1 200 OKContent-Type: application/octet-streamTransfer-Encoding: chunkedConnection: keep-aliveDate: Wed, 20 Jan 2021 07:57:50 GMTServer: APISIX/2.2
hello
real    0m2.009suser    0m0.004ssys     0m0.004s

Example 5: Enable the fault-injection plugin for a specific route, and specify the vars rules for the abort and delay parameters.

curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "plugins": {        "fault-injection": {            "abort": {                "http_status": 403,                "body": "Fault Injection!\n",                "vars": [                    [                        [ "arg_name","==","jack" ]                    ]                ]            },            "delay": {                "duration": 2,                "vars": [                    [                        [ "http_age","==","18" ]                    ]                ]            }        }    },    "upstream": {        "nodes": {            "127.0.0.1:1980": 1        },        "type": "roundrobin"    },    "uri": "/hello"}'

Test plugin:

  1. The vars rules of abort and delay fail to match:
$ time curl "http://127.0.0.1:9080/hello?name=allen" -H 'age: 20' -iHTTP/1.1 200 OKContent-Type: application/octet-streamTransfer-Encoding: chunkedConnection: keep-aliveDate: Wed, 20 Jan 2021 08:01:43 GMTServer: APISIX/2.2
hello
real    0m0.007suser    0m0.003ssys     0m0.003s
  1. The abort vars rule fails to match, no fault injection is performed, but the request is delayed:
$ time curl "http://127.0.0.1:9080/hello?name=allen" -H 'age: 18' -iHTTP/1.1 200 OKContent-Type: application/octet-streamTransfer-Encoding: chunkedConnection: keep-aliveDate: Wed, 20 Jan 2021 08:19:03 GMTServer: APISIX/2.2
hello
real    0m2.009suser    0m0.001ssys     0m0.006s
  1. The vars rule of delay fails to match, the request is not delayed, but fault injection is performed:
$ time curl "http://127.0.0.1:9080/hello?name=jack" -H 'age: 20' -iHTTP/1.1 403 ForbiddenDate: Wed, 20 Jan 2021 08:20:18 GMTContent-Type: text/plain; charset=utf-8Transfer-Encoding: chunkedConnection: keep-aliveServer: APISIX/2.2
Fault Injection!
real    0m0.007suser    0m0.002ssys     0m0.004s
  1. The vars rules of abort and delay parameters match successfully, perform fault injection, and delay the request:
$ time curl "http://127.0.0.1:9080/hello?name=jack" -H 'age: 18' -iHTTP/1.1 403 ForbiddenDate: Wed, 20 Jan 2021 08:21:17 GMTContent-Type: text/plain; charset=utf-8Transfer-Encoding: chunkedConnection: keep-aliveServer: APISIX/2.2
Fault Injection!
real    0m2.006suser    0m0.001ssys     0m0.005s

Example 6: Enable the fault-injection plugin for a specific route, and specify the vars rule of the abort parameter (the relationship of or).

curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "plugins": {        "fault-injection": {            "abort": {                "http_status": 403,                "body": "Fault Injection!\n",                "vars": [                    [                        ["arg_name","==","jack"],                        ["arg_age","!","<",18]                    ],                    [                        ["http_apikey","==","apisix-key"]                    ]                ]            }        }    },    "upstream": {        "nodes": {            "127.0.0.1:1980": 1        },        "type": "roundrobin"    },    "uri": "/hello"}'

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:

  1. The request parameter name and age match successfully, and the request header apikey is missing, and fault injection is performed:
$ curl "http://127.0.0.1:9080/hello?name=jack&age=19" -iHTTP/1.1 403 ForbiddenDate: Fri, 22 Jan 2021 11:05:46 GMTContent-Type: text/plain; charset=utf-8Transfer-Encoding: chunkedConnection: keep-aliveServer: APISIX/2.2
Fault Injection!
  1. The request header apikey is successfully matched, and the request parameters are missing, and fault injection is performed:
$ curl http://127.0.0.1:9080/hello -H "apikey: apisix-key" -iHTTP/1.1 403 ForbiddenDate: Fri, 22 Jan 2021 11:08:34 GMTContent-Type: text/plain; charset=utf-8Transfer-Encoding: chunkedConnection: keep-aliveServer: APISIX/2.2
Fault Injection!
  1. Both request parameters and request headers fail to match, and fault injection is not performed:
$ curl http://127.0.0.1:9080/hello -iHTTP/1.1 200 OKContent-Type: application/octet-streamTransfer-Encoding: chunkedConnection: keep-aliveDate: Fri, 22 Jan 2021 11:11:17 GMTServer: APISIX/2.2
hello

Disable Plugin#

Remove the corresponding JSON in the plugin configuration to disable the plugin immediately without restarting the service:

curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "uri": "/hello",    "plugins": {},    "upstream": {        "type": "roundrobin",        "nodes": {            "127.0.0.1:1980": 1        }    }}'

The plugin has been disabled now.