Skip to content

Commit 3651424

Browse files
committed
chore(datadog) use kong pdk
### Summary Change `datadog` plugin to use Kong PDK.
1 parent a9f8a72 commit 3651424

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

kong/plugins/datadog/handler.lua

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
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"
33

44

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
1511

1612

1713
local get_consumer_id = {
1814
consumer_id = function(consumer)
19-
return consumer and string_gsub(consumer.id, "-", "_")
15+
return consumer and gsub(consumer.id, "-", "_")
2016
end,
21-
custom_id = function(consumer)
17+
custom_id = function(consumer)
2218
return consumer and consumer.custom_id
2319
end,
24-
username = function(consumer)
20+
username = function(consumer)
2521
return consumer and consumer.username
2622
end
2723
}
@@ -30,11 +26,11 @@ local get_consumer_id = {
3026
local function compose_tags(service_name, status, consumer_id, tags)
3127
local result = {"name:" ..service_name, "status:"..status}
3228
if consumer_id ~= nil then
33-
table.insert(result, "consumer:" ..consumer_id)
29+
insert(result, "consumer:" ..consumer_id)
3430
end
3531
if tags ~= nil then
3632
for _, v in pairs(tags) do
37-
table.insert(result, v)
33+
insert(result, v)
3834
end
3935
end
4036
return result
@@ -46,9 +42,9 @@ local function log(premature, conf, message)
4642
return
4743
end
4844

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+
"%.", "_")
5248

5349
local stat_name = {
5450
request_size = "request.size",
@@ -69,7 +65,7 @@ local function log(premature, conf, message)
6965

7066
local logger, err = statsd_logger:new(conf)
7167
if err then
72-
ngx_log(NGX_ERR, "failed to create Statsd logger: ", err)
68+
kong.log.err("failed to create Statsd logger: ", err)
7369
return
7470
end
7571

@@ -90,17 +86,23 @@ local function log(premature, conf, message)
9086
end
9187

9288

89+
local DatadogHandler = {
90+
PRIORITY = 10,
91+
VERSION = "3.0.1",
92+
}
93+
94+
9395
function DatadogHandler:log(conf)
9496
if not ngx.ctx.service then
9597
return
9698
end
9799

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)
101102
if not ok then
102-
ngx_log(NGX_ERR, "failed to create timer: ", err)
103+
kong.log.err("failed to create timer: ", err)
103104
end
104105
end
105106

107+
106108
return DatadogHandler

kong/plugins/datadog/statsd_logger.lua

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
local ngx_socket_udp = ngx.socket.udp
2-
local ngx_log = ngx.log
3-
local table_concat = table.concat
4-
local setmetatable = setmetatable
5-
local NGX_ERR = ngx.ERR
6-
local NGX_DEBUG = ngx.DEBUG
7-
local fmt = string.format
8-
local tostring = tostring
1+
local kong = kong
2+
local udp = ngx.socket.udp
3+
local concat = table.concat
4+
local setmetatable = setmetatable
5+
local fmt = string.format
6+
local tostring = tostring
97

108

119
local stat_types = {
@@ -17,16 +15,17 @@ local stat_types = {
1715
set = "s",
1816
}
1917

18+
2019
local statsd_mt = {}
2120
statsd_mt.__index = statsd_mt
2221

2322

2423
function statsd_mt:new(conf)
25-
local sock = ngx_socket_udp()
24+
local sock = udp()
2625
local _, err = sock:setpeername(conf.host, conf.port)
2726
if err then
2827
return nil, fmt("failed to connect to %s:%s: %s", conf.host,
29-
tostring(conf.port), err)
28+
tostring(conf.port), err)
3029
end
3130

3231
local statsd = {
@@ -49,7 +48,7 @@ local function statsd_message(prefix, stat, delta, kind, sample_rate, tags)
4948
end
5049

5150
if tags and #tags > 0 then
52-
str_tags = "|#" .. table_concat(tags, ",")
51+
str_tags = "|#" .. concat(tags, ",")
5352
end
5453

5554
return fmt("%s.%s:%s|%s%s%s", prefix, stat,
@@ -60,8 +59,8 @@ end
6059
function statsd_mt:close_socket()
6160
local ok, err = self.socket:close()
6261
if not ok then
63-
ngx_log(NGX_ERR, "[udp-log] failed to close connection from ",
64-
self.host, ":", self.port, ": ", err)
62+
kong.log.err("failed to close connection from ", self.host, ":", self.port,
63+
": ", err)
6564
end
6665
end
6766

@@ -70,12 +69,12 @@ function statsd_mt:send_statsd(stat, delta, kind, sample_rate, tags)
7069
local udp_message = statsd_message(self.prefix or "kong", stat,
7170
delta, kind, sample_rate, tags)
7271

73-
ngx_log(NGX_DEBUG, fmt("Sending data to statsd server: %s", udp_message))
72+
kong.log.debug("Sending data to statsd server: ", udp_message)
7473

7574
local ok, err = self.socket:send(udp_message)
7675
if not ok then
77-
ngx_log(NGX_ERR, fmt("failed to send data to %s:%s: %s", self.host,
78-
tostring(self.port), err))
76+
kong.log.err("failed to send data to ", self.host, ":",
77+
tostring(self.port), ": ", err)
7978
end
8079
end
8180

0 commit comments

Comments
 (0)