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

feat(statsd): allow using credential id as consumer id #12412

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@
Previously, the `header_type` was hardcoded to `preserve`, now it can be set to one of the
following values: `preserve`, `ignore`, `b3`, `b3-single`, `w3c`, `jaeger`, `ot`.
[#10620](https://github.com/Kong/kong/pull/10620)
- **StatsD**: allow using credential id as consumer id
[#12412](https://github.com/Kong/kong/pull/12412)

#### PDK

Expand Down
19 changes: 11 additions & 8 deletions kong/plugins/statsd/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,18 @@ local SHDICT_METRICS_SEND_THRESHOLD = 60


local get_consumer_id = {
consumer_id = function(consumer)
consumer_id = function(consumer, cred)
return consumer and consumer.id
end,
custom_id = function(consumer)
custom_id = function(consumer, cred)
return consumer and consumer.custom_id
end,
username = function(consumer)
username = function(consumer, cred)
return consumer and consumer.username
end
end,
credential = function(consumer, cred)
return cred and cred.id
end,
}

local get_service_id = {
Expand Down Expand Up @@ -143,7 +146,7 @@ local metrics = {
return
end
local get_consumer_id = get_consumer_id[metric_config.consumer_identifier or conf.consumer_identifier_default]
local consumer_id = get_consumer_id(message.consumer)
local consumer_id = get_consumer_id(message.consumer, message.authenticated_entity)

if consumer_id then
local stat = string_format("%s.user.uniques", scope_name)
Expand All @@ -156,7 +159,7 @@ local metrics = {
return
end
local get_consumer_id = get_consumer_id[metric_config.consumer_identifier or conf.consumer_identifier_default]
local consumer_id = get_consumer_id(message.consumer)
local consumer_id = get_consumer_id(message.consumer, message.authenticated_entity)

if consumer_id then
local stat = string_format("%s.user.%s.request.count", scope_name, consumer_id)
Expand All @@ -179,7 +182,7 @@ local metrics = {
return
end
local get_consumer_id = get_consumer_id[metric_config.consumer_identifier or conf.consumer_identifier_default]
local consumer_id = get_consumer_id(message.consumer)
local consumer_id = get_consumer_id(message.consumer, message.authenticated_entity)

if consumer_id then
logger:send_statsd(string_format("%s.user.%s.status.%s", scope_name,
Expand Down Expand Up @@ -210,7 +213,7 @@ local metrics = {
end

local get_consumer_id = get_consumer_id[metric_config.consumer_identifier or conf.consumer_identifier_default]
local consumer_id = get_consumer_id(message.consumer)
local consumer_id = get_consumer_id(message.consumer, message.authenticated_entity)
if not consumer_id then
return
end
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/statsd/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local STAT_TYPES = {


local CONSUMER_IDENTIFIERS = {
"consumer_id", "custom_id", "username",
"consumer_id", "custom_id", "username", "credential"
}

local SERVICE_IDENTIFIERS = {
Expand Down
38 changes: 37 additions & 1 deletion spec/03-plugins/06-statsd/01-log_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ for _, strategy in helpers.each_strategy() do
local proxy_client
local proxy_client_grpc
local shdict_count
local cred

lazy_setup(function()
local bp = helpers.get_db_utils(strategy, {
Expand All @@ -69,7 +70,7 @@ for _, strategy in helpers.each_strategy() do
custom_id = "robert",
}

bp.keyauth_credentials:insert {
cred = bp.keyauth_credentials:insert {
key = "kong",
consumer = { id = consumer.id },
}
Expand Down Expand Up @@ -685,6 +686,23 @@ for _, strategy in helpers.each_strategy() do
},
}

bp.key_auth_plugins:insert { route = { id = routes[39].id } }

bp.statsd_plugins:insert {
route = { id = routes[39].id },
config = {
host = "127.0.0.1",
port = UDP_PORT,
metrics = {
{
name = "unique_users",
stat_type = "set",
consumer_identifier = "credential",
}
},
},
}

for i = 100, 110 do
local service = bp.services:insert {
protocol = helpers.mock_upstream_protocol,
Expand Down Expand Up @@ -1452,6 +1470,24 @@ for _, strategy in helpers.each_strategy() do
assert.contains("^kong.service.statsd14.user.uniques:" .. uuid_pattern .. "|s", res, true)
end)

it("consumer by credential", function()
local metrics_count = expected_metrics_count(1)
local thread = helpers.udp_server(UDP_PORT, metrics_count, 2)
local response = assert(proxy_client:send {
method = "GET",
path = "/request?apikey=kong",
headers = {
host = "logging39.test"
}
})
assert.res_status(200, response)

local ok, res, err = thread:join()
assert(ok, res)
assert(res, err)
assert.contains(fmt("kong.service.statsd39.user.uniques:%s|s", cred.id), res)
end)

it("status_count_per_user_per_route", function()
local metrics_count = expected_metrics_count(1)
local thread = helpers.udp_server(UDP_PORT, metrics_count, 2)
Expand Down
2 changes: 1 addition & 1 deletion spec/03-plugins/06-statsd/02-schema_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ describe("Plugin: statsd (schema)", function()
workspace_identifier_default = "invalid type"
}, statsd_schema)
assert.not_nil(err)
assert.equal("expected one of: consumer_id, custom_id, username", err.config.consumer_identifier_default)
assert.equal("expected one of: consumer_id, custom_id, username, credential", err.config.consumer_identifier_default)
assert.equal("expected one of: service_id, service_name, service_host, service_name_or_host", err.config.service_identifier_default)
assert.equal("expected one of: workspace_id, workspace_name", err.config.workspace_identifier_default)
end)
Expand Down
Loading