Using Kong

In this guide, we will install http-mirror plugin to mirror traffic via Kong Gateway to HyperTest.

Brief Steps

  1. Install kong-plugin-http-mirror via luarocks (or clone plugin repository from here and get the .lua files)

  2. Migrate plugin from base plugin module (few changes in handler.lua)

  3. Add kong-plugin-http-mirror plugin in kong configuration file

  4. Restart Kong

  5. Add kong-plugin-http-mirror plugin to your service

Getting Started:

Get Plugin

1. Install kong-plugin-http-mirror plugin via luarocks

luarocks install kong-plugin-http-mirror

2. Migrate from BasePlugin module

Go to the folder where the plugin's lua files are created.

It should be something like /usr/local/share/lua/<version>/kong/plugins/kong-plugin-http-mirror/

cd /usr/local/share/lua/5.1/kong/plugins/kong-plugin-http-mirror/
vi handler.lua

Do the following changes to migrate plugin from base plugin module as it is now deprecated and removed in Kong Gateway

Migrating from BasePlugin module

The BasePlugin module is deprecated and has been removed from Kong Gateway. Since kong-plugin-http-mirror uses this module, replace the following section:

--  DEPRECATED --
local BasePlugin = require "kong.plugins.base_plugin"
local HttpMirrorHandler = BasePlugin:extend()
HttpMirrorHandler.VERSION  = "1.0.0"
HttpMirrorHandler.PRIORITY = 10

with the current equivalent:

local HttpMirrorHandler = {
  VERSION  = "1.0.0",
  PRIORITY = 10,
}

You don’t need to add a :new() method or call any of the HttpMirrorHandler.super.XXX:(self) methods.

Or Just replace the content of handler.lua with below content

handler.lua
---
--- Created by liuxiaodong
--- Modified by lgazo (v1), nhp0712 (v2)
--- DateTime: 2019/4/3 19:10
---
local mirror = require "kong.plugins.kong-plugin-http-mirror.mirror"
--- local BasePlugin = require "kong.plugins.base_plugin"

--- local HttpMirrorHandler = BasePlugin:extend()

--- HttpMirrorHandler.PRIORITY = 1
local HttpMirrorHandler = {
    VERSION  = "1.0.0",
    PRIORITY = 1
  }

-- function HttpMirrorHandler:new()
--     HttpMirrorHandler.super.new(self, "http-mirror")
-- end

function HttpMirrorHandler:access(conf)
    -- HttpMirrorHandler.super.log(self)
    if conf.mirror_request_body then
        ngx.req.read_body()
    end
end

function HttpMirrorHandler:log(conf)
    -- HttpMirrorHandler.super.log(self)
    local request = mirror.serialize(conf)
    local ok, err = ngx.timer.at(0, mirror.do_mirror, conf, request)
    if not ok then
        ngx.log(ngx.ERR, "mirror plugin failed to create timer: ", err)
    end

end

return HttpMirrorHandler

Install Plugin in Kong

Add the kong-plugin-http-mirror plugin in kong.conf configuration for each kong node.

The file is by default located in /etc/kong/kong.conf.default or /etc/kong/kong.conf.

If you are working on default file. Make a copy via below and edit it and use that to start kong

cp /etc/kong/kong.conf.default /etc/kong/kong.conf
vi /etc/kong/kong.conf
kong.conf
# uncomment/add in your plugins line
plugins = bundled,kong-plugin-http-mirror

If you are manually adding the plugin with lua files, then also give the path to kong plugin directory in lua_package_path variable

If the plugin kong-plugin-http-mirror is located on the file system and the handler file is in the following directory:

/usr/local/share/lua/5.1/kong/plugins/kong-plugin-http-mirror/handler.lua

The location of the kong directory is /usr/local/share/lua/5.1/, so the proper path setup would be:

lua_package_path = /usr/local/share/lua/5.1/?.lua;;

Restart Kong

Restart Kong using above edited conf file

kong restart -c /etc/kong/kong.conf -v

Verify Plugin is installed in Kong

curl -X GET http://<kong_endpoint>/plugins/

verify the plugins has been added to kong

Add Plugin to Service

Add kong-plugin-http-mirror to your service via REST call

curl -X POST http://<kong_endpoint>/services/<service_name>/plugins/ --data "name=kong-plugin-http-mirror" --data "config.mirror_request_body=true" --data "config.mirror_endpoints=http://<hypertest-vm-ip:logger-port>"

Now traffic coming to this app is being mirrored to HyperTest.

To Verify the mirroring setup, hit any API on the application and check for request in "last mirrored requests" section or Session page in HyperTest.

Last updated