You can also create or extend Kong Plugins and add the Event Hooks functionality for custom use cases. Businesses can use Event Hooks to bring enhanced awareness of change in a fully automated fashion, which ultimately increases enterprise visibility and reduces risk.
## Event Hooks Use Case
A customer is utilizing [Kong Workspaces](https://konghq.com/products/kong-enterprise/workspaces)Kong Workspaces to provide a logical separation between the management and configuration of their APIs with various teams. Each team uses a different logging tool or endpoint. By configuring the HTTP Log plugin with different logging endpoints for each Workspace, each team's API traffic is logged to separate logging endpoints. But what if a customer wants a global view of all API traffic without configuring another plugin?
### *By adding Event Hooks to the HTTP Log plugin, we can easily solve this problem, with the added benefit of not affecting existing functionality. *
With just a few lines of code, it is possible to extend the HTTP Log plugin to provide the functionality required.
## Kong Plugin Development Refresher
A Kong plugin allows you to inject custom logic in Lua, Go or JavaScript at several entry points in the lifecycle of a request and response or a TCP stream connection as it is proxied by Kong.
Since we are extending an existing plugin, we can simply add the Event Hook functionality and reuse functionality from the HTTP Log plugin.
To enable Event Hooks in the plugin, all that is required are three lines of code as follows:
- First, load the Event Hooks library using require “kong.enterprise_edition.event_hooks”
- In the :init_worker() phase of a plugin lifecycle, this is executed upon every Nginx worker process' startup. Here we register the event hook for our plugin using the event_hooks.publish() function.
- Next, in the :log() phase we add the event_hooks.emit() function. This fires an asynchronous request to an endpoint.
Below is the complete source code for handler.lua:
local event_hooks = require "kong.enterprise_edition.event_hooks"local HttpLogHandler = require "kong.plugins.http-log.handler"local HttpLogHandlerEnterprise = { PRIORITY = 12, VERSION = "0.1.0",}function HttpLogHandlerEnterprise:init_worker()
-- register event hook
event_hooks.publish("http-log-enterprise","log",{ fields = {"consumer","ip","service","route"}, unique = {"consumer","ip","service"}, description = "Run an event for a request",})
end
function HttpLogHandlerEnterprise:log(conf)
-- execute event hook
event_hooks.emit("http-log-enterprise","log",{ consumer = kong.client.get_consumer() or {}, ip = kong.client.get_forwarded_ip(), service = kong.router.get_service() or {}, route = kong.router.get_route() or {},})
-- execute HTTP Log core functionality
HttpLogHandler:log(conf)
end
return HttpLogHandlerEnterprise
And for schema.lua, we load the existing HTTP Log schema.lua library:
local HttpLogSchema = require "kong.plugins.http-log.schema"HttpLogSchema.name = "http-log-enterprise"
return HttpLogSchema
## Configuring the HTTP Log Event Listener
Once we have deployed the enhanced HTTP Log plugin, use the Kong Admin API to retrieve the Event Hook HTTP Log source. I recommend using a tool such as [Insomnia](https://insomnia.rest)Insomnia, which is a lightweight RESTFul client. Using the /event-hooks/sources endpoint returns a lot of data, but using Insomnia allows you to collapse fields you are not interested in.
The new HTTP Log plugin is returned in the list of Event Hook sources. Next, configure the HTTP Log Event Hook.
Here, we are configuring a webhook handler. This makes a JSON POST request to a provided URL with the event data as payload. In our case, by configuring the Event Hook functionality in the HTTP Plugin, we configure a URL webhook endpoint used by all HTTP Log instances. This means we can use the Event Hook Webhook feature to log all API traffic across a Kong Deployment using the Event Hooks feature. Still, each Team associated with a Kong Workspace can also continue to use their own logging instances or endpoints.
## Conclusion
By either reusing the built-in Event Hooks sources or adding to the Event Hook sources by extending or creating new plugins, Event Hooks greatly extends Kong's observability options.
As part of the Kong Gateway 2.6 release, we shipped a brand new jq plugin for anyone with an enterprise license to use. It’s like we combined the request and response transformer plugins to form a single, more powerful plugin—supercharging the w
In this blog post, we will explore how organizations can leverage Kong and OpenTelemetry to establish and monitor Service Level Objectives (SLOs) and manage error budgets more effectively. By tracking performance metrics and error rates against pred
We're pleased to announce the launch of Standard Webhooks! Kong has been part of the Technical Committee of this standard with other great companies like Svix (the initiator of the project), Ngrok, Zapier, Twillio, Lob, Mux, and Supabase. This was
While JSON-based APIs are ubiquitous in the API-centric world of today, many industries adapted internet-based protocols for automated information exchange way before REST and JSON became popular. One attempt to establish a standardized protocol sui
Developers will remember times when they were trying to figure out why something they were working on wasn’t behaving as expected. Hours of frustration, too much (or perhaps never enough) caffeine consumed, and sotto voce curses uttered. And then
Viktor Gamov and Rick Spurgeon co-authored this blog post. Kong Gateway provides dynamic plugin ordering allowing administrators to control plugin execution order. Dynamic plugin ordering was added in Kong Enterprise 3.0 and the full technical re
As more companies are undergoing digital transformation (resulting in a huge explosion of APIs and microservices), it's of paramount importance to get all the necessary data points and feedback to provide the best experience for both users and devel