Skip to content

Commit d3f9bd4

Browse files
committed
fix test
1 parent b9f3215 commit d3f9bd4

File tree

5 files changed

+190
-257
lines changed

5 files changed

+190
-257
lines changed

apisix/plugins/opentelemetry.lua

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,7 @@ function _M.rewrite(conf, api_ctx)
388388
end
389389

390390

391-
local function create_child_span(tracer, parent_span_ctx, spans, span_idx)
392-
local span = spans[span_idx]
391+
local function create_child_span(tracer, parent_span_ctx, spans, span)
393392
if not span or span.finished then
394393
return
395394
end
@@ -402,7 +401,7 @@ local function create_child_span(tracer, parent_span_ctx, spans, span_idx)
402401
new_span.start_time = span.start_time
403402

404403
for _, idx in ipairs(span.child_ids or {}) do
405-
create_child_span(tracer, new_span_ctx, spans, idx)
404+
create_child_span(tracer, new_span_ctx, spans, spans[idx])
406405
end
407406
if span.status then
408407
new_span:set_status(span.status.code, span.status.message)
@@ -443,8 +442,10 @@ local function inject_core_spans(root_span_ctx, api_ctx, conf)
443442
return
444443
end
445444
span.start_time = tracing.spans[1].start_time
446-
for i, _ in ipairs(tracing.spans or {}) do
447-
create_child_span(tracer, root_span_ctx, tracing.spans, i)
445+
local root_span = tracing.root_span
446+
local spans = tracing.spans
447+
for _, idx in ipairs(root_span.child_ids or {}) do
448+
create_child_span(tracer, root_span_ctx, spans, spans[idx])
448449
end
449450
end
450451

@@ -463,15 +464,10 @@ function _M.log(conf, api_ctx)
463464
end
464465

465466
inject_core_spans(ctx, api_ctx, conf)
466-
span:set_attributes(attr.int("http.status_code", upstream_status))
467+
span:set_attributes(attr.int("http.status_code", upstream_status),
468+
attr.int("http.response.status_code", upstream_status))
467469
update_time()
468470
span:finish()
469-
if ngx.ctx._apisix_spans then
470-
for _, sp in ipairs(ngx.ctx._apisix_spans) do
471-
sp:release()
472-
end
473-
ngx.ctx._apisix_spans = nil
474-
end
475471
end
476472
end
477473

apisix/tracer.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
--
1717
local table = require("apisix.core.table")
1818
local tablepool = require("tablepool")
19-
local stack = require("apisix.utils.stack")
2019
local span = require("apisix.utils.span")
2120
local span_kind = require("opentelemetry.trace.span_kind")
2221
local span_status = require("opentelemetry.trace.span_status")

apisix/utils/span.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ function _M.new(name, kind)
4141
self.start_time = get_time()
4242
self.name = name
4343
self.kind = kind
44-
self.end_time = 0
4544
self.status = nil
4645
self.dead = false
4746
return setmetatable(self, mt)
@@ -50,12 +49,17 @@ end
5049

5150
function _M.append_child(self, child_id)
5251
if not self.child_ids then
53-
self.child_ids = {}
52+
self.child_ids = table.new(10, 0)
5453
end
5554
table.insert(self.child_ids, child_id)
5655
end
5756

5857

58+
function _M.set_parent(self, parent_id)
59+
self.parent_id = parent_id
60+
end
61+
62+
5963
function _M.release(self)
6064
tablepool.release(pool_name, self)
6165
end

docs/en/latest/plugins/opentelemetry.md

Lines changed: 88 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -167,153 +167,120 @@ You should receive an `HTTP/1.1 200 OK` response.
167167
In OpenTelemetry collector's log, you should see information similar to the following:
168168

