Skip to content

Commit 9b34b89

Browse files
committed
feat(datadog): response_status_class tag to metrics
Having a separate tag for the HTTP response code class allows useful grouping in Datadog by e.g. successful requests (2xx), client errors (4xx), and server errors (5xx). The existing `response_status` already essentially includes that information, but because of how Datadog works, one would need to list out each possible value between 400 and 431 to group all client errors, for example. Having a separate tag for the class greatly simplifies this. Generally it may be problematic to add new tags by default without an opt-in configuration option, because Datadog charges based on [custom metrics count][1] and additional tags can increase that count. However, the additional tag is safe to add here, because it is guaranteed not to increase the custom metric count. That is because the new tag is based on the already included `response_status` value, which is more granular than the new `response_status_class` value, so the number of unique tag combinations will not change. [1]: https://docs.datadoghq.com/metrics/custom_metrics/
1 parent da581f7 commit 9b34b89

File tree

3 files changed

+64
-61
lines changed

3 files changed

+64
-61
lines changed

apisix/plugins/datadog.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ local format = string.format
2525
local concat = table.concat
2626
local tostring = tostring
2727
local pairs = pairs
28+
local floor = math.floor
2829

2930
local plugin_name = "datadog"
3031
local defaults = {
@@ -118,6 +119,7 @@ local function generate_tag(entry, const_tags)
118119
end
119120
if entry.response.status then
120121
core.table.insert(tags, "response_status:" .. entry.response.status)
122+
core.table.insert(tags, "response_status_class:" .. floor(entry.response.status / 100) .. "xx")
121123
end
122124
if entry.scheme ~= "" then
123125
core.table.insert(tags, "scheme:" .. entry.scheme)

docs/en/latest/plugins/datadog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ The metrics will be sent to the DogStatsD agent with the following tags:
116116
- `service_name`: If a Route has been created with an abstracted Service, the Service name/ID based on the attribute `prefer_name`.
117117
- `consumer`: If the Route is linked to a Consumer, the username will be added as a tag.
118118
- `balancer_ip`: IP address of the Upstream balancer that processed the current request.
119-
- `response_status`: HTTP response status code.
119+
- `response_status`: HTTP response status code. E.g. "200", "404", "503".
120+
- `response_status_class`: HTTP response status code class. E.g. "2xx", "4xx", "5xx".
120121
- `scheme`: Request scheme such as HTTP, gRPC, and gRPCs.
121122
- `path`: The HTTP path pattern. Only available if the attribute `include_path` is set to true.
122123
- `method`: The HTTP method. Only available if the attribute `include_method` is set to true.

0 commit comments

Comments
 (0)