Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add global name #1524

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apisix/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
3 changes: 3 additions & 0 deletions apisix/core/config_etcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions apisix/core/etcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -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
Expand Down
28 changes: 16 additions & 12 deletions apisix/plugins/prometheus/exporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -62,27 +64,28 @@ 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",
{"type", "service", "node"}, DEFAULT_BUCKETS)

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


Expand All @@ -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


Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apisix/plugins/skywalking/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions apisix/plugins/zipkin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion apisix/plugins/zipkin/reporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions apisix/utils/log-util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ local function get_full_log(ngx, conf)
service_id = var.host
end


local log = {
request = {
url = url,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 75 additions & 0 deletions t/core/core.t
Original file line number Diff line number Diff line change
@@ -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]
36 changes: 29 additions & 7 deletions t/plugin/prometheus.t
Original file line number Diff line number Diff line change
Expand Up @@ -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__

Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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]

Expand All @@ -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]

Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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]