Attempt to perform arithmetic on field KONG_HEADER_FILTER_STARTED_AT (a nil value)

Good Day,

I am currently trying to create a plugin similar to Kong-middleman by pantsel, however I needed that Authorization header that is why I opted to create a new one. I have successfully call the plugin that calls a http request. However, when it is about to proceed to call the endpoint api, I am getting this error.

74 failed to run body_filter_by_lua: /usr/local/share/lua/5.1/kong/core/handler.lua:556: attempt to perform arithmetic on field ‘KONG_HEADER_FILTER_STARTED_AT’ (a nil value)

I’m not sure why it triggers that. can anyone help me?

Thank you in advance.

Hi,

With the information you are giving us it’s difficult to know what is wrong. Do you encounter this problem without your plugin? If you don’t, then the problem is in your plugin.

The problem might be that your plugin has a header_filter section which does not begin with <your-plugin>.super.header_filter(self). This the place where KONG_HEADER_FILTER_STARTED_AT is set. If you don’t have that line, please try including it and see if it solves your issue.

If not, can you share your plugin code, or at least part of it, in order for us to give it a look?

Update: Just found the github issue you created ( https://github.com/Kong/kong/issues/3236 ), which has a little more info. However my answer remains the same: with what you have given us, a missing call to super.header_filter(self) looks like the most possible candidate.

I tried to reproduce this locally with the kong-middle man plugin and was not able to. You must be doing something weird in your own plugin and we cannot help you without you showing us the code.

Btw: I highly discourage using the kong-middleman plugin. The plugin uses LuaSocket which is blocking and will horribly hurt your performance. NGINX will not be able to handle any sort of production traffic with this plugin, even at low traffic rates. Even locally we can see Kong can handle about 1req/s only:

wrk -t 4 -c 80 -d 30s http://localhost:8000/here
Running 30s test @ http://localhost:8000/here
  4 threads and 80 connections
Thread Stats   Avg      Stdev     Max   +/- Stdev
  Latency     0.00us    0.00us   0.00us     nan%
  Req/Sec     0.71      1.89     5.00     85.71%
  37 requests in 30.10s, 12.76KB read                                                                                                                                                         
  Socket errors: connect 0, read 0, write 0, timeout 37
Requests/sec:      1.23
Transfer/sec:     433.92B

Thank you so much for this! :slight_smile:

Hi @thibaultcha, I was able to fix the problem on this one. Apparently the For loop inside the lua plugin somehow is causing the problem. Logically it is correct, but it affects the flow of the lua-nginx. After removing the loop, the error was gone.

Thank you for the advice. I opted to use the middleman since I needed the pre http post request before hitting the endpoint api to do some validations. I believe (after reading some post in the kong community) the multiple endpoints in one api call is not yet available in Kong.

Do you have other recommendations on how I can do pre http post?

@sukidesuaoi You certainly can make HTTP requests with OpenResty - by using lua-resty-http (or any other HTTP library as long as it is based on OpenResty’s cosockets API).

If you want several, concurrent HTTP requests you can program them yourself by using the ngx_lua ngx.thread API.

Hi @thibaultcha! Thank you for your advise. I will definitely try it out. :slight_smile:

@sukidesuaoi hello, i get the same program, can you describe detail about how to removing the loop,
will think you very much