fault-injection

故障注入插件,该插件可以和其他插件一起使用,并且会在其他插件前被执行,配置 abort 参数将直接返回给客户端指定的响应码并且终止其他插件的执行,配置 delay 参数将延迟某个请求,并且还会执行配置的其他插件。

参数

名称类型必选项默认值有效值描述
abort.http_statusinteger必需[200, …]返回给客户端的 http 状态码
abort.bodystring可选返回给客户端的响应数据。支持使用 Nginx 变量,如 client addr: $remote_addr\n
abort.percentageinteger可选[0, 100]将被中断的请求占比
abort.varsarray[]可选执行故障注入的规则,当规则匹配通过后才会执行故障注。vars 是一个表达式的列表,来自 lua-resty-expr
delay.durationnumber必需延迟时间,可以指定小数
delay.percentageinteger可选[0, 100]将被延迟的请求占比
delay.varsarray[]可选执行请求延迟的规则,当规则匹配通过后才会延迟请求。vars 是一个表达式列表,来自 lua-resty-expr

注:参数 abort 和 delay 至少要存在一个。

vars 是由 lua-resty-expr 的表达式组成的列表,它可以灵活的实现规则之间的 and/or 关系,示例:

  1. [
  2. [
  3. [ "arg_name","==","jack" ],
  4. [ "arg_age","==",18 ]
  5. ],
  6. [
  7. [ "arg_name2","==","allen" ]
  8. ]
  9. ]

这表示前两个表达式之间的关系是 and ,而前两个和第三个表达式之间的关系是 or

示例

启用插件

示例1:为特定路由启用 fault-injection 插件,并指定 abort 参数:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "fault-injection": {
  5. "abort": {
  6. "http_status": 200,
  7. "body": "Fault Injection!"
  8. }
  9. }
  10. },
  11. "upstream": {
  12. "nodes": {
  13. "127.0.0.1:1980": 1
  14. },
  15. "type": "roundrobin"
  16. },
  17. "uri": "/hello"
  18. }'

测试:

  1. $ curl http://127.0.0.1:9080/hello -i
  2. HTTP/1.1 200 OK
  3. Date: Mon, 13 Jan 2020 13:50:04 GMT
  4. Content-Type: text/plain
  5. Transfer-Encoding: chunked
  6. Connection: keep-alive
  7. Server: APISIX web server
  8. Fault Injection!

http status 返回 200 并且响应 bodyFault Injection!,表示该插件已启用。

示例2:为特定路由启用 fault-injection 插件,并指定 delay 参数:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "fault-injection": {
  5. "delay": {
  6. "duration": 3
  7. }
  8. }
  9. },
  10. "upstream": {
  11. "nodes": {
  12. "127.0.0.1:1980": 1
  13. },
  14. "type": "roundrobin"
  15. },
  16. "uri": "/hello"
  17. }'

测试:

  1. $ time curl http://127.0.0.1:9080/hello -i
  2. HTTP/1.1 200 OK
  3. Content-Type: application/octet-stream
  4. Content-Length: 6
  5. Connection: keep-alive
  6. Server: APISIX web server
  7. Date: Tue, 14 Jan 2020 14:30:54 GMT
  8. Last-Modified: Sat, 11 Jan 2020 12:46:21 GMT
  9. hello
  10. real 0m3.034s
  11. user 0m0.007s
  12. sys 0m0.010s

示例3:为特定路由启用 fault-injection 插件,并指定 abort 参数的 vars 规则。

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "fault-injection": {
  5. "abort": {
  6. "http_status": 403,
  7. "body": "Fault Injection!\n",
  8. "vars": [
  9. [
  10. [ "arg_name","==","jack" ]
  11. ]
  12. ]
  13. }
  14. }
  15. },
  16. "upstream": {
  17. "nodes": {
  18. "127.0.0.1:1980": 1
  19. },
  20. "type": "roundrobin"
  21. },
  22. "uri": "/hello"
  23. }'

测试:

1、vars 规则匹配失败,请求返回上游响应数据:

  1. $ curl "http://127.0.0.1:9080/hello?name=allen" -i
  2. HTTP/1.1 200 OK
  3. Content-Type: application/octet-stream
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. Date: Wed, 20 Jan 2021 07:21:57 GMT
  7. Server: APISIX/2.2
  8. hello

2、vars 规则匹配成功,执行故障注入:

  1. $ curl "http://127.0.0.1:9080/hello?name=jack" -i
  2. HTTP/1.1 403 Forbidden
  3. Date: Wed, 20 Jan 2021 07:23:37 GMT
  4. Content-Type: text/plain; charset=utf-8
  5. Transfer-Encoding: chunked
  6. Connection: keep-alive
  7. Server: APISIX/2.2
  8. Fault Injection!

示例4:为特定路由启用 fault-injection 插件,并指定 delay 参数的 vars 规则。

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "fault-injection": {
  5. "delay": {
  6. "duration": 2,
  7. "vars": [
  8. [
  9. [ "arg_name","==","jack" ]
  10. ]
  11. ]
  12. }
  13. }
  14. },
  15. "upstream": {
  16. "nodes": {
  17. "127.0.0.1:1980": 1
  18. },
  19. "type": "roundrobin"
  20. },
  21. "uri": "/hello"
  22. }'

测试:

1、vars 规则匹配失败,不延迟请求:

  1. $ time "curl http://127.0.0.1:9080/hello?name=allen" -i
  2. HTTP/1.1 200 OK
  3. Content-Type: application/octet-stream
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. Date: Wed, 20 Jan 2021 07:26:17 GMT
  7. Server: APISIX/2.2
  8. hello
  9. real 0m0.007s
  10. user 0m0.003s
  11. sys 0m0.003s

