apisix icon indicating copy to clipboard operation
apisix copied to clipboard

How to call an external endpoint before the upstream proxy is called

Open vaibhavsw opened this issue 3 years ago • 4 comments

Description

I am trying to configure the serverless-pre-function and trying to run it before the proxy happens to the upstream so that the request body received in the HTTP Body could be sent to an external app, get transformed, and then get it forwarded to the upstream proxy, however, it doesn't; seem to be working, below is the config at the service level.

"serverless-pre-function": {
      "disable": false,
      "functions": [
        "return function(conf, ctx) ngx.location.capture(\"/health\"); end"
      ],
      "phase": "before_proxy"
    }

and below error is thrown again and again

2022/09/13 19:05:15 [error] 48#48: *147715 [lua] init.lua:760: http_balancer_phase(): invalid api_ctx while connecting to upstream, client: 172.18.0.1, server: _, request: "GET /dbt/actuator/health HTTP/1.1", subrequest: "/health", host: "127.0.0.1:9080"

Once the response is received from the subrequest, the response of it will be overridden for the upstream proxy as well.,\

Environment

  • APISIX version (run apisix version): 2.15
  • Operating system (run uname -a): MacOS
  • OpenResty / Nginx version (run openresty -V or nginx -V): nginx version: openresty/1.21.4.1
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info): 3.4.15
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version): 3.8.0

vaibhavsw avatar Sep 13 '22 19:09 vaibhavsw

ref: https://github.com/apache/apisix/issues/1826#issuecomment-656606944

tzssangglass avatar Sep 14 '22 01:09 tzssangglass

@tzssangglass Do we need to include the resty-http within the function or is it already available? Is there any example available for the implementation?

vaibhavsw avatar Sep 14 '22 02:09 vaibhavsw

I tried implementing

"serverless-pre-function": {
      "disable": false,
      "functions": [
        "return function(conf, ctx) httpc:request_uri(\"http://host.docker.internal:9191/health\"); end"
      ],
      "phase": "before_proxy"
    }

and I got the below error in the error console:

2022/09/14 02:52:47 [error] 45#45: *275474 lua entry thread aborted: runtime error: [string "return function(conf, ctx) httpc:request_uri(..."]:1: attempt to index global 'httpc' (a nil value)
stack traceback:
coroutine 0:
	[string "return function(conf, ctx) httpc:request_uri(..."]: in function 'func'
	/usr/local/apisix/apisix/plugins/serverless/init.lua:84: in function 'phase_func'
	/usr/local/apisix/apisix/plugin.lua:897: in function 'common_phase'
	/usr/local/apisix/apisix/init.lua:555: in function 'http_access_phase'
	access_by_lua(nginx.conf:284):2: in main chunk, client: 172.18.0.1, server: _, request: "GET /dbt/actuator/health HTTP/1.1", host: "127.0.0.1:9080"

vaibhavsw avatar Sep 14 '22 03:09 vaibhavsw

        "return function(conf, ctx) httpc:request_uri(\"http://host.docker.internal:9191/health\"); end"

we need use

local http = require("resty.http")
local httpc = http.new()

I think you should learn a little bit about the basic syntax of Lua.

tzssangglass avatar Sep 14 '22 09:09 tzssangglass

This issue has been marked as stale due to 350 days of inactivity. It will be closed in 2 weeks if no further activity occurs. If this issue is still relevant, please simply write any comment. Even if closed, you can still revive the issue at any time or discuss it on the [email protected] list. Thank you for your contributions.

github-actions[bot] avatar Sep 08 '23 10:09 github-actions[bot]

This issue has been closed due to lack of activity. If you think that is incorrect, or the issue requires additional review, you can revive the issue at any time.

github-actions[bot] avatar Sep 22 '23 10:09 github-actions[bot]