You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/11/01 06:48:46 UTC

[GitHub] [apisix] spacewander commented on a change in pull request #5372: feat(plugins): Datadog for metrics collection

spacewander commented on a change in pull request #5372:
URL: https://github.com/apache/apisix/pull/5372#discussion_r739981995



##########
File path: apisix/plugins/datadog.lua
##########
@@ -0,0 +1,178 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+local core = require("apisix.core")
+local plugin = require("apisix.plugin")
+local send_statsd = require("apisix.plugins.udp-logger").send_udp_data
+local fetch_info = require("apisix.plugins.prometheus.exporter").parse_info_from_ctx
+local format = string.format
+local concat = table.concat
+local tostring = tostring
+local ngx = ngx
+
+
+local plugin_name = "datadog"
+
+local schema = {
+    type = "object",
+    properties = {
+    }
+}
+
+local metadata_schema = {
+    type = "object",
+    properties = {
+        host = {type = "string"},
+        port = {type = "integer", minimum = 0},
+        namespace = {type = "string", default = "apisix.dev"},

Review comment:
       ```suggestion
           namespace = {type = "string", default = "apisix"},
   ```
   
   Would be better. The idea given in the mail list is just an example...

##########
File path: apisix/plugins/prometheus/exporter.lua
##########
@@ -131,6 +128,13 @@ function _M.log(conf, ctx)
             end
         end
     end
+    return route_id, service_id, consumer_name, balancer_ip
+end
+
+function _M.log(conf, ctx)
+    local vars = ctx.var
+
+    local route_id, service_id, consumer_name, balancer_ip = _M.parse_info_from_ctx(conf, ctx)

Review comment:
       For code style, we don't call a module level method inside another module level method. We use this:
   ```
   local function fetch_req_info(conf, ctx)
   end
   
   _M.fetch_req_info = fetch_req_info
   ```

##########
File path: apisix/plugins/datadog.lua
##########
@@ -0,0 +1,178 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+local core = require("apisix.core")
+local plugin = require("apisix.plugin")
+local send_statsd = require("apisix.plugins.udp-logger").send_udp_data
+local fetch_info = require("apisix.plugins.prometheus.exporter").parse_info_from_ctx
+local format = string.format
+local concat = table.concat
+local tostring = tostring
+local ngx = ngx
+
+
+local plugin_name = "datadog"
+
+local schema = {
+    type = "object",
+    properties = {
+    }
+}
+
+local metadata_schema = {
+    type = "object",
+    properties = {
+        host = {type = "string"},
+        port = {type = "integer", minimum = 0},
+        namespace = {type = "string", default = "apisix.dev"},
+        sample_rate = {type = "number", default = 1, minimum = 0, maximum = 1},
+        tags = {

Review comment:
       Use name constant_tags would be better?

##########
File path: apisix/plugins/datadog.lua
##########
@@ -0,0 +1,178 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+local core = require("apisix.core")
+local plugin = require("apisix.plugin")
+local send_statsd = require("apisix.plugins.udp-logger").send_udp_data
+local fetch_info = require("apisix.plugins.prometheus.exporter").parse_info_from_ctx
+local format = string.format
+local concat = table.concat
+local tostring = tostring
+local ngx = ngx
+
+
+local plugin_name = "datadog"
+
+local schema = {
+    type = "object",
+    properties = {
+    }
+}
+
+local metadata_schema = {
+    type = "object",
+    properties = {
+        host = {type = "string"},
+        port = {type = "integer", minimum = 0},
+        namespace = {type = "string", default = "apisix.dev"},
+        sample_rate = {type = "number", default = 1, minimum = 0, maximum = 1},
+        tags = {
+            type = "array",
+            items = {type = "string"},
+            default = {"source:apisix"}
+        }
+    },
+    required = {"host", "port"}

Review comment:
       If we provide a default value, we don't need to require the user to configure the metadata before using it. Considering statsd is usually running as a sidecar, we can use "127.0.0.1" as the default host and 8125 as the default port. 

##########
File path: apisix/plugins/datadog.lua
##########
@@ -0,0 +1,178 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+local core = require("apisix.core")
+local plugin = require("apisix.plugin")
+local send_statsd = require("apisix.plugins.udp-logger").send_udp_data

Review comment:
       We should not require the main module of another plugin, which will have a side effect: the plugin will be enabled once the datadog is enabled.
   
   Considering the send_udp_data is small, we can copy it directly.

##########
File path: apisix/plugins/prometheus/exporter.lua
##########
@@ -110,10 +110,7 @@ function _M.init()
 
 end
 
-
-function _M.log(conf, ctx)
-    local vars = ctx.var
-
+function _M.parse_info_from_ctx(conf, ctx)

Review comment:
       ```suggestion
   function _M.fetch_req_info(conf, ctx)
   ```
   would be better

##########
File path: t/plugin/datadog.t
##########
@@ -0,0 +1,156 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+run_tests;
+
+__DATA__
+
+=== TEST 1: sanity check metadata
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.datadog")
+            local ok, err = plugin.check_schema({host = "127.0.0.1", port = 8125}, 2)
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+done
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: missing host inside metadata
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.datadog")
+            local ok, err = plugin.check_schema({port = 8125}, 2)
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+property "host" is required
+done
+--- no_error_log
+[error]
+
+
+
+=== TEST 3: add plugin

Review comment:
       We need a way to test it with a backend. It would be great if you find a way to run dogstatsd locally and check the content it receives (let it report to a mock HTTP datadog backend, etc.). Another solution is writing a mock UDP backend, see https://github.com/apache/apisix/blob/759e6663162d43e761d556b85e3061f755ea74ae/t/plugin/ext-plugin/sanity.t#L34 and https://github.com/openresty/stream-lua-nginx-module

##########
File path: apisix/plugins/datadog.lua
##########
@@ -0,0 +1,178 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+local core = require("apisix.core")
+local plugin = require("apisix.plugin")
+local send_statsd = require("apisix.plugins.udp-logger").send_udp_data
+local fetch_info = require("apisix.plugins.prometheus.exporter").parse_info_from_ctx
+local format = string.format
+local concat = table.concat
+local tostring = tostring
+local ngx = ngx
+
+
+local plugin_name = "datadog"
+
+local schema = {
+    type = "object",
+    properties = {
+    }
+}
+
+local metadata_schema = {
+    type = "object",
+    properties = {
+        host = {type = "string"},
+        port = {type = "integer", minimum = 0},
+        namespace = {type = "string", default = "apisix.dev"},
+        sample_rate = {type = "number", default = 1, minimum = 0, maximum = 1},

Review comment:
       I think we don't need to implement sample_rate as this version, otherwise, we need to sample the request in the given rate.

##########
File path: apisix/plugins/datadog.lua
##########
@@ -0,0 +1,178 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+local core = require("apisix.core")
+local plugin = require("apisix.plugin")
+local send_statsd = require("apisix.plugins.udp-logger").send_udp_data
+local fetch_info = require("apisix.plugins.prometheus.exporter").parse_info_from_ctx
+local format = string.format
+local concat = table.concat
+local tostring = tostring
+local ngx = ngx
+
+
+local plugin_name = "datadog"
+
+local schema = {
+    type = "object",
+    properties = {
+    }
+}
+
+local metadata_schema = {
+    type = "object",
+    properties = {
+        host = {type = "string"},
+        port = {type = "integer", minimum = 0},
+        namespace = {type = "string", default = "apisix.dev"},
+        sample_rate = {type = "number", default = 1, minimum = 0, maximum = 1},
+        tags = {
+            type = "array",
+            items = {type = "string"},
+            default = {"source:apisix"}
+        }
+    },
+    required = {"host", "port"}
+}
+
+local _M = {
+    version = 0.1,
+    priority = 495,
+    name = plugin_name,
+    schema = schema,
+    metadata_schema = metadata_schema,
+}
+
+function _M.check_schema(conf, schema_type)
+    if schema_type == core.schema.TYPE_METADATA then
+        return core.schema.check(metadata_schema, conf)
+    end
+    return core.schema.check(schema, conf)
+end
+
+local function generate_tag(sample_rate, tag_arr, route_id, service_id,
+        consumer_name, balancer_ip, http_status)
+    local rate, tags = "", ""
+
+    if sample_rate and sample_rate ~= 1 then
+        rate = "|@" .. tostring(sample_rate)
+    end
+
+    if tag_arr and #tag_arr > 0 then
+        tags = "|#" .. concat(tag_arr, ",")
+    end
+
+    if route_id ~= "" then
+        tags = tags .. "route_id:" .. route_id
+    end
+
+    if service_id ~= "" then
+        tags = tags .. "service_id:" .. service_id
+    end
+
+    if consumer_name ~= "" then
+        tags = tags .. "consumer_name:" .. consumer_name
+    end
+    if balancer_ip ~= "" then
+        tags = tags .. "balancer_ip:" .. balancer_ip
+    end
+    if http_status then
+        tags = tags .. "http_status:" .. http_status
+    end
+
+    if tags ~= "" and tags:sub(1, 1) ~= "|" then
+        tags = "|#" .. tags
+    end
+
+    return rate .. tags
+
+end
+
+function _M.log(conf, ctx)
+    local metadata = plugin.plugin_metadata(plugin_name)
+    if not metadata then

Review comment:
       A more appreciated way is to prove a couple of the default values when metadata is missing. See https://github.com/apache/apisix/blob/759e6663162d43e761d556b85e3061f755ea74ae/apisix/plugins/batch-requests.lua#L213




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org