How to view the contents of shared dictionaries

Uses: Kong Gateway
TL;DR

How do I view the contents of the shared dictionaries in Kong?

Use a Pre-Function plugin with Lua code that calls ngx.shared["kong_rate_limiting_counters"]:get_keys(2000) and logs each key with kong.log.err. The keys appear in the Kong Gateway error log.

Steps

The following examples show Pre-Function plugin code that inspects the kong_rate_limiting_counters dictionary for the first 2000 keys.

The Lua code:

kong.log.err("PRE FUNCTION EXECUTED")
local keys, err = ngx.shared["kong_rate_limiting_counters"]:get_keys(2000)
if not keys then
   kong.log.err("cannot fetch keys in shared dict! ", err)
else
   kong.log.err("keys retrieved: ", #keys)
   for _, key in ipairs(keys) do
       kong.log.err(key)
   end
end
kong.log.err("PRE FUNCTION ENDED")

K8s plugin code:

apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name: capture-kong-locks-pre-function
config:
  access:
  - |
    kong.log.err("PRE FUNCTION EXECUTED")
    local keys, err = ngx.shared["kong_rate_limiting_counters"]:get_keys(2000)
    if not keys then
      kong.log.err("cannot fetch keys in shared dict! ", err)
    else
      kong.log.err("keys retrieved: ", #keys)
      for _, key in ipairs(keys) do
        kong.log.err(key)
      end
    end
    kong.log.err("PRE FUNCTION ENDED")
plugin: pre-function

The result in the Kong log:

2023/02/06 07:44:30 [error] 2207#0: *990 [kong] [string "kong.log.err("PRE FUNCTION EXECUTED")..."]:6 [pre-function] keys retrieved: 4, client: 172.18.0.1, server: kong, request: "GET /bin HTTP/1.1", host: "localhost:8000"
2023/02/06 07:44:30 [error] 2207#0: *990 [kong] [string "kong.log.err("PRE FUNCTION EXECUTED")..."]:8 [pre-function] jLcEZU3Fr004xsxam1jELW07UfLf8D7p|1675669469|1|172.18.0.1|diff, client: 172.18.0.1, server: kong, request: "GET /bin HTTP/1.1", host: "localhost:8000"
2023/02/06 07:44:30 [error] 2209#0: *991 [kong] [string "kong.log.err("PRE FUNCTION EXECUTED")..."]:1 [pre-function] PRE FUNCTION EXECUTED, client: 172.18.0.1, server: kong, request: "GET /bin HTTP/1.1", host: "localhost:8000"
2023/02/06 07:44:30 [error] 2207#0: *990 [kong] [string "kong.log.err("PRE FUNCTION EXECUTED")..."]:8 [pre-function] jLcEZU3Fr004xsxam1jELW07UfLf8D7p|1675669469|1|172.18.0.1|sync, client: 172.18.0.1, server: kong, request: "GET /bin HTTP/1.1", host: "localhost:8000"
2023/02/06 07:44:30 [error] 2207#0: *990 [kong] [string "kong.log.err("PRE FUNCTION EXECUTED")..."]:8 [pre-function] jLcEZU3Fr004xsxam1jELW07UfLf8D7p|1675669470|1|172.18.0.1|diff, client: 172.18.0.1, server: kong, request: "GET /bin HTTP/1.1", host: "localhost:8000"
2023/02/06 07:44:30 [error] 2207#0: *990 [kong] [string "kong.log.err("PRE FUNCTION EXECUTED")..."]:8 [pre-function] jLcEZU3Fr004xsxam1jELW07UfLf8D7p|1675669470|1|172.18.0.1|sync, client: 172.18.0.1, server: kong, request: "GET /bin HTTP/1.1", host: "localhost:8000"
2023/02/06 07:44:30 [error] 2207#0: *990 [kong] [string "kong.log.err("PRE FUNCTION EXECUTED")..."]:11 [pre-function] PRE FUNCTION ENDED, client: 172.18.0.1, server: kong, request: "GET /bin HTTP/1.1", host: "localhost:8000"

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!