toolTracingExample 演示工具执行追踪
()
| 175 | |
| 176 | // toolTracingExample 演示工具执行追踪 |
| 177 | func toolTracingExample() { |
| 178 | ctx := context.Background() |
| 179 | tracer := telemetry.GetGlobalTracer() |
| 180 | |
| 181 | // 创建父 span |
| 182 | ctx, parentSpan := tracer.StartSpan(ctx, "agent.execute-tools") |
| 183 | defer parentSpan.End() |
| 184 | |
| 185 | // 模拟执行多个工具 |
| 186 | tools := []string{"Bash", "Read", "WebFetch"} |
| 187 | |
| 188 | for _, toolName := range tools { |
| 189 | _, span := tracer.StartSpan( |
| 190 | ctx, |
| 191 | "tool.execute", |
| 192 | telemetry.WithSpanKind(telemetry.SpanKindInternal), |
| 193 | telemetry.WithAttributes( |
| 194 | telemetry.String(telemetry.AttrToolName, toolName), |
| 195 | telemetry.String(telemetry.AttrToolCallID, "call_"+toolName), |
| 196 | ), |
| 197 | ) |
| 198 | |
| 199 | // 模拟工具执行 |
| 200 | startTime := time.Now() |
| 201 | time.Sleep(50 * time.Millisecond) |
| 202 | duration := time.Since(startTime) |
| 203 | |
| 204 | // 记录执行结果 |
| 205 | span.SetAttributes( |
| 206 | telemetry.Int64(telemetry.AttrToolDuration, duration.Milliseconds()), |
| 207 | telemetry.Bool(telemetry.AttrToolSuccess, true), |
| 208 | ) |
| 209 | |
| 210 | span.AddEvent("tool.completed", |
| 211 | telemetry.String("tool.result", "success"), |
| 212 | ) |
| 213 | |
| 214 | span.SetStatus(telemetry.StatusCodeOK, "Tool executed successfully") |
| 215 | span.End() |
| 216 | } |
| 217 | |
| 218 | parentSpan.SetStatus(telemetry.StatusCodeOK, "All tools executed") |
| 219 | fmt.Println("✅ Tool tracing completed") |
| 220 | } |
| 221 | |
| 222 | // distributedTracingExample 演示分布式追踪 |
| 223 | func distributedTracingExample() { |
no test coverage detected