这是indexloc提供的服务,不要输入任何密码
Skip to content

CHORE: debounce telems to prevent spamming #3505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion server/endpoints/workspaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,6 @@ function workspaceEndpoints(app) {
: "Forked Thread",
});

await Telemetry.sendTelemetry("thread_forked");
await EventLogs.logEvent(
"thread_forked",
{
Expand Down
51 changes: 51 additions & 0 deletions server/models/telemetry.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
const { v4 } = require("uuid");
const { SystemSettings } = require("./systemSettings");

// Map of events and last sent time to check if the event is on cooldown
// This will be cleared on server restart - but that is fine since it is mostly to just
// prevent spamming the logs.
const TelemetryCooldown = new Map();

const Telemetry = {
// Write-only key. It can't read events or any of your other data, so it's safe to use in public apps.
pubkey: "phc_9qu7QLpV8L84P3vFmEiZxL020t2EqIubP7HHHxrSsqS",
stubDevelopmentEvents: true, // [DO NOT TOUCH] Core team only.
label: "telemetry_id",
/*
Key value pairs of events that should be debounced to prevent spamming the logs.
This should be used for events that could be triggered in rapid succession that are not useful to atomically log.
The value is the number of seconds to debounce the event
*/
debounced: {
agent_chat_started: 1800,
sent_chat: 1800,
agent_chat_sent: 1800,
agent_chat_started: 1800,
agent_tool_call: 1800,
},

id: async function () {
const result = await SystemSettings.get({ label: this.label });
Expand Down Expand Up @@ -34,6 +51,34 @@ const Telemetry = {
return "other";
},

/**
* Checks if the event is on cooldown
* @param {string} event - The event to check
* @returns {boolean} - True if the event is on cooldown, false otherwise
*/
isOnCooldown: function (event) {
// If the event is not debounced, return false
if (!this.debounced[event]) return false;

// If the event is not in the cooldown map, return false
const lastSent = TelemetryCooldown.get(event);
if (!lastSent) return false;

// If the event is in the cooldown map, check if it has expired
const now = Date.now();
const cooldown = this.debounced[event] * 1000;
return now - lastSent < cooldown;
},

/**
* Marks the event as on cooldown - will check if the event is debounced first
* @param {string} event - The event to mark
*/
markOnCooldown: function (event) {
if (!this.debounced[event]) return;
TelemetryCooldown.set(event, Date.now());
},

sendTelemetry: async function (
event,
eventProperties = {},
Expand All @@ -46,6 +91,9 @@ const Telemetry = {
const distinctId = !!subUserId ? `${systemId}::${subUserId}` : systemId;
const properties = { ...eventProperties, runtime: this.runtime() };

// If the event is on cooldown, return
if (this.isOnCooldown(event)) return;

// Silence some events to keep logs from being too messy in production
// eg: Tool calls from agents spamming the logs.
if (!silent) {
Expand All @@ -63,6 +111,9 @@ const Telemetry = {
});
} catch {
return;
} finally {
// Mark the event as on cooldown if needed
this.markOnCooldown(event);
}
},

Expand Down