Kong Plugin for Treblle
The Kong plugin captures API requests and responses in real time through Kong Gateway and sends them to Treblle for monitoring, security, and analytics.
Prerequisites
- Docker installed (for MacOS or Linux)
- Git installed
- A Treblle account with an API key and Project ID
Installation Options
- MacOS with Docker
- Linux with Docker
MacOS Installation
-
Clone the repository
Terminal window git clone https://github.com/Treblle/treblle-kong.gitcd treblle-kong -
Build the Docker image
Terminal window docker build -t k:v1 . -
Start the container
Terminal window docker-compose up -d
Linux Installation
-
Prepare the project directory structure
kong-treblle-sandbox/│├── docker-compose.yml├── kong.conf├── Dockerfile└── kong.yml -
Creating the Dockerfile
The Dockerfile builds a custom Kong image with the Treblle plugin installed. It installs necessary dependencies, adds the Treblle plugin via luarocks, and configures Kong to use it.
FROM kong:3.4.0USER rootRUN apk add --no-cache git unzip curl luarocksRUN luarocks install --server=http://luarocks.org/manifests/treblle kong-plugin-treblleCOPY kong.conf /etc/kong/kong.confUSER kong -
Creating the kong.conf
The kong.conf file configures database connections and enables the Treblle plugin. It sets up PostgreSQL connection parameters and adds Treblle to the list of available plugins.
database = onpg_host = postgrespg_port = 5432pg_user = kongpg_password = kongpg_database = kongplugins = bundled,treblle -
Creating the docker-compose.yml
The docker-compose.yml orchestrates three containers: a PostgreSQL database, a Kong migration service that initializes the database, and the Kong service itself with the Treblle plugin enabled.
services:postgres:image: postgres:13environment:POSTGRES_DB: kongPOSTGRES_PASSWORD: kongPOSTGRES_USER: kongvolumes:- postgres_data:/var/lib/postgresql/datahealthcheck:test: ["CMD", "pg_isready", "-U", "kong"]interval: 30stimeout: 30sretries: 3kong-migration:image: kong:3.4.0command: kong migrations bootstrapenvironment:KONG_DATABASE: postgresKONG_PG_HOST: postgresKONG_PG_PASSWORD: kongKONG_PG_USER: kongdepends_on:postgres:condition: service_healthykong:build:context: .dockerfile: Dockerfilecontainer_name: kong-treblleenvironment:KONG_DATABASE: postgresKONG_PG_HOST: postgresKONG_PG_PASSWORD: kongKONG_PG_USER: kongKONG_PLUGINS: bundled,treblleKONG_PROXY_ACCESS_LOG: /dev/stdoutKONG_ADMIN_ACCESS_LOG: /dev/stdoutKONG_PROXY_ERROR_LOG: /dev/stderrKONG_ADMIN_ERROR_LOG: /dev/stderrKONG_ADMIN_LISTEN: 0.0.0.0:8001, 0.0.0.0:8444 sslports:- "9000:8000" # Proxy port- "9001:8001" # Admin API port- "9443:8443"- "9444:8444"depends_on:postgres:condition: service_healthykong-migration:condition: service_completed_successfullyhealthcheck:test: ["CMD", "kong", "health"]interval: 10stimeout: 10sretries: 10volumes:postgres_data: -
Start the containers
Terminal window docker-compose up -d
Configuration Steps
Follow these steps to configure Treblle with Kong. Commands vary slightly between MacOS and Linux installations.
For MacOS
# Verify plugin is enabled curl -i -X GET http://localhost:8001/plugins/enabled
# Create a service curl -i -X POST http://localhost:8001/services \ --data "name=httpbin-service" \ --data "url=https://httpbin.org"
# Create a route curl -i -X POST http://localhost:8001/services/httpbin-service/routes \ --data "name=httpbin-route-post" \ --data "paths[]=/httpbin" \ --data "methods[]=POST"
# Add Treblle plugin curl -i -X POST http://localhost:8001/services/httpbin-service/plugins \ --data "name=treblle" \ --data "config.api_key=YOUR_TREBLLE_API_KEY" \ --data "config.project_id=YOUR_TREBLLE_PROJECT_ID" \ --data "config.mask_keywords[]=Authorization" \ --data "config.mask_keywords[]=User-Agent" \ --data "config.mask_keywords[]=Cookie"
# Test curl -i -X POST http://localhost:8000/httpbin/post \ -H "Content-Type: application/json" \ -d '{"test": "data", "foo": "bar"}'
For Linux
# Create a service curl -i -X POST http://localhost:9001/services \ --data name=httpbin-service \ --data url=https://httpbin.org
# Create a route curl -i -X POST http://localhost:9001/services/httpbin-service/routes \ --data "name=httpbin-route" \ --data "paths[]=/test" \ --data "methods[]=POST"
# Add Treblle plugin curl -i -X POST http://localhost:9001/services/httpbin-service/plugins \ --data "name=treblle" \ --data "config.api_key=YOUR_TREBLLE_API_KEY" \ --data "config.project_id=YOUR_TREBLLE_PROJECT_ID" \ --data "config.mask_keywords[]=Authorization" \ --data "config.mask_keywords[]=API_Key" \ --data "config.mask_keywords[]=Secure-Token"
# Test curl -X POST http://localhost:9000/test/post
Troubleshooting
Enable Debug Logs
To print Treblle debug logs, set the debug flag to true:
curl -i -X POST http://localhost:8001/services/httpbin-service/plugins \ --data "name=treblle" \ --data "config.api_key=YOUR_TREBLLE_API_KEY" \ --data "config.project_id=YOUR_TREBLLE_PROJECT_ID" \ --data "config.debug=true"
Verification Commands (Linux)
Use these commands to verify your setup:
# Check Docker containers docker-compose ps
# View Kong logs docker-compose logs kong
Configuration Parameters
Parameter | Default | Description |
---|---|---|
config.api_key | The Treblle SDK token | |
config.project_id | The Treblle API ID | |
config.mask_keywords | Masking keywords for the payload | |
config.debug | false | Enables debug logging |
config.enable_compression | false | Compresses requests before sending |
config.connect_timeout | 1000 | Connection timeout (ms) |
config.send_timeout | 5000 | Send timeout (ms) |
config.request_max_body_size_limit | 100000 | Max request body size (bytes) |
config.response_max_body_size_limit | 100000 | Max response body size (bytes) |
For more advanced configuration options, refer to our GitHub repository.