-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from supabase-community/feat/telemetry
Telemetry
- Loading branch information
Showing
4 changed files
with
123 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Event for an AI chat rate limit. Includes the | ||
* remaining input and output tokens in the rate | ||
* limit window (one of these will be <= 0). | ||
*/ | ||
export type ChatRateLimitEvent = { | ||
type: 'chat-rate-limit' | ||
metadata: { | ||
databaseId: string | ||
userId: string | ||
inputTokensRemaining: number | ||
outputTokensRemaining: number | ||
} | ||
} | ||
|
||
/** | ||
* Event for an AI chat inference request-response. | ||
* Includes both input and output metadata. | ||
*/ | ||
export type ChatInferenceEvent = { | ||
type: 'chat-inference' | ||
metadata: { | ||
databaseId: string | ||
userId: string | ||
messageCount: number | ||
inputType: 'user-message' | 'tool-result' | ||
inputTokens: number | ||
outputTokens: number | ||
finishReason: | ||
| 'stop' | ||
| 'length' | ||
| 'content-filter' | ||
| 'tool-calls' | ||
| 'error' | ||
| 'other' | ||
| 'unknown' | ||
toolCalls?: string[] | ||
} | ||
} | ||
|
||
export type TelemetryEvent = ChatRateLimitEvent | ChatInferenceEvent | ||
|
||
export async function logEvent<E extends TelemetryEvent>(type: E['type'], metadata: E['metadata']) { | ||
if (!process.env.LOGFLARE_SOURCE || !process.env.LOGFLARE_API_KEY) { | ||
if (process.env.DEBUG) { | ||
console.log(type, metadata) | ||
} | ||
return | ||
} | ||
|
||
const response = await fetch( | ||
`https://api.logflare.app/logs?source=${process.env.LOGFLARE_SOURCE}`, | ||
{ | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-API-KEY': process.env.LOGFLARE_API_KEY, | ||
}, | ||
body: JSON.stringify({ | ||
event_message: type, | ||
metadata, | ||
}), | ||
} | ||
) | ||
|
||
if (!response.ok) { | ||
const { error } = await response.json() | ||
console.error('failed to send logflare event', error) | ||
} | ||
} |