169169
```text
170-
2024-02-18T17:14:03.825Z info ResourceSpans #0
170+
info ResourceSpans #0
171+
Resource SchemaURL:
172+
Resource attributes:
173+
-> telemetry.sdk.language: Str(lua)
174+
-> telemetry.sdk.name: Str(opentelemetry-lua)
175+
-> telemetry.sdk.version: Str(0.1.1)
176+
-> hostname: Str(RC)
177+
-> service.name: Str(APISIX)
171178
ScopeSpans #0
172-
ScopeSpans SchemaURL:
173-
InstrumentationScope opentelemetry-lua
179+
ScopeSpans SchemaURL:
180+
InstrumentationScope opentelemetry-lua
174181
Span #0
175-
Trace ID : 95a1644afaaf65e1f0193b1f193b990a
176-
Parent ID : 905f850f13e32bfb
177-
ID : 5a3835b61110d942
182+
Trace ID : 6cd24a1d178f836f43de423a45ead766
183+
Parent ID : b125e88a4ed7e409
184+
ID : 5e1d6b61c31220c9
178185
Name : http_router_match
179186
Kind : Internal
180-
Start time : 2025-10-24 06:58:04.430430976 +0000 UTC
181-
End time : 2025-10-24 06:58:04.431542016 +0000 UTC
187+
Start time : 2026-02-04 00:55:09.468541952 +0000 UTC
188+
End time : 2026-02-04 00:55:09.468577024 +0000 UTC
182189
Status code : Unset
183-
Status message :
190+
Status message :
191+
DroppedAttributesCount: 0
192+
DroppedEventsCount: 0
193+
DroppedLinksCount: 0
184194
Span #1
185-
Trace ID : 95a1644afaaf65e1f0193b1f193b990a
186-
Parent ID : 905f850f13e32bfb
187-
ID : 4ab25e2b92f394e1
188-
Name : resolve_dns
189-
Kind : Internal
190-
Start time : 2025-10-24 06:58:04.432521984 +0000 UTC
191-
End time : 2025-10-24 06:58:04.44903296 +0000 UTC
195+
Trace ID : 6cd24a1d178f836f43de423a45ead766
196+
Parent ID : 8b00441884e3908f
197+
ID : b125e88a4ed7e409
198+
Name : apisix.phase.access
199+
Kind : Server
200+
Start time : 2026-02-04 00:55:09.468467968 +0000 UTC
201+
End time : 2026-02-04 00:55:09.469076992 +0000 UTC
192202
Status code : Unset
193-
Status message :
203+
Status message :
204+
DroppedAttributesCount: 0
205+
DroppedEventsCount: 0
206+
DroppedLinksCount: 0
194207
Span #2
195-
Trace ID : 95a1644afaaf65e1f0193b1f193b990a
196-
Parent ID : 905f850f13e32bfb
197-
ID : 3620c0f05dd2be4f
198-
Name : apisix.phase.header_filter
208+
Trace ID : 6cd24a1d178f836f43de423a45ead766
209+
Parent ID : 8b00441884e3908f
210+
ID : f603a09f54520f8b
211+
Name : resolve_dns
199212
Kind : Internal
200-
Start time : 2025-10-24 06:58:06.960481024 +0000 UTC
201-
End time : 2025-10-24 06:58:06.960510976 +0000 UTC
213+
Start time : 2026-02-04 00:55:09.469084928 +0000 UTC
214+
End time : 2026-02-04 00:55:09.469104896 +0000 UTC
202215
Status code : Unset
203-
Status message :
216+
Status message :
217+
DroppedAttributesCount: 0
218+
DroppedEventsCount: 0
219+
DroppedLinksCount: 0
204220
Span #3
205-
Trace ID : 95a1644afaaf65e1f0193b1f193b990a
206-
Parent ID : 4c5f3476f62a7e8a
207-
ID : a9bfad7bb6986e41
208-
Name : apisix.phase.body_filter
209-
Kind : Internal
210-
Start time : 2025-10-24 06:58:06.960579072 +0000 UTC
211-
End time : 2025-10-24 06:58:06.96059008 +0000 UTC
221+
Trace ID : 6cd24a1d178f836f43de423a45ead766
222+
Parent ID : 8b00441884e3908f
223+
ID : 303694d8fc044ab2
224+
Name : apisix.phase.header_filter
225+
Kind : Server
226+
Start time : 2026-02-04 00:55:09.470095872 +0000 UTC
227+
End time : 2026-02-04 00:55:09.470138112 +0000 UTC
212228
Status code : Unset
213-
Status message :
229+
Status message :
230+
DroppedAttributesCount: 0
231+
DroppedEventsCount: 0
232+
DroppedLinksCount: 0
214233
Span #4
215-
Trace ID : 95a1644afaaf65e1f0193b1f193b990a
216-
Parent ID : b2994675df6baa83
217-
ID : 26705f9c47584a5b
218-
Name : apisix.phase.delayed_body_filter.opentelemetry
219-
Kind : Internal
220-
Start time : 2025-10-24 06:58:06.960613888 +0000 UTC
221-
End time : 2025-10-24 06:58:06.960687104 +0000 UTC
222-
Status code : Unset
223-
Status message :
224-
Span #5
225-
Trace ID : 95a1644afaaf65e1f0193b1f193b990a
226-
Parent ID : 4c5f3476f62a7e8a
227-
ID : b2994675df6baa83
228-
Name : apisix.phase.delayed_body_filter
229-
Kind : Internal
230-
Start time : 2025-10-24 06:58:06.96059904 +0000 UTC
231-
End time : 2025-10-24 06:58:06.960692992 +0000 UTC
232-
Status code : Unset
233-
Status message :
234-
Span #6
235-
Trace ID : 95a1644afaaf65e1f0193b1f193b990a
236-
Parent ID : 905f850f13e32bfb
237-
ID : 4c5f3476f62a7e8a
234+
Trace ID : 6cd24a1d178f836f43de423a45ead766
235+
Parent ID : 8b00441884e3908f
236+
ID : 078f82f64325c684
238237
Name : apisix.phase.body_filter
239238
Kind : Server
240-
Start time : 2025-10-24 06:58:06.96056704 +0000 UTC
241-
End time : 2025-10-24 06:58:06.960698112 +0000 UTC
242-
Status code : Unset
243-
Status message :
244-
Span #7
245-
Trace ID : 95a1644afaaf65e1f0193b1f193b990a
246-
Parent ID : 2024d73d32cbd81b
247-
ID : 223c64fb691a24e8
248-
Name : apisix.phase.body_filter
249-
Kind : Internal
250-
Start time : 2025-10-24 06:58:06.961624064 +0000 UTC
251-
End time : 2025-10-24 06:58:06.961635072 +0000 UTC
239+
Start time : 2026-02-04 00:55:09.470141952 +0000 UTC
240+
End time : 2026-02-04 00:55:09.47021696 +0000 UTC
252241
Status code : Unset
253-
Status message :
254-
Span #8
255-
Trace ID : 95a1644afaaf65e1f0193b1f193b990a
256-
Parent ID : fd193dd24c618f60
257-
ID : 8729ad6e0d94a23b
258-
Name : apisix.phase.delayed_body_filter.opentelemetry
259-
Kind : Internal
260-
Start time : 2025-10-24 06:58:06.961648896 +0000 UTC
261-
End time : 1970-01-01 00:00:00 +0000 UTC
262-
Status code : Unset
263-
Status message :
264-
Span #9
265-
Trace ID : 95a1644afaaf65e1f0193b1f193b990a
266-
Parent ID : 2024d73d32cbd81b
267-
ID : fd193dd24c618f60
268-
Name : apisix.phase.delayed_body_filter
242+
Status message :
243+
DroppedAttributesCount: 0
244+
DroppedEventsCount: 0
245+
DroppedLinksCount: 0
246+
Span #5
247+
Trace ID : 6cd24a1d178f836f43de423a45ead766
248+
Parent ID : 8b00441884e3908f
249+
ID : fb6fba6b28496598
250+
Name : apisix.phase.log.plugins.opentelemetry
269251
Kind : Internal
270-
Start time : 2025-10-24 06:58:06.961641984 +0000 UTC
271-
End time : 1970-01-01 00:00:00 +0000 UTC
252+
Start time : 2026-02-04 00:55:09.470256896 +0000 UTC
253+
End time : 2026-02-04 00:55:09.479666944 +0000 UTC
272254
Status code : Unset
273-
Status message :
274-
Span #10
275-
Trace ID : 95a1644afaaf65e1f0193b1f193b990a
276-
Parent ID : 905f850f13e32bfb
277-
ID : 2024d73d32cbd81b
278-
Name : apisix.phase.body_filter
279-
Kind : Server
280-
Start time : 2025-10-24 06:58:06.960980992 +0000 UTC
281-
End time : 1970-01-01 00:00:00 +0000 UTC
282-
Status code : Unset
283-
Status message :
284-
Span #11
285-
Trace ID : 95a1644afaaf65e1f0193b1f193b990a
286-
Parent ID : cfb0b4603dc2e385
287-
ID : 905f850f13e32bfb
288-
Name : apisix.phase.access
289-
Kind : Server
290-
Start time : 2025-10-24 06:58:04.427932928 +0000 UTC
291-
End time : 1970-01-01 00:00:00 +0000 UTC
292-
Status code : Unset
293-
Status message :
294-
Span #12
295-
Trace ID : 95a1644afaaf65e1f0193b1f193b990a
296-
Parent ID :
297-
ID : cfb0b4603dc2e385
298-
Name : GET /headers
255+
Status message :
256+
DroppedAttributesCount: 0
257+
DroppedEventsCount: 0
258+
DroppedLinksCount: 0
259+
Span #6
260+
Trace ID : 6cd24a1d178f836f43de423a45ead766
261+
Parent ID :
262+
ID : 8b00441884e3908f
263+
Name : GET /anything
299264
Kind : Server
300-
Start time : 2025-10-24 06:58:04.432427008 +0000 UTC
301-
End time : 2025-10-24 06:58:06.962299904 +0000 UTC
265+
Start time : 2026-02-04 00:55:09.468459008 +0000 UTC
266+
End time : 2026-02-04 00:55:09.480956928 +0000 UTC
302267
Status code : Unset
303-
Status message :
268+
Status message :
269+
DroppedAttributesCount: 0
270+
DroppedEventsCount: 0
271+
DroppedLinksCount: 0
304272
Attributes:
305-
-> net.host.name: Str(127.0.0.1)
273+
-> net.host.name: Str(localhost)
306274
-> http.method: Str(GET)
307275
-> http.scheme: Str(http)
308-
-> http.target: Str(/headers)
309-
-> http.user_agent: Str(curl/8.16.0)
276+
-> http.target: Str(/anything)
277+
-> http.user_agent: Str(curl/7.81.0)
310278
-> apisix.route_id: Str(otel-tracing-route)
311279
-> apisix.route_name: Empty()
312-
-> http.route: Str(/headers)
280+
-> http.route: Str(/anything)
313281
-> http.status_code: Int(200)
314-
{"resource": {"service.instance.id": "5006c483-d64c-4d1d-87ac-edb037ba3669", "service.name": "otelcol-contrib", "service.version": "0.138.0"}, "otelcol.component.id": "debug", "otelcol.component.kind": "exporter", "otelcol.signal": "traces"}
315-
2025-10-24T06:58:13.893Z info Metrics {"resource": {"service.instance.id": "5006c483-d64c-4d1d-87ac-edb037ba3669", "service.name": "otelcol-contrib", "service.version": "0.138.0"}, "otelcol.component.id": "debug", "otelcol.component.kind": "exporter", "otelcol.signal": "metrics", "resource metrics": 1, "metrics": 25, "data points": 26}
316-
2025-10-24T06:58:13.893Z info ResourceMetrics #0
282+
-> http.response.status_code: Int(200)
283+
{"resource": {"service.instance.id": "6f63899f-6fda-42db-a18b-b0892a77958a", "service.name": "otelcol-contrib", "service.version": "0.144.0"}, "otelcol.component.id": "debug", "otelcol.component.kind": "exporter", "otelcol.signal": "traces"}
317284
```
318285

319286
To visualize these traces, you can export your telemetry to backend Services, such as Zipkin and Prometheus. See [exporters](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter) for more details.

0 commit comments

Comments
 (0)