Skip to content

Commit b79329d

Browse files
feat: add log_format_extra to enrich the default logger format (#13568)
1 parent 6f62be4 commit b79329d

41 files changed

Lines changed: 793 additions & 6 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apisix/balancer.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ local function pick_server(route, ctx)
252252
local node = up_conf.nodes[1]
253253
ctx.balancer_ip = node.host
254254
ctx.balancer_port = node.port
255+
ctx.upstream_unresolved_host = node.domain or node.host
256+
-- also expose it as an nginx var so it can be used in the access log (http only)
257+
if is_http and ctx.var then
258+
ctx.var.upstream_unresolved_host = ctx.upstream_unresolved_host
259+
end
255260
node.upstream_host = parse_server_for_upstream_host(node, ctx.upstream_scheme)
256261
return node
257262
end
@@ -337,6 +342,11 @@ local function pick_server(route, ctx)
337342
res.domain = domain
338343
ctx.balancer_ip = res.host
339344
ctx.balancer_port = res.port
345+
ctx.upstream_unresolved_host = res.domain or res.host
346+
-- also expose it as an nginx var so it can be used in the access log (http only)
347+
if is_http and ctx.var then
348+
ctx.var.upstream_unresolved_host = ctx.upstream_unresolved_host
349+
end
340350
ctx.server_picker = server_picker
341351
res.upstream_host = parse_server_for_upstream_host(res, ctx.upstream_scheme)
342352

apisix/cli/ngx_tpl.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ http {
810810
811811
set $upstream_scheme 'http';
812812
set $upstream_host $http_host;
813+
set $upstream_unresolved_host '';
813814
set $upstream_uri '';
814815
set $request_line '';
815816
set $ctx_ref '';

apisix/core/ctx.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ do
190190
local ngx_var_names = {
191191
upstream_scheme = true,
192192
upstream_host = true,
193+
upstream_unresolved_host = true,
193194
upstream_upgrade = true,
194195
upstream_connection = true,
195196
upstream_uri = true,
@@ -244,6 +245,8 @@ do
244245
route_name = true,
245246
service_id = true,
246247
service_name = true,
248+
-- the upstream host before DNS resolution (configured domain/host)
249+
upstream_unresolved_host = true,
247250
}
248251

249252
local mt = {

apisix/plugins/clickhouse-logger.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ local schema = {
4242
name = {type = "string", default = "clickhouse logger"},
4343
ssl_verify = {type = "boolean", default = true},
4444
log_format = {type = "object"},
45+
log_format_extra = {type = "object"},
4546
include_req_body = {type = "boolean", default = false},
4647
include_req_body_expr = {
4748
type = "array",
@@ -72,6 +73,9 @@ local schema = {
7273
local metadata_schema = {
7374
type = "object",
7475
properties = {
76+
log_format_extra = {
77+
type = "object"
78+
},
7579
log_format = {
7680
type = "object"
7781
},

apisix/plugins/elasticsearch-logger.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ local schema = {
5555
required = {"index"}
5656
},
5757
log_format = {type = "object"},
58+
log_format_extra = {type = "object"},
5859
auth = {
5960
type = "object",
6061
properties = {
@@ -121,6 +122,9 @@ local schema = {
121122
local metadata_schema = {
122123
type = "object",
123124
properties = {
125+
log_format_extra = {
126+
type = "object"
127+
},
124128
log_format = {
125129
type = "object"
126130
},

apisix/plugins/file-logger.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ local schema = {
3434
type = "string"
3535
},
3636
log_format = {type = "object"},
37+
log_format_extra = {type = "object"},
3738
include_req_body = {type = "boolean", default = false},
3839
include_req_body_expr = {
3940
type = "array",
@@ -71,6 +72,9 @@ local metadata_schema = {
7172
},
7273
log_format = {
7374
type = "object"
75+
},
76+
log_format_extra = {
77+
type = "object"
7478
}
7579
}
7680
}

apisix/plugins/google-cloud-logging.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
local core = require("apisix.core")
1919
local plugin = require("apisix.plugin")
2020
local tostring = tostring
21+
local pairs = pairs
2122
local http = require("resty.http")
2223
local log_util = require("apisix.utils.log-util")
2324
local bp_manager_mod = require("apisix.utils.batch-processor-manager")
@@ -98,6 +99,7 @@ local schema = {
9899
default = "apisix.apache.org%2Flogs"
99100
},
100101
log_format = {type = "object"},
102+
log_format_extra = {type = "object"},
101103
},
102104
oneOf = {
103105
{ required = { "auth_config" } },
@@ -109,6 +111,9 @@ local schema = {
109111
local metadata_schema = {
110112
type = "object",
111113
properties = {
114+
log_format_extra = {
115+
type = "object"
116+
},
112117
log_format = {
113118
type = "object"
114119
},
@@ -190,7 +195,7 @@ end
190195

191196

192197
local function get_logger_entry(conf, ctx, oauth)
193-
local entry, customized = log_util.get_log_entry(plugin_name, conf, ctx)
198+
local entry, customized, extra = log_util.get_log_entry(plugin_name, conf, ctx)
194199
local google_entry
195200
if not customized then
196201
google_entry = {
@@ -210,6 +215,14 @@ local function get_logger_entry(conf, ctx, oauth)
210215
service_id = entry.service_id,
211216
},
212217
}
218+
-- the fixed payload above drops everything else, so add log_format_extra
219+
if extra then
220+
for k, v in pairs(extra) do
221+
if google_entry.jsonPayload[k] == nil then
222+
google_entry.jsonPayload[k] = v
223+
end
224+
end
225+
end
213226
else
214227
google_entry = {
215228
jsonPayload = entry,

apisix/plugins/http-logger.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ local schema = {
3434
auth_header = {type = "string"},
3535
timeout = {type = "integer", minimum = 1, default = 3},
3636
log_format = {type = "object"},
37+
log_format_extra = {type = "object"},
3738
include_req_body = {type = "boolean", default = false},
3839
include_req_body_expr = {
3940
type = "array",
@@ -64,6 +65,9 @@ local schema = {
6465
local metadata_schema = {
6566
type = "object",
6667
properties = {
68+
log_format_extra = {
69+
type = "object"
70+
},
6771
log_format = {
6872
type = "object"
6973
},

apisix/plugins/kafka-logger.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ local schema = {
4040
enum = {"default", "origin"},
4141
},
4242
log_format = {type = "object"},
43+
log_format_extra = {type = "object"},
4344
-- deprecated, use "brokers" instead
4445
broker_list = {
4546
type = "object",
@@ -146,6 +147,9 @@ local schema = {
146147
local metadata_schema = {
147148
type = "object",
148149
properties = {
150+
log_format_extra = {
151+
type = "object"
152+
},
149153
log_format = {
150154
type = "object"
151155
},

apisix/plugins/loggly.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ local schema = {
9595
default = true
9696
},
9797
log_format = {type = "object"},
98+
log_format_extra = {type = "object"},
9899
severity_map = {
99100
type = "object",
100101
description = "upstream response code vs syslog severity mapping",
@@ -143,6 +144,9 @@ local metadata_schema = {
143144
minimum = 1,
144145
default= defaults.timeout
145146
},
147+
log_format_extra = {
148+
type = "object"
149+
},
146150
log_format = {
147151
type = "object",
148152
}

0 commit comments

Comments
 (0)