OtelTracingMiddleware
public class OtelTracingMiddleware implements MiddlewareBase
io/agentscope/core/tracing/OtelTracingMiddleware.java:77–369Otel 追踪中间件,在智能体执行流程中提供横切扩展。位于 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
构造器详情Constructor Detail
OtelTracingMiddleware
public OtelTracingMiddleware()
初始化 OtelTracingMiddleware 实例。
Creates a new instance.
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.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | Agent | 智能体(类型 `Agent`)。The agent. |
ctx | RuntimeContext | 运行时上下文(类型 `RuntimeContext`)。The ctx. |
input | AgentInput | 输入消息(类型 `AgentInput`)。The input. |
next | Function<AgentInput, Flux<AgentEvent>> | 下一个(类型 `Function >`)。The next. |
Flux<AgentEvent>io/agentscope/core/tracing/OtelTracingMiddleware.java:113–176onModelCall
public Flux<AgentEvent> onModelCall(Agent agent, RuntimeContext ctx, ModelCallInput input, Function<ModelCallInput, Flux<AgentEvent>> next)
处理模型调用事件。
Performs the on model call operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | Agent | 智能体(类型 `Agent`)。The agent. |
ctx | RuntimeContext | 运行时上下文(类型 `RuntimeContext`)。The ctx. |
input | ModelCallInput | 输入消息(类型 `ModelCallInput`)。The input. |
next | Function<ModelCallInput, Flux<AgentEvent>> | 下一个(类型 `Function >`)。The next. |
Flux<AgentEvent>io/agentscope/core/tracing/OtelTracingMiddleware.java:182–245onActing
public Flux<AgentEvent> onActing(Agent agent, RuntimeContext ctx, ActingInput input, Function<ActingInput, Flux<AgentEvent>> next)
处理执行事件。
Performs the on acting operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | Agent | 智能体(类型 `Agent`)。The agent. |
ctx | RuntimeContext | 运行时上下文(类型 `RuntimeContext`)。The ctx. |
input | ActingInput | 输入消息(类型 `ActingInput`)。The input. |
next | Function<ActingInput, Flux<AgentEvent>> | 下一个(类型 `Function >`)。The next. |
Flux<AgentEvent>io/agentscope/core/tracing/OtelTracingMiddleware.java:251–322