Skip to content

Commit 83be609

Browse files
committed
resolve comments
Signed-off-by: Nic <qianyong@api7.ai>
1 parent 51ebadd commit 83be609

8 files changed

Lines changed: 119 additions & 91 deletions

File tree

apisix/init.lua

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,21 +220,23 @@ function _M.ssl_client_hello_phase()
220220
core.log.error("failed to fetch ssl config: ", err)
221221
end
222222
core.log.error("failed to match any SSL certificate by SNI: ", sni)
223-
tracer.finish(ngx_ctx, span, tracer.status.ERROR, "failed match SNI")
223+
span:set_status(tracer.status.ERROR, "no matched SSL")
224+
span:finish(ngx_ctx)
224225
ngx_exit(-1)
225226
end
226227

227228
ok, err = apisix_ssl.set_protocols_by_clienthello(ngx_ctx.matched_ssl.value.ssl_protocols)
228229
if not ok then
229230
core.log.error("failed to set ssl protocols: ", err)
230-
tracer.finish(ngx_ctx, span, tracer.status.ERROR, "failed set protocols")
231+
span:set_status(tracer.status.ERROR, "failed set protocols")
232+
span:finish(ngx_ctx)
231233
ngx_exit(-1)
232234
end
233235

234236
-- in stream subsystem, ngx.ssl.server_name() return hostname of ssl session in preread phase,
235237
-- so that we can't get real SNI without recording it in ngx.ctx during client_hello phase
236238
ngx.ctx.client_hello_sni = sni
237-
tracer.finish(ngx_ctx, span)
239+
span:finish(ngx_ctx)
238240
end
239241

240242

@@ -732,7 +734,8 @@ function _M.http_access_phase()
732734

733735
local route = api_ctx.matched_route
734736
if not route then
735-
tracer.finish(ngx.ctx, match_span, tracer.status.ERROR, "no matched route")
737+
match_span:set_status(tracer.status.ERROR, "no matched route")
738+
match_span:finish(ngx.ctx)
736739
-- run global rule when there is no matching route
737740
local global_rules, conf_version = apisix_global_rules.global_rules()
738741
plugin.run_global_rules(api_ctx, global_rules, conf_version, nil)
@@ -741,7 +744,7 @@ function _M.http_access_phase()
741744
return core.response.exit(404,
742745
{error_msg = "404 Route Not Found"})
743746
end
744-
tracer.finish(ngx_ctx, match_span)
747+
match_span:finish(ngx_ctx)
745748

746749
core.log.info("matched route: ",
747750
core.json.delay_encode(api_ctx.matched_route, true))
@@ -833,7 +836,7 @@ function _M.http_access_phase()
833836
end
834837
plugin.run_plugin("access", plugins, api_ctx)
835838
end
836-
tracer.finish(ngx_ctx, span)
839+
span:finish(ngx_ctx)
837840

838841
_M.handle_upstream(api_ctx, route, enable_websocket)
839842

@@ -916,7 +919,7 @@ function _M.http_header_filter_phase()
916919
end
917920
core.response.set_header("Apisix-Plugins", core.table.concat(deduplicate, ", "))
918921
end
919-
tracer.finish(ngx_ctx, span)
922+
span:finish(ngx_ctx)
920923

921924
tracer.start(ngx_ctx, "apisix.phase.body_filter", tracer.kind.server)
922925
end

apisix/plugin.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ function _M.run_plugin(phase, plugins, api_ctx)
12321232
local span = tracer.start(api_ctx.ngx_ctx, "apisix.phase." .. phase
12331233
.. ".plugins." .. api_ctx._plugin_name)
12341234
phase_func(conf, api_ctx)
1235-
tracer.finish(api_ctx.ngx_ctx, span)
1235+
span:finish(api_ctx.ngx_ctx)
12361236
api_ctx._plugin_name = nil
12371237
end
12381238
end
@@ -1340,7 +1340,7 @@ function _M.run_global_rules(api_ctx, global_rules, conf_version, phase_name)
13401340
api_ctx.conf_type = orig_conf_type
13411341
api_ctx.conf_version = orig_conf_version
13421342
api_ctx.conf_id = orig_conf_id
1343-
tracer.finish(api_ctx.ngx_ctx, span)
1343+
span:finish(api_ctx.ngx_ctx)
13441344
end
13451345
end
13461346

apisix/secret.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,12 @@ local function fetch_by_uri_secret(secret_uri)
158158

