From 459f41960e5dfa47c67f28fddc15173bf5b17d2e Mon Sep 17 00:00:00 2001 From: qiujiayu <153163285@qq.com> Date: Tue, 31 Mar 2020 21:46:14 +0800 Subject: [PATCH 1/5] add global name --- apisix/core.lua | 1 + apisix/core/config_etcd.lua | 2 +- apisix/core/etcd.lua | 2 +- apisix/plugins/prometheus/exporter.lua | 28 +++++++++++++++----------- apisix/plugins/zipkin/reporter.lua | 3 ++- apisix/utils/log-util.lua | 2 ++ conf/config.yaml | 1 + 7 files changed, 24 insertions(+), 15 deletions(-) diff --git a/apisix/core.lua b/apisix/core.lua index 051dae36af75..88be71541459 100644 --- a/apisix/core.lua +++ b/apisix/core.lua @@ -23,6 +23,7 @@ log.info("use config_center: ", config_center) return { version = require("apisix.core.version"), + name = local_conf.apisix and local_conf.apisix.name or "APISIX", log = log, config = require("apisix.core.config_" .. config_center), json = require("apisix.core.json"), diff --git a/apisix/core/config_etcd.lua b/apisix/core/config_etcd.lua index dd0a5487bac1..ffce07717ea6 100644 --- a/apisix/core/config_etcd.lua +++ b/apisix/core/config_etcd.lua @@ -380,7 +380,7 @@ function _M.new(key, opts) end local etcd_conf = clone_tab(local_conf.etcd) - local prefix = etcd_conf.prefix + local prefix = etcd_conf.prefix or (local_conf.apisix.name and '/' .. local_conf.apisix.name) or "/apisix" etcd_conf.http_host = etcd_conf.host etcd_conf.host = nil etcd_conf.prefix = nil diff --git a/apisix/core/etcd.lua b/apisix/core/etcd.lua index 818c99b95d1c..5f10b3c0047e 100644 --- a/apisix/core/etcd.lua +++ b/apisix/core/etcd.lua @@ -28,7 +28,7 @@ local function new() end local etcd_conf = clone_tab(local_conf.etcd) - local prefix = etcd_conf.prefix + local prefix = etcd_conf.prefix or (local_conf.apisix.name and '/' .. local_conf.apisix.name) or "/apisix" etcd_conf.http_host = etcd_conf.host etcd_conf.host = nil etcd_conf.prefix = nil diff --git a/apisix/plugins/prometheus/exporter.lua b/apisix/plugins/prometheus/exporter.lua index 2c0b6fc618ab..cbe7d30e5eff 100644 --- a/apisix/plugins/prometheus/exporter.lua +++ b/apisix/plugins/prometheus/exporter.lua @@ -23,6 +23,8 @@ local re_gmatch = ngx.re.gmatch local tonumber = tonumber local select = select local prometheus +local app_name_label_name = "application_name" + -- Default set of latency buckets, 1ms to 60s: local DEFAULT_BUCKETS = { 1, 2, 5, 7, 10, 15, 20, 25, 30, 40, 50, 60, 70, @@ -62,19 +64,20 @@ function _M.init() prometheus = base_prometheus.init("prometheus-metrics", "apisix_") metrics.connections = prometheus:gauge("nginx_http_current_connections", "Number of HTTP connections", - {"state"}) + {"state", app_name_label_name}) metrics.etcd_reachable = prometheus:gauge("etcd_reachable", - "Config server etcd reachable from APISIX, 0 is unreachable") + "Config server etcd reachable from APISIX, 0 is unreachable", + {app_name_label_name}) -- per service metrics.status = prometheus:counter("http_status", "HTTP status codes per service in APISIX", - {"code", "route", "service", "node"}) + {"code", "route", "service", "node", app_name_label_name}) metrics.latency = prometheus:histogram("http_latency", "HTTP request latency per service in APISIX", - {"type", "service", "node"}, DEFAULT_BUCKETS) + {"type", "service", "node", app_name_label_name}, DEFAULT_BUCKETS) metrics.overhead = prometheus:histogram("http_overhead", "HTTP request overhead per service in APISIX", @@ -82,7 +85,7 @@ function _M.init() metrics.bandwidth = prometheus:counter("bandwidth", "Total bandwidth in bytes consumed per service in APISIX", - {"type", "route", "service", "node"}) + {"type", "route", "service", "node", app_name_label_name}) end @@ -102,24 +105,24 @@ function _M.log(conf, ctx) end metrics.status:inc(1, - gen_arr(vars.status, route_id, service_id, balancer_ip)) + gen_arr(vars.status, route_id, service_id, balancer_ip, core.name)) local latency = (ngx.now() - ngx.req.start_time()) * 1000 metrics.latency:observe(latency, - gen_arr("request", service_id, balancer_ip)) + gen_arr("request", service_id, balancer_ip, core.name)) local overhead = latency if ctx.var.upstream_response_time then overhead = overhead - tonumber(ctx.var.upstream_response_time) * 1000 end metrics.overhead:observe(overhead, - gen_arr("request", service_id, balancer_ip)) + gen_arr("request", service_id, balancer_ip, core.name)) metrics.bandwidth:inc(vars.request_length, - gen_arr("ingress", route_id, service_id, balancer_ip)) + gen_arr("ingress", route_id, service_id, balancer_ip, core.name)) metrics.bandwidth:inc(vars.bytes_sent, - gen_arr("egress", route_id, service_id, balancer_ip)) + gen_arr("egress", route_id, service_id, balancer_ip, core.name)) end @@ -151,6 +154,7 @@ local function nginx_status() end label_values[1] = name + label_values[2] = core.name metrics.connections:set(val[0], label_values) end end @@ -170,10 +174,10 @@ function _M.collect() local config = core.config.new() local version, err = config:server_version() if version then - metrics.etcd_reachable:set(1) + metrics.etcd_reachable:set(1, app_name_label_name) else - metrics.etcd_reachable:set(0) + metrics.etcd_reachable:set(0, app_name_label_name) core.log.error("prometheus: failed to reach config server while ", "processing metrics endpoint: ", err) end diff --git a/apisix/plugins/zipkin/reporter.lua b/apisix/plugins/zipkin/reporter.lua index cc71fc5c8cca..cf1f337cb6e6 100644 --- a/apisix/plugins/zipkin/reporter.lua +++ b/apisix/plugins/zipkin/reporter.lua @@ -14,6 +14,7 @@ -- See the License for the specific language governing permissions and -- limitations under the License. -- +local core = require "apisix.core" local resty_http = require "resty.http" local to_hex = require "resty.string".to_hex local cjson = require "cjson".new() @@ -39,7 +40,7 @@ local span_kind_map = { function _M.new(conf) local endpoint = conf.endpoint - local service_name = conf.service_name + local service_name = conf.service_name or core.name local server_port = conf.server_port local server_addr = conf.server_addr assert(type(endpoint) == "string", "invalid http endpoint") diff --git a/apisix/utils/log-util.lua b/apisix/utils/log-util.lua index b11a435808f8..1714874fe5b1 100644 --- a/apisix/utils/log-util.lua +++ b/apisix/utils/log-util.lua @@ -34,6 +34,7 @@ local function get_full_log(ngx, conf) service_id = var.host end + local log = { request = { url = url, @@ -48,6 +49,7 @@ local function get_full_log(ngx, conf) headers = ngx.resp.get_headers(), size = var.bytes_sent }, + application_name = core.name, upstream = var.upstream_addr, service_id = service_id, route_id = route_id, diff --git a/conf/config.yaml b/conf/config.yaml index fe7716d87afd..26ad5caf8fcb 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -15,6 +15,7 @@ # limitations under the License. # apisix: + name: APISIX # APISIX gateway name node_listen: 9080 # APISIX listening port enable_heartbeat: true enable_admin: true From d3954fad6209bf78d9cdb541d3a86488b9ef71eb Mon Sep 17 00:00:00 2001 From: qiujiayu <153163285@qq.com> Date: Thu, 30 Apr 2020 20:20:03 +0800 Subject: [PATCH 2/5] fix test case --- apisix/plugins/prometheus/exporter.lua | 4 ++-- t/plugin/prometheus.t | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/apisix/plugins/prometheus/exporter.lua b/apisix/plugins/prometheus/exporter.lua index cbe7d30e5eff..da5d4661630d 100644 --- a/apisix/plugins/prometheus/exporter.lua +++ b/apisix/plugins/prometheus/exporter.lua @@ -174,10 +174,10 @@ function _M.collect() local config = core.config.new() local version, err = config:server_version() if version then - metrics.etcd_reachable:set(1, app_name_label_name) + metrics.etcd_reachable:set(1, core.name) else - metrics.etcd_reachable:set(0, app_name_label_name) + metrics.etcd_reachable:set(0, core.name) core.log.error("prometheus: failed to reach config server while ", "processing metrics endpoint: ", err) end diff --git a/t/plugin/prometheus.t b/t/plugin/prometheus.t index 4e99b84d4a95..d230c4d8cd25 100644 --- a/t/plugin/prometheus.t +++ b/t/plugin/prometheus.t @@ -121,7 +121,7 @@ passed --- request GET /apisix/prometheus/metrics --- response_body_like -apisix_etcd_reachable 1 +apisix_etcd_reachable{application_name="APISIX"} 1 --- no_error_log [error] @@ -151,7 +151,7 @@ apisix_etcd_reachable 1 --- request GET /apisix/prometheus/metrics --- response_body eval -qr/apisix_bandwidth\{type="egress",route="1",service="",node="127.0.0.1"\} \d+/ +qr/apisix_bandwidth\{type="egress",route="1",service="",node="127.0.0.1",application_name="APISIX"\} \d+/ --- no_error_log [error] @@ -293,7 +293,7 @@ passed --- request GET /apisix/prometheus/metrics --- response_body eval -qr/apisix_bandwidth\{type="egress",route="1",service="",node="127.0.0.1"\} \d+/ +qr/apisix_bandwidth\{type="egress",route="1",service="",node="127.0.0.1",application_name="APISIX"\} \d+/ --- no_error_log [error] @@ -303,7 +303,7 @@ qr/apisix_bandwidth\{type="egress",route="1",service="",node="127.0.0.1"\} \d+/ --- request GET /apisix/prometheus/metrics --- response_body eval -qr/apisix_http_latency_count\{type="request",service="",node="127.0.0.1"\} \d+/ +qr/apisix_http_latency_count\{type="request",service="",node="127.0.0.1",application_name="APISIX"\} \d+/ --- no_error_log [error] @@ -386,7 +386,7 @@ passed --- request GET /apisix/prometheus/metrics --- response_body eval -qr/apisix_bandwidth\{type="egress",route="2",service="1",node="127.0.0.1"\} \d+/ +qr/apisix_bandwidth\{type="egress",route="2",service="1",node="127.0.0.1",application_name="APISIX"\} \d+/ --- no_error_log [error] @@ -521,7 +521,7 @@ passed --- request GET /apisix/prometheus/metrics --- response_body eval -qr/apisix_http_status\{code="404",route="3",service="",node="127.0.0.1"\} 2/ +qr/apisix_http_status\{code="404",route="3",service="",node="127.0.0.1",application_name="APISIX"\} 2/ --- no_error_log [error] From a4f90e8b4fe414f65d62a3c13aca48c130e04390 Mon Sep 17 00:00:00 2001 From: qiujiayu <153163285@qq.com> Date: Mon, 4 May 2020 22:59:37 +0800 Subject: [PATCH 3/5] add test case --- apisix/core/config_etcd.lua | 5 +- apisix/core/etcd.lua | 6 ++- apisix/plugins/zipkin.lua | 3 +- apisix/plugins/zipkin/reporter.lua | 1 + t/core/core.t | 74 ++++++++++++++++++++++++++++++ t/plugin/prometheus.t | 24 +++++++++- 6 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 t/core/core.t diff --git a/apisix/core/config_etcd.lua b/apisix/core/config_etcd.lua index ffce07717ea6..177f68a4f615 100644 --- a/apisix/core/config_etcd.lua +++ b/apisix/core/config_etcd.lua @@ -380,7 +380,10 @@ function _M.new(key, opts) end local etcd_conf = clone_tab(local_conf.etcd) - local prefix = etcd_conf.prefix or (local_conf.apisix.name and '/' .. local_conf.apisix.name) or "/apisix" + local prefix = etcd_conf.prefix + or (local_conf.apisix.name and '/' .. local_conf.apisix.name) + or "/apisix" + log.info("config etcd prefix ", prefix) etcd_conf.http_host = etcd_conf.host etcd_conf.host = nil etcd_conf.prefix = nil diff --git a/apisix/core/etcd.lua b/apisix/core/etcd.lua index 5f10b3c0047e..2e8fe5f1c125 100644 --- a/apisix/core/etcd.lua +++ b/apisix/core/etcd.lua @@ -17,6 +17,7 @@ local fetch_local_conf = require("apisix.core.config_local").local_conf local etcd = require("resty.etcd") local clone_tab = require("table.clone") +local log = require("apisix.core.log") local _M = {version = 0.1} @@ -28,7 +29,10 @@ local function new() end local etcd_conf = clone_tab(local_conf.etcd) - local prefix = etcd_conf.prefix or (local_conf.apisix.name and '/' .. local_conf.apisix.name) or "/apisix" + local prefix = etcd_conf.prefix + or (local_conf.apisix.name and '/' .. local_conf.apisix.name) + or "/apisix" + log.info("etcd prefix ", prefix) etcd_conf.http_host = etcd_conf.host etcd_conf.host = nil etcd_conf.prefix = nil diff --git a/apisix/plugins/zipkin.lua b/apisix/plugins/zipkin.lua index 934d88398ac1..458bf9f3770f 100644 --- a/apisix/plugins/zipkin.lua +++ b/apisix/plugins/zipkin.lua @@ -33,8 +33,7 @@ local schema = { sample_ratio = {type = "number", minimum = 0.00001, maximum = 1}, service_name = { type = "string", - description = "service name for zipkin reporter", - default = "APISIX", + description = "service name for zipkin reporter" }, server_addr = { type = "string", diff --git a/apisix/plugins/zipkin/reporter.lua b/apisix/plugins/zipkin/reporter.lua index cf1f337cb6e6..a005053d9cfa 100644 --- a/apisix/plugins/zipkin/reporter.lua +++ b/apisix/plugins/zipkin/reporter.lua @@ -44,6 +44,7 @@ function _M.new(conf) local server_port = conf.server_port local server_addr = conf.server_addr assert(type(endpoint) == "string", "invalid http endpoint") + core.log.info("the service name of zipkin:", service_name) return setmetatable({ endpoint = endpoint, service_name = service_name, diff --git a/t/core/core.t b/t/core/core.t new file mode 100644 index 000000000000..164c861195d2 --- /dev/null +++ b/t/core/core.t @@ -0,0 +1,74 @@ +# +# 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_shuffle(); +no_root_location(); + +sub read_file($) { + my $infile = shift; + open my $in, $infile + or die "cannot open $infile for reading: $!"; + my $cert = do { local $/; <$in> }; + close $in; + $cert; +} + +our $yaml_config = read_file("conf/config.yaml"); +$yaml_config =~ s/name: APISIX/name: test-gateway/; +$yaml_config =~ s/prefix: \"\/apisix\"/prefix: \"\/test-gateway\"/; + +run_tests(); + +__DATA__ + +=== TEST 1: the default core name +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + ngx.say(core.name) + } + } +--- request +GET /t +--- response_body +APISIX +--- no_error_log +[error] + + + +=== TEST 2: custom name +--- yaml_config eval: $::yaml_config +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + ngx.say(core.name) + } + } +--- request +GET /t +--- response_body +test-gateway +--- error_log +config etcd prefix /test-gateway +--- no_error_log +[error] \ No newline at end of file diff --git a/t/plugin/prometheus.t b/t/plugin/prometheus.t index d230c4d8cd25..0485beb568ba 100644 --- a/t/plugin/prometheus.t +++ b/t/plugin/prometheus.t @@ -30,7 +30,20 @@ repeat_each(1); no_long_string(); no_shuffle(); no_root_location(); -run_tests; + +sub read_file($) { + my $infile = shift; + open my $in, $infile + or die "cannot open $infile for reading: $!"; + my $cert = do { local $/; <$in> }; + close $in; + $cert; +} + +our $yaml_config = read_file("conf/config.yaml"); +$yaml_config =~ s/name: APISIX/name: test-gateway/; + +run_tests(); __DATA__ @@ -663,3 +676,12 @@ GET /t passed --- no_error_log [error] + +=== TEST 32: change application_name +--- yaml_config eval: $::yaml_config +--- request +GET /apisix/prometheus/metrics +--- response_body_like +apisix_etcd_reachable{application_name="test-gateway"} 1 +--- no_error_log +[error] From e6bc075ec46f061b07e341fa990f0b1e13c8fbee Mon Sep 17 00:00:00 2001 From: qiujiayu <153163285@qq.com> Date: Mon, 4 May 2020 23:55:04 +0800 Subject: [PATCH 4/5] fix --- t/core/core.t | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/t/core/core.t b/t/core/core.t index 164c861195d2..d56f9e42c1c0 100644 --- a/t/core/core.t +++ b/t/core/core.t @@ -69,6 +69,7 @@ GET /t --- response_body test-gateway --- error_log -config etcd prefix /test-gateway +config etcd prefix /test-gateway, +etcd prefix /test-gateway, --- no_error_log -[error] \ No newline at end of file +[error] From 8c15f80ee68fc15cd193798861d2b554be08c73a Mon Sep 17 00:00:00 2001 From: qiujiayu <153163285@qq.com> Date: Sun, 31 May 2020 16:40:04 +0800 Subject: [PATCH 5/5] use core.name for skywalking --- apisix/plugins/skywalking/client.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/plugins/skywalking/client.lua b/apisix/plugins/skywalking/client.lua index f83a6e35bf80..60cd8a80a210 100644 --- a/apisix/plugins/skywalking/client.lua +++ b/apisix/plugins/skywalking/client.lua @@ -33,7 +33,7 @@ local function register_service(conf) return service_id end - local service_name = conf.service_name + local service_name = conf.service_name or core.name local service = register.newServiceRegister(service_name) local httpc = http.new()