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

Function llmTracingExample

examples/telemetry/main.go:139–174  ·  view source on GitHub ↗

llmTracingExample 演示 LLM 调用追踪

()

Source from the content-addressed store, hash-verified

137
138// llmTracingExample 演示 LLM 调用追踪
139func llmTracingExample() {
140 ctx := context.Background()
141 tracer := telemetry.GetGlobalTracer()
142
143 // 创建父 span
144 ctx, parentSpan := tracer.StartSpan(ctx, "agent.process")
145 defer parentSpan.End()
146
147 // 创建 LLM 调用的子 span
148 _, span := tracer.StartSpan(
149 ctx,
150 "llm.request",
151 telemetry.WithSpanKind(telemetry.SpanKindClient),
152 telemetry.WithAttributes(
153 telemetry.String(telemetry.AttrLLMProvider, "anthropic"),
154 telemetry.String(telemetry.AttrLLMModel, "claude-3-5-sonnet-20241022"),
155 telemetry.Int(telemetry.AttrLLMMaxTokens, 4096),
156 ),
157 )
158 defer span.End()
159
160 // 模拟 API 调用
161 time.Sleep(200 * time.Millisecond)
162
163 // 记录 token 使用情况
164 span.SetAttributes(
165 telemetry.Int(telemetry.AttrLLMInputTokens, 150),
166 telemetry.Int(telemetry.AttrLLMOutputTokens, 80),
167 telemetry.Int(telemetry.AttrLLMTotalTokens, 230),
168 )
169
170 span.AddEvent("llm.response.received")
171 span.SetStatus(telemetry.StatusCodeOK, "LLM request completed")
172
173 fmt.Println("✅ LLM tracing completed")
174}
175
176// toolTracingExample 演示工具执行追踪
177func toolTracingExample() {

Callers 1

mainFunction · 0.85

Calls 10

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

Tested by

no test coverage detected