159159
local value, err = sm.get(conf, opts.key)
160160
if err then
161-
tracer.finish(ngx.ctx, span, tracer.status.ERROR, err)
161+
span:set_status(tracer.status.ERROR, err)
162+
span:finish(ngx.ctx)
162163
return nil, err
163164
end
164165

165-
tracer.finish(ngx.ctx, span)
166+
span:finish(ngx.ctx)
166167
return value
167168
end
168169

apisix/ssl/router/radixtree_sni.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,11 @@ function _M.match_and_set(api_ctx, match_only, alt_sni)
179179
-- with it sometimes
180180
core.log.error("failed to find any SSL certificate by SNI: ", sni)
181181
end
182-
tracer.finish(api_ctx.ngx_ctx, span, tracer.status.ERROR, "failed match SNI")
182+
span:set_status(tracer.status.ERROR, "failed match SNI")
183+
span:finish(api_ctx.ngx_ctx)
183184
return false
184185
end
185-
tracer.finish(api_ctx.ngx_ctx, span)
186+
span:finish(api_ctx.ngx_ctx)
186187

187188
if api_ctx.matched_sni == "*" then
188189
-- wildcard matches everything, no need for further validation

apisix/tracer.lua

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
-- See the License for the specific language governing permissions and
1515
-- limitations under the License.
1616
--
17-
local table = require("apisix.core.table")
1817
local tablepool = require("tablepool")
1918
local span = require("apisix.utils.span")
19+
local noop_span = require("apisix.utils.noop_span").new()
2020
local span_kind = require("opentelemetry.trace.span_kind")
2121
local span_status = require("opentelemetry.trace.span_status")
2222
local local_conf = require("apisix.core.config_local").local_conf()
@@ -36,75 +36,42 @@ local _M = {
3636

3737
function _M.start(ctx, name, kind)
3838
if not enable_tracing then
39-
return
39+
return noop_span
4040
end
4141

42-
local tracing = ctx and ctx.tracing
42+
local tracing = ctx.tracing
4343
if not tracing then
44-
local root_span = span.new()
4544
tracing = tablepool.fetch("tracing", 0, 8)
4645
tracing.spans = tablepool.fetch("tracing_spans", 20, 0)
47-
tracing.root_span = root_span
48-
tracing.current_span = root_span
49-
table.insert(tracing.spans, root_span)
50-
root_span.id = 1
5146
ctx.tracing = tracing
5247
end
5348
if tracing.skip then
54-
return
49+
return noop_span
5550
end
5651

57-
local spans = tracing.spans
58-
local sp = span.new(name, kind)
59-
60-
table.insert(spans, sp)
61-
local id = #spans
62-
sp.id = id
63-
local parent = tracing.current_span
64-
if parent then
65-
sp:set_parent(parent.id)
66-
parent:append_child(id)
67-
end
68-
tracing.current_span = sp
52+
local sp = span.new(ctx, name, kind)
6953
return sp
7054
end
7155

7256

73-
local function finish_span(spans, sp, code, message)
74-
if not sp or sp.end_time then
75-
return
76-
end
77-
for _, id in ipairs(sp.child_ids or {}) do
78-
finish_span(spans, spans[id])
79-
end
80-
if code then
81-
sp:set_status(code, message)
82-
end
83-
sp:finish()
84-
end
85-
86-
87-
function _M.finish(ctx, sp, code, message)
88-
local tracing = ctx and ctx.tracing
57+
function _M.finish_all(ctx, code, message)
58+
local tracing = ctx.tracing
8959
if not tracing then
9060
return
9161
end
9262

93-
sp = sp or tracing.current_span
94-
if not sp then
95-
return
96-
end
63+
tracing.current_span:set_status(code, message)
64+
tracing.current_span:finish(ctx)
9765

98-
finish_span(tracing.spans, sp, code, message)
99-
if sp == tracing.root_span then
100-
return
66+
while tracing.current_span.parent_id do
67+
tracing.current_span = tracing.spans[tracing.current_span.parent_id]
68+
tracing.current_span:finish(ctx)
10169
end
102-
tracing.current_span = tracing.spans[sp.parent_id]
10370
end
10471

10572

10673
function _M.release(ctx)
107-
local tracing = ctx and ctx.tracing
74+
local tracing = ctx.tracing
10875
if not tracing then
10976
return
11077
end
@@ -117,18 +84,4 @@ function _M.release(ctx)
11784
end
11885

11986

120-
function _M.finish_all(ctx, code, message)
121-
local tracing = ctx and ctx.tracing
122-
if not tracing then
123-
return
124-
end
125-
126-
local spans = tracing.spans
127-
tracing.current_span = tracing.root_span
128-
for _, id in ipairs(tracing.root_span.child_ids or {}) do
129-
finish_span(spans, spans[id], code, message)
130-
end
131-
end
132-
133-
13487
return _M

apisix/utils/noop_span.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--
2+
-- Licensed to the Apache Software Foundation (ASF) under one or more
3+
-- contributor license agreements. See the NOTICE file distributed with
4+
-- this work for additional information regarding copyright ownership.
5+
-- The ASF licenses this file to You under the Apache License, Version 2.0
6+
-- (the "License"); you may not use this file except in compliance with
7+
-- the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing, software
12+
-- distributed under the License is distributed on an "AS IS" BASIS,
13+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
-- See the License for the specific language governing permissions and
15+
-- limitations under the License.
16+
--
17+
local setmetatable = setmetatable
18+
19+
local _M = {}
20+
21+
22+
local mt = {
23+
__index = _M
24+
}
25+
26+
function _M.new(ctx, name, kind)
27+
return setmetatable({}, mt)
28+
end
29+
30+
31+
function _M.set_status(self, code, message)
32+
end
33+
34+
35+
function _M.set_attributes(self, ...)
36+
end
37+
38+
39+
function _M.finish(self)
40+
end
41+
42+
43+
function _M.release(self)
44+
end
45+
46+
47+
return _M

apisix/utils/span.lua

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ local util = require("opentelemetry.util")
1919
local span_status = require("opentelemetry.trace.span_status")
2020
local setmetatable = setmetatable
2121
local table = table
22+
local new_tab = require("table.new")
2223
local select = select
2324
local pool_name = "opentelemetry_span"
2425
local update_time = ngx.update_time
@@ -36,30 +37,42 @@ local function get_time()
3637
end
3738

3839

39-
function _M.new(name, kind)
40-
local self = tablepool.fetch(pool_name, 0, 16)
41-
self.start_time = get_time()
42-
self.name = name
43-
self.kind = kind
44-
return setmetatable(self, mt)
45-
end
46-
4740

48-
function _M.append_child(self, child_id)
49-
if not self.child_ids then
50-
self.child_ids = table.new(10, 0)
41+
local function append_child(sp, child_id)
42+
if not sp.child_ids then
43+
sp.child_ids = new_tab(10, 0)
5144
end
52-
table.insert(self.child_ids, child_id)
45+
table.insert(sp.child_ids, child_id)
5346
end
5447

5548

56-
function _M.set_parent(self, parent_id)
57-
self.parent_id = parent_id
49+
local function set_parent(sp, parent_id)
50+
sp.parent_id = parent_id
5851
end
5952

6053

61-
function _M.release(self)
62-
tablepool.release(pool_name, self)
54+
function _M.new(ctx, name, kind)
55+
local tracing = ctx.tracing
56+
57+
local self = tablepool.fetch(pool_name, 0, 16)
58+
self.start_time = get_time()
59+
self.name = name
60+
self.kind = kind
61+
62+
table.insert(tracing.spans, self)
63+
local id = #tracing.spans
64+
self.id = id
65+
66+
local parent = tracing.current_span
67+
if parent then
68+
set_parent(self, parent.id)
69+
append_child(parent, id)
70+
else
71+
tracing.root_span = self
72+
end
73+
74+
ctx.tracing.current_span = self
75+
return setmetatable(self, mt)
6376
end
6477

6578

@@ -94,8 +107,18 @@ function _M.set_attributes(self, ...)
94107
end
95108

96109

97-
function _M.finish(self)
110+
function _M.finish(self, ctx)
111+
local tracing = ctx.tracing
98112
self.end_time = get_time()
113+
if not self.parent_id then
114+
return
115+
end
116+
ctx.tracing.current_span = tracing.spans[self.parent_id]
117+
end
118+
119+
120+
function _M.release(self)
121+
tablepool.release(pool_name, self)
99122
end
100123

101124

apisix/utils/upstream.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ local function parse_domain_for_nodes(nodes)
102102
core.table.insert(new_nodes, node)
103103
end
104104
end
105-
tracer.finish(ngx.ctx, span)
105+
span:finish(ngx.ctx)
106106
return new_nodes
107107
end
108108
_M.parse_domain_for_nodes = parse_domain_for_nodes

0 commit comments

Comments
 (0)