Skip to content

Commit ba35450

Browse files
committed
feat(datadog): add include_path option
Currently there is no way to distinguish Datadog metrics for different HTTP endpoints if these endpoints are served through a single Apisix route. With these changes, if `include_path` is set to true, the path pattern by which the HTTP request was matched to a route is included as a metric tag with the `path:` key. This allows different endpoints to be distinguished in metrics.
1 parent 7898012 commit ba35450

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

apisix/plugins/datadog.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ local batch_processor_manager = bp_manager_mod.new(plugin_name)
3737
local schema = {
3838
type = "object",
3939
properties = {
40-
prefer_name = {type = "boolean", default = true}
40+
prefer_name = {type = "boolean", default = true},
41+
include_path = {type = "boolean", default = false}
4142
}
4243
}
4344

@@ -84,6 +85,10 @@ local function generate_tag(entry, const_tags)
8485
core.table.insert(tags, "route_name:" .. entry.route_id)
8586
end
8687

88+
if entry.path and entry.path ~= "" then
89+
core.table.insert(tags, "path:" .. entry.path)
90+
end
91+
8792
if entry.service_id and entry.service_id ~= "" then
8893
core.table.insert(tags, "service_name:" .. entry.service_id)
8994
end
@@ -241,6 +246,12 @@ function _M.log(conf, ctx)
241246
end
242247
end
243248

249+
if conf.include_path then
250+
if ctx.curr_req_matched and ctx.curr_req_matched._path then
251+
entry.path = ctx.curr_req_matched._path
252+
end
253+
end
254+
244255
if batch_processor_manager:add_entry(conf, entry) then
245256
return
246257
end

docs/en/latest/plugins/datadog.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ 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. |
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. |
4748

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

@@ -115,6 +116,7 @@ The metrics will be sent to the DogStatsD agent with the following tags:
115116
- `balancer_ip`: IP address of the Upstream balancer that processed the current request.
116117
- `response_status`: HTTP response status code.
117118
- `scheme`: Request scheme such as HTTP, gRPC, and gRPCs.
119+
- `path`: The HTTP path pattern. Only available if the attribute `include_path` is set to true.
118120

119121
:::note
120122

t/plugin/datadog.t

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,37 @@ message received: apisix\.apisix\.latency:[\d.]+\|h\|#source:apisix,route_name:d
535535
message received: apisix\.ingress\.size:[\d]+\|ms\|#source:apisix,route_name:datadog,consumer:user0,balancer_ip:[\d.]+,response_status:200,scheme:http
536536
message received: apisix\.egress\.size:[\d]+\|ms\|#source:apisix,route_name:datadog,consumer:user0,balancer_ip:[\d.]+,response_status:200,scheme:http
537537
/
538+
539+
540+
541+
=== TEST 11: testing behaviour with include_path
542+
--- apisix_yaml
543+
routes:
544+
- uri: /articles/*/comments
545+
name: datadog
546+
upstream:
547+
nodes:
548+
"127.0.0.1:1982": 1
549+
plugins:
550+
datadog:
551+
batch_max_size: 1
552+
max_retry_count: 0
553+
include_path: true
554+
proxy-rewrite:
555+
uri: /opentracing
556+
#END
557+
--- request
558+
GET /articles/12345/comments?foo=bar
559+
--- response_body
560+
opentracing
561+
--- wait: 0.5
562+
--- grep_error_log eval
563+
qr/message received: apisix(.+?(?=, ))/
564+
--- 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
571+
/

0 commit comments

Comments
 (0)