Skip to content

Commit 216c7b6

Browse files
committed
feat(datadog): add route-level constant_tags support
`constant_tags` are already supported at the plugin configuration level. However, sometimes different values may be required for each route, but this was previously not possible. For example, if some routes are owned by team A and some routes by team B, they could add `constant_tags: ["owner:team_a"]` or `constant_tags: ["owner:team_b"]` to each route, and would then be able to group metrics by team on Datadog.
1 parent 79d9ed4 commit 216c7b6

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

apisix/plugins/datadog.lua

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ local schema = {
3939
properties = {
4040
prefer_name = {type = "boolean", default = true},
4141
include_path = {type = "boolean", default = false},
42-
include_method = {type = "boolean", default = false}
42+
include_method = {type = "boolean", default = false},
43+
constant_tags = {
44+
type = "array",
45+
items = {type = "string"},
46+
default = {}
47+
}
4348
}
4449
}
4550

@@ -82,6 +87,12 @@ local function generate_tag(entry, const_tags)
8287
tags = {}
8388
end
8489

90+
if entry.constant_tags and #entry.constant_tags > 0 then
91+
for _, tag in pairs(entry.constant_tags) do
92+
core.table.insert(tags, tag)
93+
end
94+
end
95+
8596
if entry.route_id and entry.route_id ~= "" then
8697
core.table.insert(tags, "route_name:" .. entry.route_id)
8798
end
@@ -261,6 +272,10 @@ function _M.log(conf, ctx)
261272
entry.method = ctx.var.method
262273
end
263274

275+
if conf.constant_tags and #conf.constant_tags > 0 then
276+
entry.constant_tags = core.table.clone(conf.constant_tags)
277+
end
278+
264279
if batch_processor_manager:add_entry(conf, entry) then
265280
return
266281
end

docs/en/latest/plugins/datadog.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ This Plugin provides the ability to push metrics as a batch to the external Data
4141

4242
## Attributes
4343

44-
| Name | Type | Required | Default | Valid values | Description |
45-
| -------------- | ------- | -------- | ------- | ------------ | -------------------------------------------------------------------------------------- |
46-
| prefer_name | boolean | False | true | [true,false] | When set to `false`, uses Route/Service ID instead of name (default) with metric tags. |
47-
| include_path | boolean | False | false | [true,false] | When set to `true`, includes the path pattern in metric tags. |
48-
| include_method | boolean | False | false | [true,false] | When set to `true`, includes the HTTP method in metric tags. |
44+
| Name | Type | Required | Default | Valid values | Description |
45+
| -------------- | ------- | -------- | ------- | ------------ | ---------------------------------------------------------------------------------------------------------------- |
46+
| prefer_name | boolean | False | true | [true,false] | When set to `false`, uses Route/Service ID instead of name (default) with metric tags. |
47+
| include_path | boolean | False | false | [true,false] | When set to `true`, includes the path pattern in metric tags. |
48+
| include_method | boolean | False | false | [true,false] | When set to `true`, includes the HTTP method in metric tags. |
49+
| constant_tags | array | False | [] | | Static tags to embed into all metrics generated by this route. Useful for grouping metrics over certain signals. |
4950

5051
This Plugin supports using batch processors to aggregate and process entries (logs/data) in a batch. This avoids the need for frequently submitting the data. The batch processor submits data every `5` seconds or when the data in the queue reaches `1000`. See [Batch Processor](../batch-processor.md#configuration) for more information or setting your custom configuration.
5152

t/plugin/datadog.t

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ message received: apisix\.egress\.size:[\d]+\|ms\|#source:apisix,route_name:data
538538
539539
540540
541-
=== TEST 11: testing behaviour with include_path and include_method
541+
=== TEST 11: testing behaviour with include_path, include_method, and route level constant_tags
542542
--- apisix_yaml
543543
routes:
544544
- uri: /articles/*/comments
@@ -552,6 +552,9 @@ routes:
552552
max_retry_count: 0
553553
include_path: true
554554
include_method: true
555+
constant_tags:
556+
- route_tag1:foo
557+
- route_tag2:bar
555558
proxy-rewrite:
556559
uri: /opentracing
557560
#END
@@ -563,10 +566,10 @@ opentracing
563566
--- grep_error_log eval
564567
qr/message received: apisix(.+?(?=, ))/
565568
--- grep_error_log_out eval
566-
qr/message received: apisix\.request\.counter:1\|c\|#source:apisix,route_name:datadog,path:\/articles\/\*\/comments,method:GET,balancer_ip:[\d.]+,response_status:200,scheme:http
567-
message received: apisix\.request\.latency:[\d.]+\|h\|#source:apisix,route_name:datadog,path:\/articles\/\*\/comments,method:GET,balancer_ip:[\d.]+,response_status:200,scheme:http
568-
message received: apisix\.upstream\.latency:[\d.]+\|h\|#source:apisix,route_name:datadog,path:\/articles\/\*\/comments,method:GET,balancer_ip:[\d.]+,response_status:200,scheme:http
569-
message received: apisix\.apisix\.latency:[\d.]+\|h\|#source:apisix,route_name:datadog,path:\/articles\/\*\/comments,method:GET,balancer_ip:[\d.]+,response_status:200,scheme:http
570-
message received: apisix\.ingress\.size:[\d]+\|ms\|#source:apisix,route_name:datadog,path:\/articles\/\*\/comments,method:GET,balancer_ip:[\d.]+,response_status:200,scheme:http
571-
message received: apisix\.egress\.size:[\d]+\|ms\|#source:apisix,route_name:datadog,path:\/articles\/\*\/comments,method:GET,balancer_ip:[\d.]+,response_status:200,scheme:http
569+
qr/message received: apisix\.request\.counter:1\|c\|#source:apisix,route_tag1:foo,route_tag2:bar,route_name:datadog,path:\/articles\/\*\/comments,method:GET,balancer_ip:[\d.]+,response_status:200,scheme:http
570+
message received: apisix\.request\.latency:[\d.]+\|h\|#source:apisix,route_tag1:foo,route_tag2:bar,route_name:datadog,path:\/articles\/\*\/comments,method:GET,balancer_ip:[\d.]+,response_status:200,scheme:http
571+
message received: apisix\.upstream\.latency:[\d.]+\|h\|#source:apisix,route_tag1:foo,route_tag2:bar,route_name:datadog,path:\/articles\/\*\/comments,method:GET,balancer_ip:[\d.]+,response_status:200,scheme:http
572+
message received: apisix\.apisix\.latency:[\d.]+\|h\|#source:apisix,route_tag1:foo,route_tag2:bar,route_name:datadog,path:\/articles\/\*\/comments,method:GET,balancer_ip:[\d.]+,response_status:200,scheme:http
573+
message received: apisix\.ingress\.size:[\d]+\|ms\|#source:apisix,route_tag1:foo,route_tag2:bar,route_name:datadog,path:\/articles\/\*\/comments,method:GET,balancer_ip:[\d.]+,response_status:200,scheme:http
574+
message received: apisix\.egress\.size:[\d]+\|ms\|#source:apisix,route_tag1:foo,route_tag2:bar,route_name:datadog,path:\/articles\/\*\/comments,method:GET,balancer_ip:[\d.]+,response_status:200,scheme:http
572575
/

0 commit comments

Comments
 (0)