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) new metrics and more flexible configuration #2400

Merged
merged 1 commit into from
Jun 22, 2017

Conversation

shashiranjan84
Copy link
Contributor

@shashiranjan84 shashiranjan84 commented Apr 17, 2017

Summary

  • Now each metric supports configurable stat type,
    sample rate and customer identifier if applicable.
  • Custom prefix for stats's name
  • New metrics upstream_latency, kong_latency and status_count_per_user.

Full changelog

  • Schema updated to support new metrics
  • Schema updated to support stat_type, sample_rate and consumer_identifier
  • Added new schema checks and test

Issues resolved

FIX #1533

NOTE: depends on the PR on #2367

@p0pr0ck5
Copy link
Contributor

Failed tests seem related to these changes?

@shashiranjan84
Copy link
Contributor Author

@p0pr0ck5 test will fail because this PR depends on PR on #2367. So basically next needs to be synced with master

local function allow_user_metric(message, identifier)
if message.consumer ~= nil and
message.consumer[identifier] ~= nil then
return true, message.consumer[identifier]
Copy link
Member

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

Copy link
Contributor Author

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.

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
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same reason.

end
else
local gauge = gauges[metric_config.name]
if gauge ~= nil then
Copy link
Member

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
Copy link
Member

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
}
}
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Contributor Author

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.

end
end
return false
end
Copy link
Member

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
  ...

Copy link
Contributor Author

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.

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."
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@Tieske Tieske added pr/changes requested Changes were requested to this PR by a maintainer. Please address them and ping back once done. and removed pr/status/please review labels May 1, 2017
@shashiranjan84 shashiranjan84 force-pushed the feat/configurable-statsd branch from 0a101ef to 2429c8d Compare May 1, 2017 17:27
@shashiranjan84 shashiranjan84 added pr/status/please review and removed pr/changes requested Changes were requested to this PR by a maintainer. Please address them and ping back once done. labels May 1, 2017
@thibaultcha thibaultcha added this to the 0.11.0 milestone May 19, 2017
@shashiranjan84 shashiranjan84 force-pushed the feat/configurable-statsd branch from 2429c8d to dc19e59 Compare June 1, 2017 23:15
@shashiranjan84 shashiranjan84 force-pushed the feat/configurable-statsd branch from dc19e59 to 9733c5b Compare June 5, 2017 22:08
@shashiranjan84 shashiranjan84 changed the title feat(plugins/statsd) new metrics and more flexible configuration feat(statsd) new metrics and more flexible configuration Jun 5, 2017
@shashiranjan84 shashiranjan84 force-pushed the feat/configurable-statsd branch 2 times, most recently from 784ecb0 to dd461f6 Compare June 6, 2017 17:43
@shashiranjan84 shashiranjan84 force-pushed the feat/configurable-statsd branch 2 times, most recently from fed5858 to df28326 Compare June 6, 2017 21:56
config = {
host = statsd.config.host,
port = statsd.config.port,
metrics = default_metrics,
Copy link
Contributor

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
Copy link
Contributor

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

@shashiranjan84 shashiranjan84 force-pushed the feat/configurable-statsd branch 3 times, most recently from 1e2ae3d to 20205e8 Compare June 13, 2017 01:37
@p0pr0ck5
Copy link
Contributor

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)?

@p0pr0ck5 p0pr0ck5 added pr/changes requested Changes were requested to this PR by a maintainer. Please address them and ping back once done. and removed pr/status/please review labels Jun 21, 2017
@shashiranjan84 shashiranjan84 force-pushed the feat/configurable-statsd branch from 20205e8 to c435460 Compare June 21, 2017 23:02
@shashiranjan84
Copy link
Contributor Author

@p0pr0ck5 Conflict resolved.

Migration removing the old config and adding it back with new changes.

@shashiranjan84 shashiranjan84 force-pushed the feat/configurable-statsd branch from c435460 to 5d0d5be Compare June 21, 2017 23:14
@shashiranjan84 shashiranjan84 added pr/status/please review and removed pr/changes requested Changes were requested to this PR by a maintainer. Please address them and ping back once done. labels Jun 21, 2017
Tieske
Tieske previously requested changes Jun 22, 2017
Copy link
Member

@Tieske Tieske left a 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)

local ngx_timer_at = ngx.timer.at
local string_gsub = string.gsub
local pairs = pairs
local format = string.format
Copy link
Member

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,
Copy link
Member

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)

and not entry.consumer_identifier then

return false, "consumer_identifier must be defined for metric "
.. entry.name
Copy link
Member

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

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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: same

prefix =
{
type = "string",
default = "kong"
Copy link
Member

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


Copy link
Member

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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)
Copy link
Member

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.

@shashiranjan84 shashiranjan84 force-pushed the feat/configurable-statsd branch 2 times, most recently from 7854286 to f9b7234 Compare June 22, 2017 19:07
@shashiranjan84
Copy link
Contributor Author

@p0pr0ck5 @Tieske done.

p0pr0ck5
p0pr0ck5 previously approved these changes Jun 22, 2017
Copy link
Contributor

@p0pr0ck5 p0pr0ck5 left a 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.

local string_gsub = string.gsub
local pairs = pairs
local string_format = string.format
local NGX_ERR = ngx.ERR
Copy link
Contributor

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)
Copy link
Contributor

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
@shashiranjan84 shashiranjan84 dismissed Tieske’s stale review June 22, 2017 23:07

Applied all his advised chances with few exception

@shashiranjan84 shashiranjan84 added pr/ready This PR is considered ready and can be merged at anytime (given it received no subsequent changes) and removed pr/status/please review labels Jun 22, 2017
@shashiranjan84 shashiranjan84 merged commit e635a34 into next Jun 22, 2017
@Tieske Tieske deleted the feat/configurable-statsd branch June 23, 2017 08:45
@Tieske
Copy link
Member

Tieske commented Jun 23, 2017

yay, finally done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/ready This PR is considered ready and can be merged at anytime (given it received no subsequent changes)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants