recordCompletionEvent 记录完成事件
(span telemetry.Span, resp *ModelResponse)
| 278 | |
| 279 | // recordCompletionEvent 记录完成事件 |
| 280 | func (m *TelemetryMiddleware) recordCompletionEvent(span telemetry.Span, resp *ModelResponse) { |
| 281 | attrs := []telemetry.Attribute{} |
| 282 | |
| 283 | // 记录响应内容长度(不记录具体内容) |
| 284 | if resp.Message.Content != "" { |
| 285 | attrs = append(attrs, telemetry.Int("content_length", len(resp.Message.Content))) |
| 286 | } |
| 287 | |
| 288 | // 记录工具调用数量 |
| 289 | if len(resp.Message.ContentBlocks) > 0 { |
| 290 | toolCallCount := 0 |
| 291 | for _, block := range resp.Message.ContentBlocks { |
| 292 | // 使用类型断言检查是否为 ToolUseBlock |
| 293 | if _, ok := block.(*types.ToolUseBlock); ok { |
| 294 | toolCallCount++ |
| 295 | } |
| 296 | } |
| 297 | if toolCallCount > 0 { |
| 298 | attrs = append(attrs, telemetry.Int("tool_call_count", toolCallCount)) |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | if len(attrs) > 0 { |
| 303 | span.AddEvent(genai.EventCompletion, attrs...) |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // extractTokenUsage 从响应 Metadata 中提取 token 使用情况 |
| 308 | func (m *TelemetryMiddleware) extractTokenUsage(span telemetry.Span, metadata map[string]any) { |
no test coverage detected