Skip to content

Commit 32c2ca0

Browse files
Added mock tracing logger for testing purposes
1 parent ea4efac commit 32c2ca0

File tree

3 files changed

+64
-50
lines changed

3 files changed

+64
-50
lines changed

src/lua/api-gateway/tracking/log/tracingRulesLogger.lua

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
]]
1414

1515
local cjson = require "cjson"
16-
local _M = {}
16+
local su = require "api-gateway.tracking.util.stringutil"
1717

18-
local kinesisLogger
18+
local _M = {}
1919

2020
function _M:new(o)
2121
local o = o or {}
@@ -24,45 +24,30 @@ function _M:new(o)
2424
return o
2525
end
2626

27-
function _M:sendKinesisMessage(kinesis_data)
28-
29-
local kinesisSwitch = ngx.var.kinesisSwitch
30-
if (kinesisSwitch == nil or kinesisSwitch == "") then
31-
kinesisSwitch = "on"
32-
end
33-
if ( kinesisLogger ~= nil and (kinesisSwitch ~= nil and kinesisSwitch == "on")) then
34-
local partition_key = ngx.utctime() .."-".. math.random(ngx.now() * 1000)
35-
36-
-- Send logs to kinesis
37-
kinesisLogger:logMetrics(partition_key, cjson.encode(kinesis_data))
38-
else
39-
ngx.log(ngx.WARN, "Trace info not sent to kinesis")
40-
end
41-
42-
end
43-
44-
function _M:getKinesisMessage(rule)
45-
local kinesis_data = {}
27+
function _M:buildTraceMessage(rule)
28+
local data = {}
4629

47-
kinesis_data["id"] = tostring(rule.id)
48-
kinesis_data["domain"] = tostring(rule.domain)
49-
kinesis_data["request_body"] = tostring(ngx.var.request_body)
30+
data["id"] = tostring(rule.id)
31+
data["domain"] = tostring(rule.domain)
32+
data["request_body"] = tostring(ngx.var.request_body)
33+
data["status"] = tostring(ngx.var.status)
5034

5135
for key,value in pairs(ngx.header) do
52-
kinesis_data[tostring(key)] = tostring(value)
36+
data[tostring(key)] = tostring(value)
5337
end
5438

55-
local meta = rule.meta
39+
local meta = rule.meta:split(";")
5640
local meta_length = table.getn(meta)
5741

5842
local var_value, index
5943
for index = 1, meta_length do
6044
local variableManager = ngx.apiGateway.tracking.variableManager
61-
var_value = variableManager:getRequestVariable(meta[index], nil)
62-
kinesis_data[tostring(meta[index])] = tostring(var_value)
45+
local var_name = string.sub(su.trim(meta[index]), 2);
46+
var_value = variableManager:getRequestVariable(var_name, nil)
47+
data[var_name] = tostring(var_value)
6348
end
6449

65-
return kinesis_data
50+
return data
6651
end
6752

6853
---
@@ -74,9 +59,9 @@ function _M:log_trace_rules(config_obj)
7459
ngx.log(ngx.WARN, "Please initialize RequestTrackingManager before calling this method")
7560
end
7661

77-
kinesisLogger = ngx.apiGateway.getAsyncLogger("api-gateway-debugging")
78-
if ( kinesisLogger == nil ) then
79-
ngx.log(ngx.WARN, "Could not track request. kinesis logger should not be nil")
62+
local tracingLogger = ngx.apiGateway.getAsyncLogger("api-gateway-debugging")
63+
if ( tracingLogger == nil ) then
64+
ngx.log(ngx.WARN, "Could not track request. Tracing logger should not be nil")
8065
return
8166
end
8267

