Skip to content

feat(otel): trace sandbox deletion across services - #2432

Open
JiaxPeaceLove wants to merge 1 commit into
TencentCloud:masterfrom
JiaxPeaceLove:cs_github_test_delete_trace
Open

JiaxPeaceLove wants to merge 1 commit into
TencentCloud:masterfrom
JiaxPeaceLove:cs_github_test_delete_trace

Conversation

@JiaxPeaceLove

@JiaxPeaceLove JiaxPeaceLove commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Extend the lifecycle tracing introduced in #2165 to sandbox deletion. DELETE /sandboxes/:sandboxID continues the inbound W3C trace in CubeAPI. CubeMaster forwards the trace context to the Cubelet Destroy RPC while preserving the existing RPC deadline and cancellation behavior.

The delete spans carry the sandbox ID. The CubeMaster and Cubelet operation spans also record cube.ret_code.

Validation

Passed locally on Linux/amd64 at 354639e5:

  • CubeAPI telemetry tests, including delete route selection, inbound parentage, and outbound context injection.
  • CubeMaster telemetry and Cubelet client package tests, plus targeted sandbox deletion and error-mapping tests.
  • Cubelet destroy regression tests, including inbound gRPC parentage, cube.sandbox_id, and cube.ret_code.

KVM cluster smoke test

  • Built and deployed Linux/amd64 CubeAPI, CubeMaster, and Cubelet from 354639e5 for a temporary canary. The test used the existing runtime images with the candidate binaries and an existing test template.
  • Sandbox creation returned HTTP 201. A delete request with a sampled W3C traceparent returned HTTP 204, and the subsequent lookup returned HTTP 404.
  • Tempo trace 1893646c064f6aff851890dcbd8a85ed contained 13 spans. The CubeAPI delete span inherited the supplied parent, and the trace continued through the CubeMaster HTTP and business spans to Cubelet Destroy, including containerd Task/Kill, Task/Delete, and Task/Shutdown. The three operation spans recorded the same cube.sandbox_id, and CubeMaster and Cubelet recorded cube.ret_code=200.
  • An initial create returned scheduler code 130597 (no more resource). A retry succeeded and was used for the delete test.
  • Removed the test sandbox .

@JiaxPeaceLove
JiaxPeaceLove force-pushed the cs_github_test_delete_trace branch from f0606d6 to 78ec006 Compare November 26, 2026 01:38
Continue the inbound trace from CubeAPI through CubeMaster to Cubelet. Record the sandbox ID and business return code while preserving the existing Destroy RPC timeout and cancellation behavior.

Signed-off-by: jiaxu <18910286396@163.com>
@JiaxPeaceLove
JiaxPeaceLove force-pushed the cs_github_test_delete_trace branch from 78ec006 to 354639e Compare November 26, 2026 01:41
RetMsg: errorcode.ErrorCode_Success.String(),
},
}
ctx, span := telemetry.StartIfTraced(ctx, "cubemaster.sandbox.delete")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: span name should be a constant in the telemetry package. Every other CubeMaster span name is declared in pkg/base/telemetry/telemetry.go (SpanCreate, SpanSandboxPausePrepare, …), and this same PR adds telemetry.SpanDelete for the Cubelet side. Please add e.g. SpanSandboxDelete = "cubemaster.sandbox.delete" to the Span* const block and use it here, so the name is discoverable and greppable like the rest.

if rsp != nil && rsp.Ret != nil {
code = rsp.Ret.RetCode
}
telemetry.EndWithCode(span, code)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the span only covers the enqueue for async deletes. When req.Sync == false, DestroySandbox returns right after task.AddAsyncTask(t) below, so this defer ends the span with cube.ret_code = 200 before anything is deleted. The Cubelet span raised later by DestroySandboxTaskHandler (pkg/task/handlers.go:259) is still linked (it inherits the span context through t.Ctx), but it starts after its parent has ended and a real destroy failure is not reflected on the master span.

