Skip to content

Commit

Permalink
refactor(correlation-id) remove kong.tools.* dependency from correlat…
Browse files Browse the repository at this point in the history
…ion-id plugin
  • Loading branch information
bungle committed Oct 19, 2018
1 parent 561245c commit f84af08
Showing 1 changed file with 48 additions and 37 deletions.
85 changes: 48 additions & 37 deletions kong/plugins/correlation-id/handler.lua
Original file line number Diff line number Diff line change
@@ -1,78 +1,89 @@
-- Copyright (C) Kong Inc.

local BasePlugin = require "kong.plugins.base_plugin"
local req_set_header = ngx.req.set_header
local req_get_headers = ngx.req.get_headers
local uuid = require("kong.tools.utils").uuid
local uuid = require "kong.tools.utils".uuid

local CorrelationIdHandler = BasePlugin:extend()

CorrelationIdHandler.PRIORITY = 1
CorrelationIdHandler.VERSION = "0.1.0"
local kong = kong


local worker_uuid
local worker_counter
local generators


local fmt = string.format
local now = ngx.now
local worker_pid = ngx.worker.pid()

local generators = setmetatable({
["uuid"] = function()
return uuid()
end,
["uuid#counter"] = function()
worker_counter = worker_counter + 1
return worker_uuid .. "#" .. worker_counter
end,
["tracker"] = function()
local var = ngx.var
return fmt("%s-%s-%d-%s-%s-%0.3f",
var.server_addr,
var.server_port,
worker_pid,
var.connection, -- connection serial number
var.connection_requests, -- current number of requests made through a connection
now() -- the current time stamp from the nginx cached time.
)
end,
}, { __index = function(self, generator)
ngx.log(ngx.ERR, "Invalid generator: " .. generator)
do
local worker_pid = ngx.worker.pid()
local now = ngx.now
local fmt = string.format

generators = {
["uuid"] = function()
return uuid()
end,
["uuid#counter"] = function()
worker_counter = worker_counter + 1
return worker_uuid .. "#" .. worker_counter
end,
["tracker"] = function()
local var = ngx.var

return fmt("%s-%s-%d-%s-%s-%0.3f",
var.server_addr,
var.server_port,
worker_pid,
var.connection, -- connection serial number
var.connection_requests, -- current number of requests made through a connection
now() -- the current time stamp from the nginx cached time.
)
end,
}
end
})


local CorrelationIdHandler = BasePlugin:extend()


CorrelationIdHandler.PRIORITY = 1
CorrelationIdHandler.VERSION = "0.2.0"


function CorrelationIdHandler:new()
CorrelationIdHandler.super.new(self, "correlation-id")
end


function CorrelationIdHandler:init_worker()
CorrelationIdHandler.super.init_worker(self)
worker_uuid = uuid()
worker_counter = 0
end


function CorrelationIdHandler:access(conf)
CorrelationIdHandler.super.access(self)

-- Set header for upstream
local header_value = req_get_headers()[conf.header_name]
local header_value = kong.request.get_header(conf.header_name)
if not header_value then
-- Generate the header value
header_value = generators[conf.generator]()
req_set_header(conf.header_name, header_value)
kong.service.request.set_header(conf.header_name, header_value)
end

if conf.echo_downstream then
-- For later use, to echo it back downstream
ngx.ctx.correlationid_header_value = header_value
kong.ctx.plugin.correlationid_header_value = header_value
end
end


function CorrelationIdHandler:header_filter(conf)
CorrelationIdHandler.super.header_filter(self)

if conf.echo_downstream then
ngx.header[conf.header_name] = ngx.ctx.correlationid_header_value
kong.response.set_header(conf.header_name, kong.ctx.plugin.correlationid_header_value)
end
end


return CorrelationIdHandler

0 comments on commit f84af08

Please sign in to comment.