1313 ]]
1414
1515local cjson = require " cjson"
16- local _M = {}
16+ local su = require " api-gateway.tracking.util.stringutil "
1717
18- local kinesisLogger
18+ local _M = {}
1919
2020function _M :new (o )
2121 local o = o or {}
@@ -24,45 +24,30 @@ function _M:new(o)
2424 return o
2525end
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
6651end
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
0 commit comments