1414-- See the License for the specific language governing permissions and
1515-- limitations under the License.
1616--
17- local table = require (" apisix.core.table" )
1817local tablepool = require (" tablepool" )
1918local span = require (" apisix.utils.span" )
19+ local noop_span = require (" apisix.utils.noop_span" ).new ()
2020local span_kind = require (" opentelemetry.trace.span_kind" )
2121local span_status = require (" opentelemetry.trace.span_status" )
2222local local_conf = require (" apisix.core.config_local" ).local_conf ()
@@ -36,75 +36,42 @@ local _M = {
3636
3737function _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
7054end
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 ]
10370end
10471
10572
10673function _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)
11784end
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-
13487return _M
0 commit comments