2、vars 规则匹配成功,延迟请求两秒:

  1. $ time curl "http://127.0.0.1:9080/hello?name=jack" -i
  2. HTTP/1.1 200 OK
  3. Content-Type: application/octet-stream
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. Date: Wed, 20 Jan 2021 07:57:50 GMT
  7. Server: APISIX/2.2
  8. hello
  9. real 0m2.009s
  10. user 0m0.004s
  11. sys 0m0.004s

示例5:为特定路由启用 fault-injection 插件,并指定 abort 和 delay 参数的 vars 规则。

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "fault-injection": {
  5. "abort": {
  6. "http_status": 403,
  7. "body": "Fault Injection!\n",
  8. "vars": [
  9. [
  10. [ "arg_name","==","jack" ]
  11. ]
  12. ]
  13. },
  14. "delay": {
  15. "duration": 2,
  16. "vars": [
  17. [
  18. [ "http_age","==","18" ]
  19. ]
  20. ]
  21. }
  22. }
  23. },
  24. "upstream": {
  25. "nodes": {
  26. "127.0.0.1:1980": 1
  27. },
  28. "type": "roundrobin"
  29. },
  30. "uri": "/hello"
  31. }'

测试:

1、abort 和 delay 的 vars 规则匹配失败:

  1. $ time curl "http://127.0.0.1:9080/hello?name=allen" -H 'age: 20' -i
  2. HTTP/1.1 200 OK
  3. Content-Type: application/octet-stream
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. Date: Wed, 20 Jan 2021 08:01:43 GMT
  7. Server: APISIX/2.2
  8. hello
  9. real 0m0.007s
  10. user 0m0.003s
  11. sys 0m0.003s

2、abort 的 vars 规则匹配失败,不执行故障注入,但延迟请求:

  1. $ time curl "http://127.0.0.1:9080/hello?name=allen" -H 'age: 18' -i
  2. HTTP/1.1 200 OK
  3. Content-Type: application/octet-stream
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. Date: Wed, 20 Jan 2021 08:19:03 GMT
  7. Server: APISIX/2.2
  8. hello
  9. real 0m2.009s
  10. user 0m0.001s
  11. sys 0m0.006s

3、delay 的 vars 规则匹配失败,不延迟请求,但执行故障注入:

  1. $ time curl "http://127.0.0.1:9080/hello?name=jack" -H 'age: 20' -i
  2. HTTP/1.1 403 Forbidden
  3. Date: Wed, 20 Jan 2021 08:20:18 GMT
  4. Content-Type: text/plain; charset=utf-8
  5. Transfer-Encoding: chunked
  6. Connection: keep-alive
  7. Server: APISIX/2.2
  8. Fault Injection!
  9. real 0m0.007s
  10. user 0m0.002s
  11. sys 0m0.004s

4、abort 和 delay 参数的 vars 规则匹配成功,执行故障注入,并延迟请求:

  1. $ time curl "http://127.0.0.1:9080/hello?name=jack" -H 'age: 18' -i
  2. HTTP/1.1 403 Forbidden
  3. Date: Wed, 20 Jan 2021 08:21:17 GMT
  4. Content-Type: text/plain; charset=utf-8
  5. Transfer-Encoding: chunked
  6. Connection: keep-alive
  7. Server: APISIX/2.2
  8. Fault Injection!
  9. real 0m2.006s
  10. user 0m0.001s
  11. sys 0m0.005s

示例6:为特定路由启用 fault-injection 插件,并指定 abort 参数的 vars 规则(or 的关系)。

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "fault-injection": {
  5. "abort": {
  6. "http_status": 403,
  7. "body": "Fault Injection!\n",
  8. "vars": [
  9. [
  10. ["arg_name","==","jack"],
  11. ["arg_age","!","<",18]
  12. ],
  13. [
  14. ["http_apikey","==","apisix-key"]
  15. ]
  16. ]
  17. }
  18. }
  19. },
  20. "upstream": {
  21. "nodes": {
  22. "127.0.0.1:1980": 1
  23. },
  24. "type": "roundrobin"
  25. },
  26. "uri": "/hello"
  27. }'

表示当请求参数 name 和 age 同时满足 name == "jack"age >= 18 时,执行故障注入。或请求头 apikey 满足 apikey == "apisix-key" 时,执行故障注入。

测试:

1、请求参数 name 和 age 匹配成功,缺少请求头 apikey, 执行故障注入:

  1. $ curl "http://127.0.0.1:9080/hello?name=jack&age=19" -i
  2. HTTP/1.1 403 Forbidden
  3. Date: Fri, 22 Jan 2021 11:05:46 GMT
  4. Content-Type: text/plain; charset=utf-8
  5. Transfer-Encoding: chunked
  6. Connection: keep-alive
  7. Server: APISIX/2.2
  8. Fault Injection!

2、请求头 apikey 匹配成功,缺少请求参数,执行故障注入:

  1. $ curl http://127.0.0.1:9080/hello -H "apikey: apisix-key" -i
  2. HTTP/1.1 403 Forbidden
  3. Date: Fri, 22 Jan 2021 11:08:34 GMT
  4. Content-Type: text/plain; charset=utf-8
  5. Transfer-Encoding: chunked
  6. Connection: keep-alive
  7. Server: APISIX/2.2
  8. Fault Injection!

3、请求参数与请求头都匹配失败,不执行故障注入:

  1. $ curl http://127.0.0.1:9080/hello -i
  2. HTTP/1.1 200 OK
  3. Content-Type: application/octet-stream
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. Date: Fri, 22 Jan 2021 11:11:17 GMT
  7. Server: APISIX/2.2
  8. hello

禁用插件

移除插件配置中相应的 JSON 配置可立即禁用该插件,无需重启服务:

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

这时该插件已被禁用。