AS AgentScopeAPI 手册 / API Reference

OtelTracingMiddleware

public class OtelTracingMiddleware implements MiddlewareBase
源码Source io/agentscope/core/tracing/OtelTracingMiddleware.java:77–369

Otel 追踪中间件,在智能体执行流程中提供横切扩展。位于 io.agentscope.core.tracing 包,实现 MiddlewareBase。核心成员包括 onAgent、onModelCall、onActing、OtelTracingMiddleware。

Middleware that adds OpenTelemetry tracing to the agent lifecycle.

Produces spans for:

  • invoke_agent <name> — wraps the entire reply

  • chat <model> — wraps each model API call

  • execute_tool <name> — wraps each tool execution

Model-call spans report input/output token usage and the available cache, reasoning, and tool-prompt token breakdowns using GenAI semantic-convention attributes. The tool-prompt breakdown uses an AgentScope-specific attribute because it is not yet defined by the GenAI convention.

Context propagation across Reactor's asynchronous chain (including thread hops via publishOn / subscribeOn) is handled by ContextPropagationOperator The global lift hook is registered once on class load, so child spans see the correct parent regardless of which thread the signal lands on.

When no OTel SDK is configured (only the default no-op provider is active), every hook short-circuits with near-zero overhead.

Usage:

`ReActAgent agent = ReActAgent.builder()
.name("assistant")
.model(model)
.middleware(new OtelTracingMiddleware())
.build();
`

构造器摘要Constructor Summary

构造器Constructor说明Description
public OtelTracingMiddleware()
初始化 `OtelTracingMiddleware` 实例。
Creates a new instance.

方法摘要Method Summary

方法Method说明Description
public Flux<AgentEvent> onAgent(Agent agent, RuntimeContext ctx, AgentInput input, Function<AgentInput, Flux<AgentEvent>> next)
处理智能体事件。
Performs the on agent operation.
public Flux<AgentEvent> onModelCall(Agent agent, RuntimeContext ctx, ModelCallInput input, Function<ModelCallInput, Flux<AgentEvent>> next)
处理模型调用事件。
Performs the on model call operation.
public Flux<AgentEvent> onActing(Agent agent, RuntimeContext ctx, ActingInput input, Function<ActingInput, Flux<AgentEvent>> next)
处理执行事件。
Performs the on acting operation.

构造器详情Constructor Detail

OtelTracingMiddleware

public OtelTracingMiddleware()

初始化 OtelTracingMiddleware 实例。

Creates a new instance.

源码Source io/agentscope/core/tracing/OtelTracingMiddleware.java:94–103

方法详情Method Detail

onAgent

public Flux<AgentEvent> onAgent(Agent agent, RuntimeContext ctx, AgentInput input, Function<AgentInput, Flux<AgentEvent>> next)

处理智能体事件。

Performs the on agent operation.

参数Parameters
名称Name类型Type说明Description
agentAgent智能体(类型 `Agent`)。The agent.
ctxRuntimeContext运行时上下文(类型 `RuntimeContext`)。The ctx.
inputAgentInput输入消息(类型 `AgentInput`)。The input.
nextFunction<AgentInput, Flux<AgentEvent>>下一个(类型 `Function >`)。The next.
返回Returns Flux<AgentEvent>
返回Flux。Returns flux.
源码Source io/agentscope/core/tracing/OtelTracingMiddleware.java:113–176

onModelCall

public Flux<AgentEvent> onModelCall(Agent agent, RuntimeContext ctx, ModelCallInput input, Function<ModelCallInput, Flux<AgentEvent>> next)

处理模型调用事件。

Performs the on model call operation.

参数Parameters
名称Name类型Type说明Description
agentAgent智能体(类型 `Agent`)。The agent.
ctxRuntimeContext运行时上下文(类型 `RuntimeContext`)。The ctx.
inputModelCallInput输入消息(类型 `ModelCallInput`)。The input.
nextFunction<ModelCallInput, Flux<AgentEvent>>下一个(类型 `Function >`)。The next.
返回Returns Flux<AgentEvent>
返回Flux。Returns flux.
源码Source io/agentscope/core/tracing/OtelTracingMiddleware.java:182–245

onActing

public Flux<AgentEvent> onActing(Agent agent, RuntimeContext ctx, ActingInput input, Function<ActingInput, Flux<AgentEvent>> next)

处理执行事件。

Performs the on acting operation.

参数Parameters
名称Name类型Type说明Description
agentAgent智能体(类型 `Agent`)。The agent.
ctxRuntimeContext运行时上下文(类型 `RuntimeContext`)。The ctx.
inputActingInput输入消息(类型 `ActingInput`)。The input.
nextFunction<ActingInput, Flux<AgentEvent>>下一个(类型 `Function >`)。The next.
返回Returns Flux<AgentEvent>
返回Flux。Returns flux.
源码Source io/agentscope/core/tracing/OtelTracingMiddleware.java:251–322

继承成员Inherited Members

继承自Methods inherited from MiddlewareBase
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.