Skip to main content

zerolaunch_plugin_sdk_rust/
trace.rs

1use std::future::Future;
2use tracing::Instrument;
3use zerolaunch_plugin_api::PluginContext;
4
5/// 根据 PluginContext 创建 tracing span。
6pub fn span_for(ctx: &PluginContext) -> tracing::Span {
7    tracing::info_span!(
8        "plugin",
9        trace_id = %ctx.trace_id,
10        plugin_id = ?ctx.plugin_id,
11        query_id = ?ctx.query_id,
12    )
13}
14
15/// 同步闭包包装:在 span 内执行同步代码。
16pub fn with_trace<R>(ctx: &PluginContext, f: impl FnOnce() -> R) -> R {
17    let span = span_for(ctx);
18    let _enter = span.enter();
19    f()
20}
21
22/// 异步 Future 包装:在 span 内执行异步代码。
23pub fn instrument<F: Future>(ctx: &PluginContext, fut: F) -> tracing::instrument::Instrumented<F> {
24    fut.instrument(span_for(ctx))
25}