feat: record task attempt chains

This commit is contained in:
2026-05-11 22:39:45 +08:00
parent 4c2de4b4c9
commit 0049b246c1
9 changed files with 492 additions and 9 deletions
+67 -1
View File
@@ -40,7 +40,8 @@ func buildSuccessRecord(task store.GatewayTask, user *auth.User, body map[string
}
func taskMetrics(task store.GatewayTask, user *auth.User, body map[string]any, candidate store.RuntimeModelCandidate, response clients.Response, simulated bool) map[string]any {
metrics := map[string]any{
metrics := attemptMetrics(candidate, 0, simulated)
for key, value := range map[string]any{
"kind": task.Kind,
"runMode": task.RunMode,
"requestedModel": task.Model,
@@ -57,6 +58,8 @@ func taskMetrics(task store.GatewayTask, user *auth.User, body map[string]any, c
"queueKey": candidate.QueueKey,
"requestId": response.RequestID,
"simulated": simulated,
} {
metrics[key] = value
}
if user != nil {
metrics["apiKeyId"] = user.APIKeyID
@@ -94,6 +97,29 @@ func taskMetrics(task store.GatewayTask, user *auth.User, body map[string]any, c
return metrics
}
func attemptMetrics(candidate store.RuntimeModelCandidate, attemptNo int, simulated bool) map[string]any {
metrics := map[string]any{
"resolvedModel": candidate.ModelName,
"modelName": candidate.ModelName,
"modelAlias": candidate.ModelAlias,
"providerModel": candidate.ProviderModelName,
"canonicalModel": candidate.CanonicalModelKey,
"modelType": candidate.ModelType,
"provider": candidate.Provider,
"platformId": candidate.PlatformID,
"platformKey": candidate.PlatformKey,
"platformName": candidate.PlatformName,
"platformModelId": candidate.PlatformModelID,
"clientId": candidate.ClientID,
"queueKey": candidate.QueueKey,
"simulated": simulated,
}
if attemptNo > 0 {
metrics["attempt"] = attemptNo
}
return metrics
}
func usageToMap(usage clients.Usage) map[string]any {
out := map[string]any{}
if usage.InputTokens > 0 {
@@ -172,6 +198,46 @@ func failureMetrics(err error, simulated bool) (string, map[string]any, time.Tim
return meta.RequestID, metrics, meta.ResponseStartedAt, meta.ResponseFinishedAt, meta.ResponseDurationMS
}
func mergeMetrics(values ...map[string]any) map[string]any {
out := map[string]any{}
for _, value := range values {
for key, item := range value {
out[key] = item
}
}
return out
}
func summarizeAttempts(attempts []store.TaskAttempt) []map[string]any {
items := make([]map[string]any, 0, len(attempts))
for _, attempt := range attempts {
item := map[string]any{
"attempt": attempt.AttemptNo,
"status": attempt.Status,
"platformId": attempt.PlatformID,
"platformName": attempt.PlatformName,
"provider": attempt.Provider,
"platformModelId": attempt.PlatformModelID,
"modelName": attempt.ModelName,
"providerModelName": attempt.ProviderModelName,
"modelAlias": attempt.ModelAlias,
"modelType": attempt.ModelType,
"clientId": attempt.ClientID,
"queueKey": attempt.QueueKey,
"requestId": attempt.RequestID,
"retryable": attempt.Retryable,
"simulated": attempt.Simulated,
"errorCode": attempt.ErrorCode,
"errorMessage": attempt.ErrorMessage,
"responseDurationMs": attempt.ResponseDurationMS,
"startedAt": attempt.StartedAt,
"finishedAt": attempt.FinishedAt,
}
items = append(items, item)
}
return items
}
func messageCount(body map[string]any) int {
messages, _ := body["messages"].([]any)
return len(messages)