Skip to content

Commit

Permalink
fix(datadog) skip non-matched apis
Browse files Browse the repository at this point in the history
Fix runtime error when the datadog plugin is global and there is
not a catch all api present
  • Loading branch information
Kevin committed Aug 6, 2017
1 parent c4f5f21 commit 3a56283
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions kong/plugins/datadog/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ end

function DatadogHandler:log(conf)
DatadogHandler.super.log(self)

-- unmatched apis are nil
if not ngx.ctx.api then
return
end

local message = basic_serializer.serialize(ngx)

local ok, err = ngx_timer_at(0, log, conf, message)
Expand Down
43 changes: 43 additions & 0 deletions spec/03-plugins/08-datadog/01-log_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local helpers = require "spec.helpers"
local threads = require "llthreads2.ex"
local pl_file = require "pl.file"

describe("Plugin: datadog (log)", function()
local client
Expand Down Expand Up @@ -53,6 +54,19 @@ describe("Plugin: datadog (log)", function()
}
}
})
assert(helpers.dao.plugins:insert {
name = "datadog",
config = {
host = "127.0.0.1",
port = 9999,
metrics = "request_count,status_count,latency",
tags = {
request_count = {"T1:V1"},
status_count = {"T2:V2,T3:V3,T4"},
latency = {"T2:V2:V3,T4"},
}
}
})

assert(helpers.start_kong())
client = helpers.proxy_client()
Expand Down Expand Up @@ -168,4 +182,33 @@ describe("Plugin: datadog (log)", function()
assert.contains("kong.datadog3_com.request.status.200:1|c|#T2:V2,T3:V3,T4", gauges)
assert.contains("kong.datadog3_com.latency:%d+|g|#T2:V2:V3,T4", gauges, true)
end)

it("should not return a runtime error (regression)", function()
local thread = threads.new({
function()
local socket = require "socket"
local server = assert(socket.udp())
server:settimeout(1)
server:setoption("reuseaddr", true)
server:setsockname("127.0.0.1", 9999)
local gauge = server:receive()
server:close()
return gauge
end
})
thread:start()

local res = assert(client:send {
method = "GET",
path = "/NonMatch",
headers = {
["Host"] = "fakedns.com"
}
})

assert.res_status(404, res)

local err_log = pl_file.read(helpers.test_conf.nginx_err_logs)
assert.not_matches("attempt to index field 'api' (a nil value)", err_log, nil, true)
end)
end)

0 comments on commit 3a56283

Please sign in to comment.