My demo, Evolving your , features a custom plugin. I believe that the process of is relatively well-documented. However, I wanted to check the parameters of the function, especially the one. APIs Apache APISIX creating a custom plugin _M.access(conf, ctx) ctx The documentation states: The parameter caches data information related to the request. You can use to output it to for viewing. ctx core.log.warn(core.json.encode(ctx, true)) error.log Unfortunately, ultimately depends on nginx's logging, and its buffer is limited in size. Thanks to my colleague for finding . For this reason, the display is (heavily) truncated. I had to log data bit by bit; however, it was instructive. core.log Abhishek the info ctx The context The parameter is a Lua table. In Lua, table data structures are used for regular indexed access (akin to arrays) and key access (like hash maps). A single instance is used for each . ctx ctx request The Apache APISIX engine reads and writes data in the table. It's responsible for forwarding the latter from plugin to plugin. In turn, each plugin can also read and write data. ctx I resorted to a custom plugin to conditionally apply rate-limiting in the demo. The custom plugin is a copy-paste of the limit-count plugin. Note that the analysis is done in a specific context. Refrain from assuming the same data is available in your own. However, it should be a good starting point. Overview of the parameter ctx The data available in the parameter is overwhelming. To better understand it, we shall go from the more general to the more particular. Let's start from the overview. ctx : self-explanatory _plugin_name : either route ID or service ID conf_id : data set by the plugin. proxy_rewrite_regex_uri_capture proxy-rewrite : route ID the plugin is applied to route_id : route name the plugin is applied to route_name : URI for which matching was done real_current_req_matched_path : etcd-related revision - see below conf_version : references the object and a cache of data about the request, , URI, method, etc. var ctx e.g. : the route that was matched based on host header/URI and/or ; see below matched_route remote_addr : pairs of plugin/data - see below plugins Matched route The row is a complex data tree that deserves a detailed description. matched_route : access key in the datastore key etcd , and : these attributes are related to etcd and how it stores metadata associated with revisions. Different revisions of a single key are logged in the and fields. The former points to the initial created row ID and is constant throughout the changes, while the latter points to the row ID of the previous value. created_index modifiedIndex orig_modifiedIndex create_revision pre_revision Apache APISIX maps them respectively to the and values and uses them for caching. In many places, is later assigned to - see above. created_index modifiedIndex created_index conf_version : after a plugin configuration is merged with the route configuration, the current is assigned to . It allows saving CPU cycles if one attempts to apply the same plugin config later in the call chain. prev_plugin_config_ver modifiedIndex prev_plugin_config_ver : replaced with update_count modifiedIndex : whether the matched route references an upstream with a domain, , , or not, , has_domain e.g. http://httpbin.org e.g. 192.168.0.1 : temporary placeholder used if a route has plugins defined directly and reference a plugins config orig_plugins : list of functions scheduled to be called after a plugin has been created clean_handlers has keys related to how the route was created, as well as a couple of others: value curl http://apisix:9180/apisix/admin/routes/2 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "name": "Versioned Route to Old API", "methods": ["GET"], "uris": ["/v1/hello", "/v1/hello/", "/v1/hello/*"], "upstream_id": 1, "plugin_config_id": 1 }' : since we didn't set it, it has a default value 0. is essential when multiple routes match to determine which one to apply. priority Priority : self-explanatory create_time : self-explanatory update_time : references to plugin's function plugins : I couldn't find this status Plugins The value contains plugin-related data in an indexed-based Lua table. Each plugin has two entries: the first (even-indexed) entry contains data related to the plugin in general, , its schema; while the second (odd-index) entry data is related to its configuration in the current route. plugins e.g. My setup has two plugins, hence four entries, but to keep things simpler, I kept only a single plugin in the following diagram: Key values match directly to the plugin schema and configuration; you can check the whole descriptions directly in the plugin. A final trick I initially had issues printing the table because of the nginx buffer limit and had to do it bit by bit. However, you can print it to a file. ctx Here's the function, courtesy of my colleague : Zeping Bai local file, err = io.open("conf/ctx.json", "w+") if not file then ngx.log(ngx.ERR, "failed to open file: ", err) return end file.write(core.json.encode(ctx, true) .. "\n") file.close() Here's the whole data representation, in case you have good eyes: Alternatively, here's the . PlantUML representation Conclusion In this post, I described the structure of the parameter in the function. While specific entries vary from configuration to configuration, it gives a good entry point into data manipulated by plugins. ctx access() It's also a good reminder that even if you're not fluent in a language or a codebase, you can get quite a lot of information by logging some variables. To go further: ctx parameter Originally published at on September 24th, 2023 A Java Geek