Skip to content

Commit

Permalink
WIP: Include path in tags with Datadog plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
deiwin committed Jan 31, 2025
1 parent 5a085bc commit 7767c7d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
13 changes: 12 additions & 1 deletion apisix/plugins/datadog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ local batch_processor_manager = bp_manager_mod.new(plugin_name)
local schema = {
type = "object",
properties = {
prefer_name = {type = "boolean", default = true}
prefer_name = {type = "boolean", default = true},
include_path = {type = "boolean", default = false}
}
}

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

if entry.path and entry.path ~= "" then
core.table.insert(tags, "path:" .. entry.path)
end

if entry.service_id and entry.service_id ~= "" then
core.table.insert(tags, "service_name:" .. entry.service_id)
end
Expand Down Expand Up @@ -241,6 +246,12 @@ function _M.log(conf, ctx)
end
end

if conf.include_path then
if ctx.curr_req_matched and ctx.curr_req_matched._path then
entry.path = ctx.curr_req_matched._path
end
end

if batch_processor_manager:add_entry(conf, entry) then
return
end
Expand Down
8 changes: 5 additions & 3 deletions docs/en/latest/plugins/datadog.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ This Plugin provides the ability to push metrics as a batch to the external Data

## Attributes

| Name | Type | Required | Default | Valid values | Description |
| ----------- | ------- | -------- | ------- | ------------ | -------------------------------------------------------------------------------------- |
| prefer_name | boolean | False | true | [true,false] | When set to `false`, uses Route/Service ID instead of name (default) with metric tags. |
| Name | Type | Required | Default | Valid values | Description |
| ------------ | ------- | -------- | ------- | ------------ | -------------------------------------------------------------------------------------- |
| prefer_name | boolean | False | true | [true,false] | When set to `false`, uses Route/Service ID instead of name (default) with metric tags. |
| include_path | boolean | False | true | [true,false] | When set to `false`, uses Route/Service ID instead of name (default) with metric tags. |

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.

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

:::note

Expand Down
32 changes: 32 additions & 0 deletions t/plugin/datadog.t
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,35 @@ message received: apisix\.apisix\.latency:[\d.]+\|h\|#source:apisix,route_name:d
message received: apisix\.ingress\.size:[\d]+\|ms\|#source:apisix,route_name:datadog,consumer:user0,balancer_ip:[\d.]+,response_status:200,scheme:http
message received: apisix\.egress\.size:[\d]+\|ms\|#source:apisix,route_name:datadog,consumer:user0,balancer_ip:[\d.]+,response_status:200,scheme:http
/
=== TEST 11: testing behaviour with include_path
--- apisix_yaml
routes:
- uri: /articles/*/comments
name: datadog
upstream:
nodes:
"127.0.0.1:1982": 1
plugins:
datadog:
batch_max_size: 1
max_retry_count: 0
include_path: true
#END
--- request
GET /articles/12345/comments?foo=bar
--- response_body
opentracing
--- wait: 0.5
--- grep_error_log eval
qr/message received: apisix(.+?(?=, ))/
--- grep_error_log_out eval
qr/message received: apisix\.request\.counter:1\|c\|#source:apisix,route_name:datadog,path:/articles/\*/comments,balancer_ip:[\d.]+,response_status:200,scheme:http
message received: apisix\.request\.latency:[\d.]+\|h\|#source:apisix,route_name:datadog,path:/articles/\*/comments,balancer_ip:[\d.]+,response_status:200,scheme:http
message received: apisix\.upstream\.latency:[\d.]+\|h\|#source:apisix,route_name:datadog,path:/articles/\*/comments,balancer_ip:[\d.]+,response_status:200,scheme:http
message received: apisix\.apisix\.latency:[\d.]+\|h\|#source:apisix,route_name:datadog,path:/articles/\*/comments,balancer_ip:[\d.]+,response_status:200,scheme:http
message received: apisix\.ingress\.size:[\d]+\|ms\|#source:apisix,route_name:datadog,path:/articles/\*/comments,balancer_ip:[\d.]+,response_status:200,scheme:http
message received: apisix\.egress\.size:[\d]+\|ms\|#source:apisix,route_name:datadog,path:/articles/\*/comments,balancer_ip:[\d.]+,response_status:200,scheme:http
/

0 comments on commit 7767c7d

Please sign in to comment.