Configure an Event Hook to log events with Kong Gateway

Uses: Kong Gateway
TL;DR

The log Event Hook handler can write log events. You can configure an Event Hook using the log handler to write to the log file every time a CRUD event happens on the Consumer entity by issuing a POST request to the /event-hooks endpoint.

Prerequisites

This tutorial requires Kong Gateway Enterprise. If you don’t have Kong Gateway set up yet, you can use the quickstart script with an enterprise license to get an instance of Kong Gateway running almost instantly.

  1. Export your license to an environment variable:

     export KONG_LICENSE_DATA='LICENSE-CONTENTS-GO-HERE'
    
  2. Run the quickstart script:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -e KONG_LICENSE_DATA 
    

    Once Kong Gateway is ready, you will see the following message:

     Kong Gateway Ready
    

cURL is used to send requests to Kong Gateway. curl is pre-installed on most systems.

Before you can use Event Hooks for the first time, Kong Gateway needs to be reloaded. To do this, run kong-reload. You can verify that this worked by issuing a GET request to the /event-hooks/ endpoint.

Create the Event Hook

The log Event Hook configuration specifies an event and a source. In this guide you will create an Event Hook that listens for CRUD events on the Consumers entity. This Event Hook will create a log entry when a CRUD event occurs.

Create a long Event Hook on the consumers event using the crud source:

curl -i -X POST http://localhost:8001/event-hooks/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "source": "crud",
      "event": "consumers",
      "handler": "log"
    }
    '

Validate the webhook

Important: Before you can use Event Hooks for the first time, Kong Gateway needs to be reloaded.

Use the Admin API to create a new Consumer:

curl -i -X POST http://localhost:8001/consumers/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "username": "my-consumer"
    }
    '

Review the logs at /usr/local/kong/logs/error.log for an update about the creation of this Consumer. The log will look similar to this:

172.19.0.1 - - [16/Dec/2024:15:57:15 +0000] "POST /consumers HTTP/1.1" 409 147 "-" "HTTPie/2.4.0"
2024/12/16 15:57:26 [notice] 68854#0: *819021 +--------------------------------------------------+, context: ngx.timer, client: 172.19.0.1, server: 0.0.0.0:8001
2024/12/16 15:57:26 [notice] 68854#0: *819021 |[kong] event_hooks.lua:?:452 "log callback: " { "consumers", "crud", {|, context: ngx.timer, client: 172.19.0server: 0.0.0.0:8001
2024/12/16 15:57:26 [notice] 68854#0: *819021 |    entity = {                                    |, context: ngx.timer, client: 172.19.0.1, server: 0.0.0.0:8001
2024/12/16 15:57:26 [notice] 68854#0: *819021 |      created_at = 1702735046,                    |, context: ngx.timer, client: 172.19.0.1, server: 0.0.0.0:8001
2024/12/16 15:57:26 [notice] 68854#0: *819021 |      id = "4757bd6b-8d54-4b08-bf24-01e346a9323e",|, context: ngx.timer, client: 172.19.0.1, server: 0.0.0.0:8001
2024/12/16 15:57:26 [notice] 68854#0: *819021 |      type = 0,                                   |, context: ngx.timer, client: 172.19.0.1, server: 0.0.0.0:8001
2024/12/16 15:57:26 [notice] 68854#0: *819021 |      username = "my-consumer"                    |, context: ngx.timer, client: 172.19.0.1, server: 0.0.0.0:8001
2024/12/16 15:57:26 [notice] 68854#0: *819021 |    },                                            |, context: ngx.timer, client: 172.19.0.1, server: 0.0.0.0:8001
2024/12/16 15:57:26 [notice] 68854#0: *819021 |    operation = "create",                         |, context: ngx.timer, client: 172.19.0.1, server: 0.0.0.0:8001
2024/12/16 15:57:26 [notice] 68854#0: *819021 |    schema = "consumers"                          |, context: ngx.timer, client: 172.19.0.1, server: 0.0.0.0:8001
2024/12/16 15:57:26 [notice] 68854#0: *819021 |  }, 68854 }                                      |, context: ngx.timer, client: 172.19.0.1, server: 0.0.0.0:8001
2024/12/16 15:57:26 [notice] 68854#0: *819021 +--------------------------------------------------+, context: ngx.timer, client: 172.19.0.1, server: 0.0.0.0:8001

Cleanup

curl -Ls https://get.konghq.com/quickstart | bash -s -- -d
Something wrong?

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!