MCPcopy Create free account
hub / github.com/astercloud/aster / toolTracingExample

Function toolTracingExample

examples/telemetry/main.go:177–220  ·  view source on GitHub ↗

toolTracingExample 演示工具执行追踪

()

Source from the content-addressed store, hash-verified

175
176// toolTracingExample 演示工具执行追踪
177func 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 演示分布式追踪
223func distributedTracingExample() {

Callers 1

mainFunction · 0.85

Calls 11

GetGlobalTracerFunction · 0.92
WithSpanKindFunction · 0.92
WithAttributesFunction · 0.92
StringFunction · 0.92
Int64Function · 0.92
BoolFunction · 0.92
StartSpanMethod · 0.65
EndMethod · 0.65
SetAttributesMethod · 0.65
AddEventMethod · 0.65
SetStatusMethod · 0.65

Tested by

no test coverage detected