diff --git a/apisix/plugins/datadog.lua b/apisix/plugins/datadog.lua index 972c0a2c7b9e..f6770c2f7d62 100644 --- a/apisix/plugins/datadog.lua +++ b/apisix/plugins/datadog.lua @@ -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} } } @@ -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 @@ -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 diff --git a/docs/en/latest/plugins/datadog.md b/docs/en/latest/plugins/datadog.md index 4b2d5b47c2ce..3e7bce56249d 100644 --- a/docs/en/latest/plugins/datadog.md +++ b/docs/en/latest/plugins/datadog.md @@ -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. @@ -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 diff --git a/t/plugin/datadog.t b/t/plugin/datadog.t index 506abcc0fe41..6bd2867e0d8a 100644 --- a/t/plugin/datadog.t +++ b/t/plugin/datadog.t @@ -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 +/