-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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) new metrics and more flexible configuration #2400
Conversation
Failed tests seem related to these changes? |
5a43c1b
to
0a101ef
Compare
kong/plugins/statsd/handler.lua
Outdated
local function allow_user_metric(message, identifier) | ||
if message.consumer ~= nil and | ||
message.consumer[identifier] ~= nil then | ||
return true, message.consumer[identifier] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need the nil
comparisons? can either of those values ever be false
?
eg.
if message.consumer and message.consumer[identifier] then
return true, message.consumer[identifier]
or even
local empty = {}
local function allow_user_metric(message, identifier)
local user_metric = (message.consumer or empty)[identifier]
return user_metric ~= nil, user_metric
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its just for readability, I have been told before to keep it explicit to make it more readable.
kong/plugins/statsd/handler.lua
Outdated
local stat_name = stat_name[metric_config.name] | ||
local stat_value = stat_value[metric_config.name] | ||
local gauge = gauges[metric_config.stat_type] | ||
if stat_name ~= nil and gauge ~= nil and stat_value ~= nil then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if stat_name and gauge and stat_value then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same reason.
kong/plugins/statsd/handler.lua
Outdated
end | ||
else | ||
local gauge = gauges[metric_config.name] | ||
if gauge ~= nil then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if gauge then
|
||
for _, plugin in ipairs(plugins) do | ||
local new_metrics = {} | ||
if plugin.config.metrics ~=nil then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
~=nil
is missing a space, and do we need the nil
?
end | ||
end | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the logic for this migration different from the Cassandra one? the migrations runs through the dao
so shouldn't it be identical?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
End result is still same, its just adding a new row and deleting the old one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but then for maintainability, let's use the same logic everywhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took hint from another migrations from mashape-analytics
.
kong/plugins/statsd/schema.lua
Outdated
end | ||
end | ||
return false | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use functions like this. Make a hash table and look it up, eg.
local consumer_identifiers = {
["consumer_id"] = true,
["custom_id"] = true,
["username"] = true,
}
if consumer_identifiers[somevalue] then
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I ll update the logic, though it will hardly make any difference as consumer_identifiers
is a fixed length table.
kong/plugins/statsd/schema.lua
Outdated
return false, "unique_users metric only works with stat_type 'set'" | ||
end | ||
if (entry.stat_type == "counter" or entry.stat_type == "gauge") and ((entry.sample_rate == nil) or (entry.sample_rate ~= nil and type(entry.sample_rate) ~= "number") or (entry.sample_rate ~= nil and entry.sample_rate < 1)) then | ||
return false, "sample rate must be defined for counters and gauges." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you break those lines up for readability?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
0a101ef
to
2429c8d
Compare
2429c8d
to
dc19e59
Compare
dc19e59
to
9733c5b
Compare
784ecb0
to
dd461f6
Compare
fed5858
to
df28326
Compare
config = { | ||
host = statsd.config.host, | ||
port = statsd.config.port, | ||
metrics = default_metrics, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so this replaces the existing metrics with default values, is there any way to carry over plugin configs as part of this migration? or is that impossible?
|
||
-- Delete the old one to avoid conflicts when inserting the new one | ||
local _, err = dao.plugins:delete(statsd) | ||
if err then return err end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prefer avoiding one-line conditionals, we will be removing these in #2586
1e2ae3d
to
20205e8
Compare
Looks like there's a merge conflict here. Also, based on reading the migrations, it looks like we eliminate all old configurations. Is this desirable (or am I misreading)? |
20205e8
to
c435460
Compare
@p0pr0ck5 Conflict resolved. Migration removing the old config and adding it back with new changes. |
c435460
to
5d0d5be
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shashiranjan84 sorry for some stuff that I reviewed before, but it's been a while... (I personally hate it when new stuff comes up in the second review)
kong/plugins/statsd/handler.lua
Outdated
local ngx_timer_at = ngx.timer.at | ||
local string_gsub = string.gsub | ||
local pairs = pairs | ||
local format = string.format |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: for consistency use string_format = string.format
["request_per_user"] = true, | ||
["upstream_latency"] = true, | ||
["kong_latency"] = true, | ||
["status_count_per_user"] = true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi: when the key
is a valid Lua identifier name, you can omit the quotes and brackets:
local metrics = {
request_count = true,
latency = true,
would do the job
(no need to change)
kong/plugins/statsd/schema.lua
Outdated
and not entry.consumer_identifier then | ||
|
||
return false, "consumer_identifier must be defined for metric " | ||
.. entry.name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style, put the ..
at the end of the previous line
kong/plugins/statsd/schema.lua
Outdated
and not consumer_identifiers[entry.consumer_identifier] then | ||
|
||
return false, "invalid consumer_identifier for metric '" .. entry.name | ||
.. "'. Choices are consumer_id, custom_id, and username" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: same
kong/plugins/statsd/schema.lua
Outdated
prefix = | ||
{ | ||
type = "string", | ||
default = "kong" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: trailing comma
return | ||
end | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
closing a socket always succeeds, no need for error checking here
function statsd_mt:close_socket()
return self.socket:close()
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmmm, no, not true:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm right, I stand corrected 😄
local metrics_input = {} | ||
local ok, err = validate_entity({ metrics = metrics_input}, statsd_schema) | ||
assert.is_nil(err) | ||
assert.True(ok) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: iirc the preferred style is to use assert.is_true
as opposed to the capitalized version.
7854286
to
f9b7234
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few minor things to clean up, nothing that needs another round of review.
kong/plugins/statsd/handler.lua
Outdated
local string_gsub = string.gsub | ||
local pairs = pairs | ||
local string_format = string.format | ||
local NGX_ERR = ngx.ERR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we align these whitespaces
return { | ||
{ | ||
name = "2017-06-09-160000_statsd_schema_changes", | ||
up = function(_, _, dao) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add an empty 'down' function to these
- Now each metric supports configurable stat type, sample rate and customer identifier if applicable. - New metrics `upstream_latency`, `kong_latency` and `status_count_per_user`. - Custom prefix for stat's name - Code style refomatted
f9b7234
to
b621c7f
Compare
Applied all his advised chances with few exception
yay, finally done! |
Summary
sample rate and customer identifier if applicable.
upstream_latency
,kong_latency
andstatus_count_per_user
.Full changelog
stat_type
,sample_rate
andconsumer_identifier
Issues resolved
FIX #1533
NOTE: depends on the PR on #2367