Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua script post at proxy level is not executed when backend returns context deadline exceeded #988

Closed
sebampuero opened this issue Mar 25, 2025 · 0 comments
Labels

Comments

@sebampuero
Copy link

Environment info:

  • KrakenD version: 2.9.3
  • System info: openshift
  • Backend technology: Python FastAPI

Describe what are you trying to do:
Hello, I want to modify the merged request of two backends when one of them fails with an error like context deadline exceeded. Currently, both backends send a json payload (same structure) with different contents. KrakenD merges them according to the backend (by using the deny fields). The problem relies when one of them fails, our client wont be able to parse the HTTP Headers returned (X-KrakenD-Complete) so that we need to modify the response.

Your configuration file:

{
      "$schema":"https://www.krakend.io/schema/krakend.json",
      "version":3,
      "name":"KrakenD - API Gateway",
      "port": 8443,
      "timeout":"5000ms",
      "tls": {
        "keys": [
          {
            "public_key": "key",
            "private_key": "key"
          }
        ]
      },
      "extra_config": {
        
        "telemetry/opentelemetry": {
          "service_name": "krakend_prometheus_service",
          "metric_reporting_period": 1,
          "exporters": {
              "prometheus": [
                  {
                      "name": "prometheus",
                      "port": 9090,
                      "process_metrics": true,
                      "go_metrics": true
                  }
              ]
          }
        }
      },
      "endpoints":[
        {
          "endpoint":"/myendpoint",
          "method":"GET",
          "extra_config": {
            "modifier/lua-proxy": {
              "sources": [
                "/etc/krakend/modifier.lua"
              ],
              "post": "processPostBody()"
            }
          },
          ....
          ....

Lua Script:

function processPostBody()
      local r = response.load()
      local responseData = r:data()
      local keys = responseData:keys()

      print("Number of keys: ", keys:len())
      
      if keys:len() < 18 then
        for i = 0, keys:len() - 1 do
          local key = keys:get(i)
          responseData:del(key)
        end
      end
    end

Commands used:

Logs:
2025/03/25 16:33:01 KRAKEND ERROR: [ENDPOINT: /v1/endpoint] Error #0: context deadline exceeded
[GIN] 2025/03/25 - 16:33:01 | 200 | 4.255016405s | 10.114.5.16 | GET "/v1/endpoint"
Error #1: context deadline exceeded
-> with faulty backend

[GIN] 2025/03/25 - 16:32:49 | 200 | 23.557788ms | 10.114.5.16 | GET "/v1/endpoint"
Number of keys: 18
-> not faulty backend

Additional comments:
So basically the logic was to remove all keys when the merged response has less keys as expected in the json object (result of missing one of the backends, because of the deny fields the merged response would have less number of keys. This code was just a test).
The problem is , as noted in the logs above, the script is only executed when both backends respond. If one of them is not reachable then KrakenD does not execute the Lua script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant