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) merge statsd-advanced into statsd #9046

Merged
merged 1 commit into from
Aug 19, 2022
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
1 change: 1 addition & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,4 @@ jobs:

eval $(perl -I $HOME/perl5/lib/perl5/ -Mlocal::lib)
.ci/run_tests.sh

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@
ignore the `domains` field.
- Plugins with colliding priorities have now deterministic sorting based on their name
[#8957](https://github.com/Kong/kong/pull/8957)
- **Statsd**:
- The metric name that is related to the service has been renamed by adding a `service.` prefix. e.g. `kong.service.<service_identifier>.request.count` [#9046](https://github.com/Kong/kong/pull/9046)
- The metric `kong.<service_identifier>.request.status.<status>` and `kong.<service_identifier>.user.<consumer_identifier>.request.status.<status>` has been renamed to `kong.service.<service_identifier>.status.<status>` and `kong.service.<service_identifier>.user.<consumer_identifier>.status.<status>` [#9046](https://github.com/Kong/kong/pull/9046)
- The metric `*.status.<status>.total` from metrics `status_count` and `status_count_per_user` has been removed [#9046](https://github.com/Kong/kong/pull/9046)

### Deprecations

Expand Down Expand Up @@ -348,6 +352,8 @@
[#8900](https://github.com/Kong/kong/pull/8900)
- Sync all plugin versions to the Kong version
[#8772](https://github.com/Kong/kong/pull/8772)
- **Statsd**: :fireworks: **Newly open-sourced plugin capabilities**: All capabilities of [Statsd Advanced](https://docs.konghq.com/hub/kong-inc/statsd-advanced/) are now bundled in [Statsd](https://docs.konghq.com/hub/kong-inc/statsd).
Copy link
Contributor

Choose a reason for hiding this comment

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

remove these lines, no configuration changes were made

[#9046](https://github.com/Kong/kong/pull/9046)

#### Configuration

Expand Down
2 changes: 2 additions & 0 deletions kong-3.0.0-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@ build = {
["kong.plugins.datadog.schema"] = "kong/plugins/datadog/schema.lua",
["kong.plugins.datadog.statsd_logger"] = "kong/plugins/datadog/statsd_logger.lua",

["kong.plugins.statsd.constants"] = "kong/plugins/statsd/constants.lua",
["kong.plugins.statsd.handler"] = "kong/plugins/statsd/handler.lua",
["kong.plugins.statsd.log"] = "kong/plugins/statsd/log.lua",
["kong.plugins.statsd.schema"] = "kong/plugins/statsd/schema.lua",
["kong.plugins.statsd.statsd_logger"] = "kong/plugins/statsd/statsd_logger.lua",

Expand Down
9 changes: 9 additions & 0 deletions kong/plugins/statsd/constants.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Common constants
local constants = {
-- Lua style pattern, used in schema validation
REGEX_STATUS_CODE_RANGE = [[^[0-9]+-[0-9]+$]],
-- PCRE pattern, used in log_handler.lua
REGEX_SPLIT_STATUS_CODES_BY_DASH = [[(\d\d\d)-(\d\d\d)]],
}

return constants
138 changes: 2 additions & 136 deletions kong/plugins/statsd/handler.lua
Original file line number Diff line number Diff line change
@@ -1,149 +1,15 @@
local statsd_logger = require "kong.plugins.statsd.statsd_logger"
local log = require "kong.plugins.statsd.log"
local kong_meta = require "kong.meta"


local kong = kong
local ngx = ngx
local timer_at = ngx.timer.at
local pairs = pairs
local gsub = string.gsub
local fmt = string.format


local get_consumer_id = {
consumer_id = function(consumer)
return consumer and gsub(consumer.id, "-", "_")
end,
custom_id = function(consumer)
return consumer and consumer.custom_id
end,
username = function(consumer)
return consumer and consumer.username
end
}


local metrics = {
status_count = function (service_name, message, metric_config, logger)
local response_status = message.response and message.response.status or 0
local format = fmt("%s.request.status", service_name,
response_status)

logger:send_statsd(fmt("%s.%s", format, response_status),
1, logger.stat_types.counter, metric_config.sample_rate)

logger:send_statsd(fmt("%s.%s", format, "total"), 1,
logger.stat_types.counter, metric_config.sample_rate)
end,
unique_users = function (service_name, message, metric_config, logger)
local get_consumer_id = get_consumer_id[metric_config.consumer_identifier]
local consumer_id = get_consumer_id(message.consumer)

if consumer_id then
local stat = fmt("%s.user.uniques", service_name)

logger:send_statsd(stat, consumer_id, logger.stat_types.set)
end
end,
request_per_user = function (service_name, message, metric_config, logger)
local get_consumer_id = get_consumer_id[metric_config.consumer_identifier]
local consumer_id = get_consumer_id(message.consumer)

if consumer_id then
local stat = fmt("%s.user.%s.request.count", service_name, consumer_id)

logger:send_statsd(stat, 1, logger.stat_types.counter,
metric_config.sample_rate)
end
end,
status_count_per_user = function (service_name, message, metric_config, logger)
local get_consumer_id = get_consumer_id[metric_config.consumer_identifier]
local consumer_id = get_consumer_id(message.consumer)

if consumer_id then
local format = fmt("%s.user.%s.request.status", service_name, consumer_id)

logger:send_statsd(fmt("%s.%s", format, message.response.status),
1, logger.stat_types.counter,
metric_config.sample_rate)

logger:send_statsd(fmt("%s.%s", format, "total"),
1, logger.stat_types.counter,
metric_config.sample_rate)
end
end,
}


local function log(premature, conf, message)
if premature then
return
end

local name = gsub(message.service.name ~= ngx.null and
message.service.name or message.service.host,
"%.", "_")

local stat_name = {
request_size = name .. ".request.size",
response_size = name .. ".response.size",
latency = name .. ".latency",
upstream_latency = name .. ".upstream_latency",
kong_latency = name .. ".kong_latency",
request_count = name .. ".request.count",
}
local stat_value = {
request_size = message.request and message.request.size,
response_size = message.response and message.response.size,
latency = message.latencies.request,
upstream_latency = message.latencies.proxy,
kong_latency = message.latencies.kong,
request_count = 1,
}

local logger, err = statsd_logger:new(conf)
if err then
kong.log.err("failed to create Statsd logger: ", err)
return
end

for _, metric_config in pairs(conf.metrics) do
local metric = metrics[metric_config.name]

if metric then
metric(name, message, metric_config, logger)

else
local stat_name = stat_name[metric_config.name]
local stat_value = stat_value[metric_config.name]

logger:send_statsd(stat_name, stat_value,
logger.stat_types[metric_config.stat_type],
metric_config.sample_rate)
end
end

logger:close_socket()
end


local StatsdHandler = {
PRIORITY = 11,
VERSION = kong_meta.version,
}


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

local message = kong.log.serialize()

local ok, err = timer_at(0, log, conf, message)
if not ok then
kong.log.err("failed to create timer: ", err)
end
log.execute(conf)
end


Expand Down
Loading