CubeAPI always sends sync: true, so this only affects non-CubeAPI callers. If that trade-off is intended, a short comment on why the span stops at enqueue would prevent it being read as an end-to-end delete span; otherwise end the span in the task handler for the async branch.

}

func (s *service) Destroy(ctx context.Context, req *cubebox.DestroyCubeSandboxRequest) (*cubebox.DestroyCubeSandboxResponse, error) {
ctx, span := telemetry.StartIfTraced(telemetry.ExtractGRPC(ctx), telemetry.SpanDelete,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: StartIfTraced diverges from the other Cubelet RPC root spans. Every instrumented Cubelet RPC root uses unconditional telemetry.Start — SpanCreate (service.go:337), SpanRollback (rollback.go:90), SpanPause (update.go:45), SpanTemplateCommit (template_ops.go:43), SpanImageCreate (images/service.go:217) — and StartIfTraced is otherwise reserved for stage spans.

With this form, a Cubelet that has tracing enabled but receives a Destroy without a traceparent (CubeMaster tracing disabled/misconfigured, or a direct/internal caller) emits no cubelet.sandbox.delete span at all, while create/rollback/pause still produce a root span. If that asymmetry is deliberate to avoid root spans for frequent internal cleanup deletes, a comment would help; otherwise use telemetry.Start(telemetry.ExtractGRPC(ctx), …) to match the siblings.

rpcCtx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return c.Destroy(ctx, req)
return c.Destroy(telemetry.InjectGRPC(telemetry.DetachTrace(rpcCtx, ctx)), req)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the new CubeMaster → Cubelet trace link has no test. This line is the only thing carrying the trace from cubemaster.sandbox.delete onto the Cubelet Destroy RPC, and the new cubemaster.sandbox.delete span in sandbox_remove.go (attributes, parent, cube.ret_code, panic path) is likewise untested. The diff adds tests for the Gin middleware, the CubeAPI entry route and the Cubelet handler, but nothing asserts this injection or the master span — while the sibling pause/resume/create spans each have dedicated span tests (sandbox_update_trace_test.go, sandbox_create_test.go, queue_span_test.go). A small test asserting the outgoing traceparent parent matches the caller span (and that the span reports 200 / a failure code) would fit the existing pattern.

@cubesandboxbot

Copy link
Copy Markdown

AI review — automated, not a human approval

Reviewed the prefetched diff against the base branch (master @ 289b641) and read the surrounding code on each touched call path. I did not build or run the test suites (no shell access in this review), so everything below is static analysis.

What the change does

Extends the lifecycle tracing from #2165 to sandbox deletion:

  • CubeAPI — DELETE /sandboxes/:sandboxID now gets an entry span and injects the W3C context into the CubeMaster DELETE /cube/sandbox request (cubemaster/mod.rs).
  • CubeMaster — DELETE /cube/sandbox is added to tracedRoutes (and the Gin middleware drops its POST-only gate), DestroySandbox opens cubemaster.sandbox.delete, and DestroyWithTimeout forwards the caller's trace onto the deadline-independent Cubelet RPC via DetachTrace/InjectGRPC.
  • Cubelet — Destroy extracts the incoming traceparent and opens cubelet.sandbox.delete, recording cube.ret_code.

Resulting chain for the CubeAPI delete path: DELETE /sandboxes/:sandboxID (cube-api) → DELETE /cube/sandbox (cubemaster) → cubemaster.sandbox.delete → cubelet.sandbox.delete. Because kill_sandbox sends sync: Some(true), all four spans are on one synchronous path, so the nesting is temporally sound.

Checks that came out clean

  • Success-code semantics: EndWithCode marks any non-200 code as an error. CubeMaster errorcode.ErrorCode_Success is 200 and Cubelet telemetry.SuccessCode is the proto ErrorCode_Success (200), so successful deletes are not mislabeled. (ErrorCode_OK is 0 but ret.Err(ErrorCode_OK, …) returns nil, so Destroy cannot return 0 here.)
  • Cubelet Destroy defer ordering: the new span defer is registered before defer recov.HandleCrash, so LIFO runs HandleCrash first (which sets RetCode = Unknown and swallows the panic), then the span defer records the final code. rsp is never reassigned in the function, and the generated getters are nil-safe, so rsp.GetRet().GetRetCode() is safe on every return path.
  • Deadline behavior preserved: DestroyWithTimeout still derives the RPC context from context.Background() with the same timeout and only copies the trace parent, so caller cancellation still cannot abort an in-flight delete (same shape as UpdateWithTimeout / CreateImage).
  • Route matching: MatchedPath is available to the top-level telemetry::layer (the existing POST routes already depend on this), so the DELETE span is created for /sandboxes/:sandboxID only; DELETE /templates/:templateID stays untraced, as the tests assert.
  • Trace reaches CubeMaster's service layer: deleteSandboxGinHandler passes c.Request, whose context the middleware replaced, so DestroySandbox sees the route span.

Findings

  1. Hardcoded span name (nit) — CubeMaster/pkg/service/sandbox/sandbox_remove.go:45 uses the literal "cubemaster.sandbox.delete". Every other CubeMaster span name lives in pkg/base/telemetry/telemetry.go (SpanCreate, SpanSandboxPausePrepare, …), and this same PR adds telemetry.SpanDelete on the Cubelet side. Please add e.g. SpanSandboxDelete to the telemetry const block and use it.
  2. Async-delete span semantics (nit) — sandbox_remove.go:56: for sync: false requests DestroySandbox returns right after task.AddAsyncTask, so cubemaster.sandbox.delete ends with cube.ret_code = 200 before the sandbox is actually deleted. The Cubelet span raised later by DestroySandboxTaskHandler is still linked (via t.Ctx), but it starts after its parent has ended and any real destroy failure is not reflected on the master span. CubeAPI always sends sync: true, so this only affects non-CubeAPI callers; if that trade-off is intended, a short comment would help.
  3. StartIfTraced diverges from the Cubelet convention (nit) — Cubelet/services/cubebox/service.go:746 uses StartIfTraced, while every other Cubelet RPC root span uses unconditional Start (SpanCreate service.go:337, SpanRollback rollback.go:90, SpanPause update.go:45, SpanTemplateCommit template_ops.go:43, SpanImageCreate images/service.go:217); StartIfTraced is otherwise only used for stages. Consequence: with tracing enabled on Cubelet but no inbound trace (CubeMaster tracing off, or a direct internal destroy), create/rollback/pause each produce a root span but destroy produces none. If this is deliberate (to avoid root spans for frequent internal cleanup deletes), please note it; otherwise use telemetry.Start(telemetry.ExtractGRPC(ctx), …).
  4. Test coverage of the new CubeMaster pieces (nit) — the diff adds tests for the Gin middleware, the CubeAPI entry route, and the Cubelet handler, but nothing exercises the new cubemaster.sandbox.delete span (attributes, parent, cube.ret_code, including the panic path) or the DestroyWithTimeout → Cubelet traceparent injection (CubeMaster/pkg/cubelet/actions.go:45). Those two are the only new links between CubeMaster and its neighbours; the sibling pause/resume/create spans all have dedicated span tests (sandbox_update_trace_test.go, sandbox_create_test.go, queue_span_test.go), so a similar test here would match the existing pattern.
  5. Tracing guide not updated (nit) — docs/guide/tracing.md and docs/zh/guide/tracing.md enumerate traced workflows in their "What is traced" tables (creation, image-to-template, commit, snapshot, rollback, pause/resume) and describe the CubeMaster-bypass entry span as POST /cube/.... Sandbox deletion is now a traced workflow but is absent from both docs; please add a row (entry DELETE /sandboxes/:sandboxID) to both language versions.

Process notes

  • The PR body states cluster validation is still pending and the commit awaits the contributor's DCO sign-off — both should be resolved before merge.
  • No functional defects found in the propagation or span-lifecycle logic; the findings above are consistency, coverage, and documentation items.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants