Skip to content

Commit 79d9ed4

Browse files
committed
feat(datadog): add include_method option
Currently there is no way to distinguish Datadog metrics for e.g. GET & POST requests for the same endpoint within a single Apisix route, although their performance characteristics are likely to be quite different. With these changes, if `include_method` is set to true, HTTP method is included as a metric tag with the `method:` key, enabling such requests to be differentiated.
1 parent ba35450 commit 79d9ed4

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

apisix/plugins/datadog.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ local schema = {
3838
type = "object",
3939
properties = {
4040
prefer_name = {type = "boolean", default = true},
41-
include_path = {type = "boolean", default = false}
41+
include_path = {type = "boolean", default = false},
42+
include_method = {type = "boolean", default = false}
4243
}
4344
}
4445

@@ -89,6 +90,10 @@ local function generate_tag(entry, const_tags)
8990
core.table.insert(tags, "path:" .. entry.path)
9091
end
9192

93+
if entry.method and entry.method ~= "" then
94+
core.table.insert(tags, "method:" .. entry.method)
95+
end
96+
9297
if entry.service_id and entry.service_id ~= "" then
9398
core.table.insert(tags, "service_name:" .. entry.service_id)
9499
end
@@ -252,6 +257,10 @@ function _M.log(conf, ctx)
252257
end
253258
end
254259

260+
if conf.include_method then
261+
entry.method = ctx.var.method
262+
end
263+
255264
if batch_processor_manager:add_entry(conf, entry) then
256265
return
257266
end

docs/en/latest/plugins/datadog.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ 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. |
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. |
4849

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

@@ -117,6 +118,7 @@ The metrics will be sent to the DogStatsD agent with the following tags:
117118
- `response_status`: HTTP response status code.
118119
- `scheme`: Request scheme such as HTTP, gRPC, and gRPCs.
119120
- `path`: The HTTP path pattern. Only available if the attribute `include_path` is set to true.
121+
- `method`: The HTTP method. Only available if the attribute `include_method` is set to true.
120122

121123
:::note
122124

t/plugin/datadog.t

Lines changed: 8 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
541+
=== TEST 11: testing behaviour with include_path and include_method
542542
--- apisix_yaml
543543
routes:
544544
- uri: /articles/*/comments
@@ -551,6 +551,7 @@ routes:
551551
batch_max_size: 1
552552
max_retry_count: 0
553553
include_path: true
554+
include_method: true
554555
proxy-rewrite:
555556
uri: /opentracing
556557
#END
@@ -562,10 +563,10 @@ opentracing
562563
--- grep_error_log eval
563564
qr/message received: apisix(.+?(?=, ))/
564565
--- grep_error_log_out eval
565-
qr/message received: apisix\.request\.counter:1\|c\|#source:apisix,route_name:datadog,path:\/articles\/\*\/comments,balancer_ip:[\d.]+,response_status:200,scheme:http
566-
message received: apisix\.request\.latency:[\d.]+\|h\|#source:apisix,route_name:datadog,path:\/articles\/\*\/comments,balancer_ip:[\d.]+,response_status:200,scheme:http
567-
message received: apisix\.upstream\.latency:[\d.]+\|h\|#source:apisix,route_name:datadog,path:\/articles\/\*\/comments,balancer_ip:[\d.]+,response_status:200,scheme:http
568-
message received: apisix\.apisix\.latency:[\d.]+\|h\|#source:apisix,route_name:datadog,path:\/articles\/\*\/comments,balancer_ip:[\d.]+,response_status:200,scheme:http
569-
message received: apisix\.ingress\.size:[\d]+\|ms\|#source:apisix,route_name:datadog,path:\/articles\/\*\/comments,balancer_ip:[\d.]+,response_status:200,scheme:http
570-
message received: apisix\.egress\.size:[\d]+\|ms\|#source:apisix,route_name:datadog,path:\/articles\/\*\/comments,balancer_ip:[\d.]+,response_status:200,scheme:http
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
571572
/

0 commit comments

Comments
 (0)