@@ -86,13 +71,14 @@ function _M:log_trace_rules(config_obj)
8671
if (tracing_rules == nil) then
8772
return
8873
end
89-
-- 2. for each tracing rule matching the request publish a kinesis message asyncronously
74+
-- 2. for each tracing rule matching the request publish a tracing message asyncronously
9075
for i, rule in pairs(tracing_rules) do
9176
if ( rule ~= nil ) then
92-
local message = self:getKinesisMessage(rule)
77+
local message = self:buildTraceMessage(rule)
9378
if ( message ~= nil ) then
94-
ngx.log(ngx.DEBUG, "Sending tracing info to kinesis: " .. cjson.encode(message))
95-
self:sendKinesisMessage(message)
79+
local partition_key = ngx.utctime() .."-".. math.random(ngx.now() * 1000)
80+
ngx.log(ngx.DEBUG, "Logging tracing info: " .. cjson.encode(message))
81+
tracingLogger:logMetrics(partition_key, cjson.encode(message));
9682
end
9783
end
9884
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
local stringutil = {}
2+
3+
-- http://coronalabs.com/blog/2013/04/16/lua-string-magic/
4+
function string:split( pattern, results )
5+
if not results then
6+
results = { }
7+
end
8+
local start = 1
9+
local split_start, split_end = string.find( self, pattern, start )
10+
while split_start do
11+
table.insert( results, string.sub( self, start, split_start - 1 ) )
12+
start = split_end + 1
13+
split_start, split_end = string.find( self, pattern, start )
14+
end
15+
table.insert( results, string.sub( self, start ) )
16+
return results
17+
end
18+
19+
-- http://coronalabs.com/blog/2013/04/16/lua-string-magic/
20+
local function trim( s )
21+
return string.match( s,"^()%s*$") and "" or string.match(s,"^%s*(.*%S)" )
22+
end
23+
24+
stringutil.trim = trim
25+
26+
return stringutil

test/perl/api-gateway/tracking/log/tracingRulesLogger.t

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use Cwd qw(cwd);
2424

2525
repeat_each(1);
2626

27-
plan tests => repeat_each() * (blocks() * 5) + 1 ;
27+
plan tests => repeat_each() * (blocks() * 5) + 3 ;
2828

2929
my $pwd = cwd();
3030

@@ -42,7 +42,11 @@ our $HttpConfig = <<_EOC_;
4242
ngx.apiGateway.tracking = require "api-gateway.tracking.factory"
4343
4444
local function get_logger(name)
45-
return {}
45+
return {
46+
logMetrics = function (self, key, value)
47+
ngx.log(ngx.INFO, "Received " .. tostring(value))
48+
end
49+
}
4650
end
4751
ngx.apiGateway.getAsyncLogger = get_logger
4852
';
@@ -81,14 +85,12 @@ __DATA__
8185
8286
error_log ../test-logs/tracingRequestValidator_test1_error.log debug;
8387
84-
location ~ /trace {
85-
set $validate_service_plan "on; path=/validate_service_plan; order=1; ";
88+
location /trace {
8689
87-
access_by_lua "
90+
log_by_lua '
8891
ngx.apiGateway.tracking.track()
89-
ngx.apiGateway.validation.validateRequest()
90-
";
91-
content_by_lua 'ngx.say(ngx.var.validate_request_response_time)';
92+
';
93+
content_by_lua 'ngx.say("OK")';
9294
9395
}
9496
--- timeout: 10
@@ -100,20 +102,20 @@ __DATA__
100102
"format": "$publisher_org_name",
101103
"expire_at_utc": 1583910454,
102104
"action" : "TRACE",
103-
"meta" : ["$request_uri"]
105+
"meta" : "$request_uri; $request_method;$publisher_org_name"
104106
}]
105107
',
106108
"GET /trace"
107109
]
108110
--- response_body_like eval
109111
[
110112
'\{"result":"success"\}.*',
111-
'0',
112-
'(\d{2,4})+',
113-
'(\d{2,4})+',
114-
'.*{"domain":"pub1","format":"\$publisher_org_name","id":222,"action":"TRACE","expire_at_utc":1583910454}.*'
113+
'OK'
115114
]
116115
--- error_code_like eval
117-
[200, 200, 200, 200, 200]
116+
[200, 200]
117+
--- grep_error_log eval: qr/Received .*?/
118+
--- grep_error_log_out
119+
request_uri
118120
--- no_error_log
119121
[error]

0 commit comments

Comments
 (0)