1
- local basic_serializer = require " kong.plugins.log-serializers.basic"
2
- local statsd_logger = require " kong.plugins.datadog.statsd_logger"
1
+ local serializer = require " kong.plugins.log-serializers.basic"
2
+ local statsd_logger = require " kong.plugins.datadog.statsd_logger"
3
3
4
4
5
- local ngx_log = ngx .log
6
- local ngx_timer_at = ngx .timer .at
7
- local string_gsub = string.gsub
8
- local pairs = pairs
9
- local NGX_ERR = ngx .ERR
10
-
11
-
12
- local DatadogHandler = {}
13
- DatadogHandler .PRIORITY = 10
14
- DatadogHandler .VERSION = " 3.0.0"
5
+ local kong = kong
6
+ local ngx = ngx
7
+ local timer_at = ngx .timer .at
8
+ local insert = table.insert
9
+ local gsub = string.gsub
10
+ local pairs = pairs
15
11
16
12
17
13
local get_consumer_id = {
18
14
consumer_id = function (consumer )
19
- return consumer and string_gsub (consumer .id , " -" , " _" )
15
+ return consumer and gsub (consumer .id , " -" , " _" )
20
16
end ,
21
- custom_id = function (consumer )
17
+ custom_id = function (consumer )
22
18
return consumer and consumer .custom_id
23
19
end ,
24
- username = function (consumer )
20
+ username = function (consumer )
25
21
return consumer and consumer .username
26
22
end
27
23
}
@@ -30,11 +26,11 @@ local get_consumer_id = {
30
26
local function compose_tags (service_name , status , consumer_id , tags )
31
27
local result = {" name:" .. service_name , " status:" .. status }
32
28
if consumer_id ~= nil then
33
- table. insert (result , " consumer:" .. consumer_id )
29
+ insert (result , " consumer:" .. consumer_id )
34
30
end
35
31
if tags ~= nil then
36
32
for _ , v in pairs (tags ) do
37
- table. insert (result , v )
33
+ insert (result , v )
38
34
end
39
35
end
40
36
return result
@@ -46,9 +42,9 @@ local function log(premature, conf, message)
46
42
return
47
43
end
48
44
49
- local name = string_gsub (message .service .name ~= ngx .null and
50
- message .service .name or message .service .host ,
51
- " %." , " _" )
45
+ local name = gsub (message .service .name ~= ngx .null and
46
+ message .service .name or message .service .host ,
47
+ " %." , " _" )
52
48
53
49
local stat_name = {
54
50
request_size = " request.size" ,
@@ -69,7 +65,7 @@ local function log(premature, conf, message)
69
65
70
66
local logger , err = statsd_logger :new (conf )
71
67
if err then
72
- ngx_log ( NGX_ERR , " failed to create Statsd logger: " , err )
68
+ kong . log . err ( " failed to create Statsd logger: " , err )
73
69
return
74
70
end
75
71
@@ -90,17 +86,23 @@ local function log(premature, conf, message)
90
86
end
91
87
92
88
89
+ local DatadogHandler = {
90
+ PRIORITY = 10 ,
91
+ VERSION = " 3.0.1" ,
92
+ }
93
+
94
+
93
95
function DatadogHandler :log (conf )
94
96
if not ngx .ctx .service then
95
97
return
96
98
end
97
99
98
- local message = basic_serializer .serialize (ngx )
99
-
100
- local ok , err = ngx_timer_at (0 , log , conf , message )
100
+ local message = serializer .serialize (ngx )
101
+ local ok , err = timer_at (0 , log , conf , message )
101
102
if not ok then
102
- ngx_log ( NGX_ERR , " failed to create timer: " , err )
103
+ kong . log . err ( " failed to create timer: " , err )
103
104
end
104
105
end
105
106
107
+
106
108
return DatadogHandler
0 commit comments