Skip to content

Commit a0fc749

Browse files
author
Theodore Li
committed
Add more detailed metrics for different throttling types
1 parent 612ea7c commit a0fc749

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

apps/sim/lib/core/telemetry.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,31 @@ export const PlatformEvents = {
935935
},
936936

937937
/**
938-
* Track hosted key rate limited
938+
* Track when a rate limit error is surfaced to the end user (not retried/absorbed).
939+
* Fires for both billing-actor limits and exhausted upstream retries.
940+
*/
941+
userThrottled: (attrs: {
942+
toolId: string
943+
reason: 'billing_actor_limit' | 'upstream_retries_exhausted'
944+
provider?: string
945+
retryAfterMs?: number
946+
userId?: string
947+
workspaceId?: string
948+
workflowId?: string
949+
}) => {
950+
trackPlatformEvent('platform.user.throttled', {
951+
'tool.id': attrs.toolId,
952+
'throttle.reason': attrs.reason,
953+
...(attrs.provider && { 'provider.id': attrs.provider }),
954+
...(attrs.retryAfterMs != null && { 'rate_limit.retry_after_ms': attrs.retryAfterMs }),
955+
...(attrs.userId && { 'user.id': attrs.userId }),
956+
...(attrs.workspaceId && { 'workspace.id': attrs.workspaceId }),
957+
...(attrs.workflowId && { 'workflow.id': attrs.workflowId }),
958+
})
959+
},
960+
961+
/**
962+
* Track hosted key rate limited by upstream provider (429 from the external API)
939963
*/
940964
hostedKeyRateLimited: (attrs: {
941965
toolId: string

apps/sim/tools/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,11 @@ async function injectHostedKeyIfNeeded(
9898
retryAfterMs: acquireResult.retryAfterMs,
9999
})
100100

101-
PlatformEvents.hostedKeyRateLimited({
101+
PlatformEvents.userThrottled({
102102
toolId: tool.id,
103-
envVarName: 'billing_actor_rate_limited',
104-
attempt: 0,
105-
maxRetries: 0,
106-
delayMs: acquireResult.retryAfterMs ?? 0,
103+
reason: 'billing_actor_limit',
104+
provider,
105+
retryAfterMs: acquireResult.retryAfterMs ?? 0,
107106
userId: executionContext?.userId,
108107
workspaceId: executionContext?.workspaceId,
109108
workflowId: executionContext?.workflowId,
@@ -175,6 +174,15 @@ async function executeWithRetry<T>(
175174
lastError = error
176175

177176
if (!isRateLimitError(error) || attempt === maxRetries) {
177+
if (isRateLimitError(error) && attempt === maxRetries) {
178+
PlatformEvents.userThrottled({
179+
toolId,
180+
reason: 'upstream_retries_exhausted',
181+
userId: executionContext?.userId,
182+
workspaceId: executionContext?.workspaceId,
183+
workflowId: executionContext?.workflowId,
184+
})
185+
}
178186
throw error
179187
}
180188

0 commit comments

Comments
 (0)