-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.lua
119 lines (102 loc) · 4.42 KB
/
handler.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
---
--- Created by ethemcemozkan.
--- DateTime: 29/11/2019 14:07
---
local SHOXrayPlugin = {
VERSION = "3.0.0",
PRIORITY = 10,
}
local function isempty(s)
return s == nil or s == ''
end
local function findEnv(url)
if url:find(".prd.") then
return "prd"
elseif url:find(".stg.") then
return "stg"
elseif url:find(".tst.") then
return "tst"
elseif url:find(".acc.") then
return "acc"
end
end
local function findServiceName(url, env)
serviceName = url:match("[^.]+")
return serviceName .. "-" .. env
end
local function generateId()
local random = math.random
local template ='xxxxxxxxxxxxxxxx'
math.randomseed(os.time())
return string.gsub(template, '[xy]', function (c)
local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
return string.format('%x', v)
end)
end
function string.toHex(str)
return (str:gsub('.', function (c)
return string.format('%02X', string.byte(c))
end))
end
local function sendUDP(premature,msg,host,port)
local socket = ngx.socket.udp
local udp = assert(socket())
assert(udp:setpeername(host, port))
assert(udp:send(msg))
assert(udp:close())
end
function SHOXrayPlugin:access(config)
SHOXrayPlugin.super.access(self)
local traceIdHeader = kong.request.get_header("X-Amzn-Trace-Id")
if not isempty(traceIdHeader) then
local method = kong.request.get_method()
local host = kong.request.get_host()
local url = kong.request.get_scheme() .. "://" .. host .. kong.request.get_path()
local env = findEnv(url)
local serviceName = findServiceName(host, env)
local traceId = traceIdHeader:match("Root=(.+)")
local parentSegmentId = nil
local parentSegment = nil
if traceId:find(";Parent=") then
parentSegmentId = traceId:match("Parent=(.+)")
if parentSegmentId:find(";") then
k, l = parentSegmentId:find(";")
parentSegmentId = parentSegmentId:sub(0,k-1)
end
i, j = traceId:find(";Parent=")
traceId = traceId:sub(0,i-1)
end
if not parentSegmentId then
parentSegmentId = ""
parentSegment = ""
else
parentSegment = ", \"parent_id\": \"".. parentSegmentId .."\""
end
local startTime = ngx.now()
local segmentId = tostring(generateId())
local subSegmentId = tostring(generateId())
kong.ctx.plugin.subSegmentDoc = "\"id\": \"" .. subSegmentId .. "\", \"start_time\": ".. tostring(startTime) .. ", \"name\": \"".. serviceName.."\", \"namespace\": \"remote\""
kong.ctx.plugin.segmentDoc = "\"trace_id\":\"" .. traceId .. "\", \"id\": \"" .. segmentId .. "\", \"start_time\": ".. tostring(startTime) .. ", \"name\": \"Kong-" .. env .. "\", \"origin\": \"AWS::ECS::Container\"" .. parentSegment
kong.ctx.plugin.httpPart = ",\"http\": {\"request\" : { \"url\" : \"".. url .."\", \"method\" : \"".. method .."\"}"
local inProgress = ",\"in_progress\": true"
local header = "{\"format\": \"json\", \"version\": 1}"
local traceData = header .. "\n" .. "{".. kong.ctx.plugin.segmentDoc .. kong.ctx.plugin.httpPart .."}, \"subsegments\": [{".. kong.ctx.plugin.subSegmentDoc .. inProgress .."}]" .. inProgress .. " }"
ngx.req.set_header("X-Amzn-Trace-Id", "Root=" .. traceId..";Parent="..subSegmentId..";Sampled=1")
sendUDP(false,traceData,config.xray_host,config.xray_port)
end
end
function SHOXrayPlugin:log(config)
SHOXrayPlugin.super.log(self)
local traceIdHeader = kong.request.get_header("X-Amzn-Trace-Id")
if not isempty(traceIdHeader) then
local endTime = ngx.now()
local response = kong.response.get_status()
kong.ctx.plugin.httpPart = kong.ctx.plugin.httpPart .. ",\"response\" : { \"status\" : ".. response .." }}"
kong.ctx.plugin.segmentDoc = kong.ctx.plugin.segmentDoc .. ",\"end_time\": " .. tostring(endTime) .. kong.ctx.plugin.httpPart
kong.ctx.plugin.subSegmentDoc = kong.ctx.plugin.subSegmentDoc .. ",\"end_time\": " .. tostring(endTime)
local header = "{\"format\": \"json\", \"version\": 1}"
local traceData = header .. "\n" .. "{".. kong.ctx.plugin.segmentDoc .. ", \"subsegments\": [{".. kong.ctx.plugin.subSegmentDoc .. "}] }"
ngx.timer.at(0, sendUDP,traceData,config.xray_host,config.xray_port )
end
end
return SHOXrayPlugin