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..177f68a4f615 100644 --- a/apisix/core/config_etcd.lua +++ b/apisix/core/config_etcd.lua @@ -381,6 +381,9 @@ function _M.new(key, opts) 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" + 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 818c99b95d1c..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} @@ -29,6 +30,9 @@ local function new() 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" + 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/prometheus/exporter.lua b/apisix/plugins/prometheus/exporter.lua index 2c0b6fc618ab..da5d4661630d 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, core.name) else - metrics.etcd_reachable:set(0) + 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/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() 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 cc71fc5c8cca..a005053d9cfa 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,10 +40,11 @@ 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") + core.log.info("the service name of zipkin:", service_name) return setmetatable({ endpoint = endpoint, service_name = service_name, 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 diff --git a/t/core/core.t b/t/core/core.t new file mode 100644 index 000000000000..d56f9e42c1c0 --- /dev/null +++ b/t/core/core.t @@ -0,0 +1,75 @@ +# +# 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, +etcd prefix /test-gateway, +--- no_error_log +[error] diff --git a/t/plugin/prometheus.t b/t/plugin/prometheus.t index 4e99b84d4a95..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__ @@ -121,7 +134,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 +164,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 +306,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 +316,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 +399,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 +534,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] @@ -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]