Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin(datadog): fix a bug that datalog plugin is not triggered for serviceless routes #12068

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "**Datadog**: Fix a bug that datadog plugin is not triggered for serviceless routes. In this fix, datadog plugin is always triggered, and the value of tag `name`(service_name) is set as an empty value."
type: bugfix
scope: Plugin
15 changes: 4 additions & 11 deletions kong/plugins/datadog/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ local function send_entries_to_datadog(conf, messages)
end

for _, message in ipairs(messages) do
local name = gsub(message.service.name ~= null and
message.service.name or message.service.host,
"%.", "_")

local stat_name = {
request_size = "request.size",
response_size = "response.size",
Expand Down Expand Up @@ -84,8 +80,10 @@ local function send_entries_to_datadog(conf, messages)
local get_consumer_id = get_consumer_id[metric_config.consumer_identifier]
local consumer_id = get_consumer_id and get_consumer_id(message.consumer) or nil
local tags = compose_tags(
name, message.response and message.response.status or "-",
consumer_id, metric_config.tags, conf)
message.service and gsub(message.service.name ~= null and
message.service.name or message.service.host, "%.", "_") or "",
message.response and message.response.status or "-",
consumer_id, metric_config.tags, conf)

logger:send_statsd(stat_name, stat_value,
logger.stat_types[metric_config.stat_type],
Expand All @@ -104,12 +102,7 @@ local DatadogHandler = {
VERSION = kong_meta.version,
}


function DatadogHandler:log(conf)
if not ngx.ctx.service then
return
end

local ok, err = Queue.enqueue(
Queue.get_plugin_params("datadog", conf),
send_entries_to_datadog,
Expand Down
95 changes: 76 additions & 19 deletions spec/03-plugins/08-datadog/01-log_spec.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
local helpers = require "spec.helpers"
local cjson = require "cjson"
local stringx = require "pl.stringx"


describe("Plugin: datadog (log)", function()
local DEFAULT_METRICS_COUNT = 6

lazy_setup(function()
helpers.setenv('KONG_DATADOG_AGENT_HOST', 'localhost')
Expand Down Expand Up @@ -93,6 +95,11 @@ describe("Plugin: datadog (log)", function()
}
}

local route9 = bp.routes:insert {
paths = { "/serviceless" },
no_service = true,
}

bp.plugins:insert {
name = "key-auth",
route = { id = route1.id },
Expand Down Expand Up @@ -237,6 +244,25 @@ describe("Plugin: datadog (log)", function()
},
}

bp.plugins:insert {
name = "datadog",
route = { id = route9.id },
config = {
host = "127.0.0.1",
port = 9999,
queue_size = 2,
},
}

bp.plugins:insert {
name = "request-termination",
route = { id = route9.id },
config = {
status_code = 200,
message = "OK",
}
}

assert(helpers.start_kong({
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
Expand All @@ -245,17 +271,23 @@ describe("Plugin: datadog (log)", function()

proxy_client = helpers.proxy_client()
end)

lazy_teardown(function()
helpers.stop_kong()
end)

before_each(function()
proxy_client = helpers.proxy_client()
end)

after_each(function()
if proxy_client then
proxy_client:close()
end


helpers.stop_kong()
end)

it("logs metrics over UDP", function()
local thread = helpers.udp_server(9999, 6)
local thread = helpers.udp_server(9999, DEFAULT_METRICS_COUNT)

local res = assert(proxy_client:send {
method = "GET",
Expand All @@ -268,7 +300,7 @@ describe("Plugin: datadog (log)", function()

local ok, gauges = thread:join()
assert.True(ok)
assert.equal(6, #gauges)
assert.equal(DEFAULT_METRICS_COUNT, #gauges)
assert.contains("kong.request.count:1|c|#name:dd1,status:200,consumer:bar,app:kong" , gauges)
assert.contains("kong.latency:%d+|ms|#name:dd1,status:200,consumer:bar,app:kong", gauges, true)
assert.contains("kong.request.size:%d+|ms|#name:dd1,status:200,consumer:bar,app:kong", gauges, true)
Expand All @@ -278,7 +310,7 @@ describe("Plugin: datadog (log)", function()
end)

it("logs metrics over UDP #grpc", function()
local thread = helpers.udp_server(9999, 6)
local thread = helpers.udp_server(9999, DEFAULT_METRICS_COUNT)

local grpc_cleint = assert(helpers.proxy_client_grpc())

Expand All @@ -293,7 +325,7 @@ describe("Plugin: datadog (log)", function()

local ok, gauges = thread:join()
assert.True(ok)
assert.equal(6, #gauges)
assert.equal(DEFAULT_METRICS_COUNT, #gauges)
assert.contains("kong.request.count:1|c|#name:grpc,status:200,consumer:bar,app:kong" , gauges)
assert.contains("kong.latency:%d+|ms|#name:grpc,status:200,consumer:bar,app:kong", gauges, true)
assert.contains("kong.request.size:%d+|ms|#name:grpc,status:200,consumer:bar,app:kong", gauges, true)
Expand All @@ -303,7 +335,7 @@ describe("Plugin: datadog (log)", function()
end)

it("logs metrics over UDP with custom prefix", function()
local thread = helpers.udp_server(9999, 6)
local thread = helpers.udp_server(9999, DEFAULT_METRICS_COUNT)

local res = assert(proxy_client:send {
method = "GET",
Expand All @@ -316,7 +348,7 @@ describe("Plugin: datadog (log)", function()

local ok, gauges = thread:join()
assert.True(ok)
assert.equal(6, #gauges)
assert.equal(DEFAULT_METRICS_COUNT, #gauges)
assert.contains("prefix.request.count:1|c|#name:dd4,status:200,consumer:bar,app:kong",gauges)
assert.contains("prefix.latency:%d+|ms|#name:dd4,status:200,consumer:bar,app:kong", gauges, true)
assert.contains("prefix.request.size:%d+|ms|#name:dd4,status:200,consumer:bar,app:kong", gauges, true)
Expand All @@ -326,7 +358,7 @@ describe("Plugin: datadog (log)", function()
end)

it("logs metrics over UDP with custom tag names", function()
local thread = helpers.udp_server(9999, 6)
local thread = helpers.udp_server(9999, DEFAULT_METRICS_COUNT)

local res = assert(proxy_client:send {
method = "GET",
Expand All @@ -339,7 +371,7 @@ describe("Plugin: datadog (log)", function()

local ok, gauges = thread:join()
assert.True(ok)
assert.equal(6, #gauges)
assert.equal(DEFAULT_METRICS_COUNT, #gauges)
assert.contains("kong.request.count:1|c|#upstream:dd6,http_status:200,user:bar,app:kong",gauges)
assert.contains("kong.latency:%d+|ms|#upstream:dd6,http_status:200,user:bar,app:kong", gauges, true)
assert.contains("kong.request.size:%d+|ms|#upstream:dd6,http_status:200,user:bar,app:kong", gauges, true)
Expand Down Expand Up @@ -387,7 +419,7 @@ describe("Plugin: datadog (log)", function()
end)

it("logs metrics to host/port defined via environment variables", function()
local thread = helpers.udp_server(9999, 6)
local thread = helpers.udp_server(9999, DEFAULT_METRICS_COUNT)

local res = assert(proxy_client:send {
method = "GET",
Expand All @@ -400,7 +432,7 @@ describe("Plugin: datadog (log)", function()

local ok, gauges = thread:join()
assert.True(ok)
assert.equal(6, #gauges)
assert.equal(DEFAULT_METRICS_COUNT, #gauges)
assert.contains("kong.request.count:1|c|#name:dd5,status:200,consumer:bar,app:kong" , gauges)
assert.contains("kong.latency:%d+|ms|#name:dd5,status:200,consumer:bar,app:kong", gauges, true)
assert.contains("kong.request.size:%d+|ms|#name:dd5,status:200,consumer:bar,app:kong", gauges, true)
Expand All @@ -410,7 +442,7 @@ describe("Plugin: datadog (log)", function()
end)

it("logs metrics in several batches", function()
local thread = helpers.udp_server(9999, 6)
local thread = helpers.udp_server(9999, DEFAULT_METRICS_COUNT)

local res = assert(proxy_client:send {
method = "GET",
Expand All @@ -423,7 +455,7 @@ describe("Plugin: datadog (log)", function()

local ok, gauges = thread:join()
assert.True(ok)
assert.equal(6, #gauges)
assert.equal(DEFAULT_METRICS_COUNT, #gauges)
assert.contains("kong.request.count:1|c|#name:dd7,status:200,consumer:bar,app:kong" , gauges)
assert.contains("kong.latency:%d+|ms|#name:dd7,status:200,consumer:bar,app:kong", gauges, true)
assert.contains("kong.request.size:%d+|ms|#name:dd7,status:200,consumer:bar,app:kong", gauges, true)
Expand All @@ -448,7 +480,7 @@ describe("Plugin: datadog (log)", function()

local ok, gauges = thread:join()
assert.True(ok)
assert.equal(6, #gauges)
assert.equal(DEFAULT_METRICS_COUNT, #gauges)
end)

it("should not return a runtime error (regression)", function()
Expand Down Expand Up @@ -476,9 +508,9 @@ describe("Plugin: datadog (log)", function()

thread:join()
end)

it("referenceable fields works", function()
local thread = helpers.udp_server(9999, 6, 6)
local thread = helpers.udp_server(9999, DEFAULT_METRICS_COUNT, 6)
local another_proxy_client = helpers.proxy_client()

local res = assert(another_proxy_client:send {
Expand All @@ -493,7 +525,32 @@ describe("Plugin: datadog (log)", function()

local ok, gauges = thread:join()
assert.True(ok)
assert.equal(6, #gauges)
assert.equal(DEFAULT_METRICS_COUNT, #gauges)
end)

it("datadog plugin is triggered for serviceless routes", function()
local thread = helpers.udp_server(9999, DEFAULT_METRICS_COUNT)
local res = assert(proxy_client:send {
method = "GET",
path = "/serviceless",
})

local body = assert.res_status(200, res)
assert.equals(body, '{"message":"OK"}')

local ok, gauges = thread:join()
assert.True(ok)
assert.equals(DEFAULT_METRICS_COUNT, #gauges)

for _, g in ipairs(gauges) do
-- tags start with `#`
local tmp = stringx.split(g, '#')
local tag_idx = #tmp
assert(tag_idx == 2, "Error: missing tags")
local tags = tmp[tag_idx]
assert(tags, "Error: missing tags")
assert(string.match(tags, "name:,"), "Error: the value of `name` must be an empty string for serviceless routes")
end
end)
end)
end
Expand Down
Loading