setErrorTypeAttribute 设置错误类型属性
(span telemetry.Span, err error)
| 348 | |
| 349 | // setErrorTypeAttribute 设置错误类型属性 |
| 350 | func (m *TelemetryMiddleware) setErrorTypeAttribute(span telemetry.Span, err error) { |
| 351 | errStr := err.Error() |
| 352 | var errType string |
| 353 | |
| 354 | // 简单的错误类型推断 |
| 355 | switch { |
| 356 | case contains(errStr, "timeout"): |
| 357 | errType = genai.ErrorTypeTimeout |
| 358 | case contains(errStr, "rate limit") || contains(errStr, "429"): |
| 359 | errType = genai.ErrorTypeRateLimit |
| 360 | case contains(errStr, "authentication") || contains(errStr, "401"): |
| 361 | errType = genai.ErrorTypeAuthentication |
| 362 | case contains(errStr, "permission") || contains(errStr, "403"): |
| 363 | errType = genai.ErrorTypePermission |
| 364 | case contains(errStr, "not found") || contains(errStr, "404"): |
| 365 | errType = genai.ErrorTypeNotFound |
| 366 | case contains(errStr, "context length") || contains(errStr, "too long"): |
| 367 | errType = genai.ErrorTypeContextLengthExceeded |
| 368 | case contains(errStr, "content filter") || contains(errStr, "blocked"): |
| 369 | errType = genai.ErrorTypeContentFilter |
| 370 | case contains(errStr, "500") || contains(errStr, "server error"): |
| 371 | errType = genai.ErrorTypeServerError |
| 372 | default: |
| 373 | errType = "unknown" |
| 374 | } |
| 375 | |
| 376 | span.SetAttributes(telemetry.String(genai.AttrErrorType, errType)) |
| 377 | } |
| 378 | |
| 379 | // contains 检查字符串是否包含子串 (case-insensitive) |
| 380 | func contains(s, substr string) bool { |
no test coverage detected