@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 63 lines 2.2 KB
Raw
1 import logger from '../../utils/logger';
2 import { TelemetryMetricPayloadBody, TelemetryPayload } from './TelemetryService.types';
3
4 const TELEMETRY_URL = 'https://telemetry.composio.dev/v1';
5 const TELEMETRY_TIMEOUT_MS = 3_000;
6
7 export class TelemetryService {
8 /**
9 * POSTs a best-effort telemetry payload, bounding the request so a stalled
10 * endpoint cannot leave it pending indefinitely.
11 *
12 * A hand-rolled timer rather than AbortSignal.timeout so it can be cleared the
13 * moment the response lands: an uncleared timer is itself pending work, which on
14 * workerd pins the request context open for the full timeout on every successful
15 * send. Mirrors the bound applied to the npm version check.
16 */
17 private static async postWithTimeout(url: string, payload: unknown) {
18 const controller = new AbortController();
19 const timer = setTimeout(() => controller.abort(), TELEMETRY_TIMEOUT_MS);
20 try {
21 return await fetch(url, {
22 method: 'POST',
23 headers: {
24 'Content-Type': 'application/json',
25 },
26 body: JSON.stringify(payload),
27 signal: controller.signal,
28 });
29 } finally {
30 clearTimeout(timer);
31 }
32 }
33
34 /**
35 * Sends a metric to the Telemetry API.
36 * @param payload - The payload to send to the Telemetry API.
37 * @returns The response from the Telemetry API.
38 */
39 static async sendMetric(payload: TelemetryMetricPayloadBody) {
40 try {
41 const result = await this.postWithTimeout(`${TELEMETRY_URL}/metrics/invocations`, payload);
42 return result;
43 } catch (error) {
44 // ignore error for now; telemetry failures should never affect SDK calls
45 logger.debug('Error sending metric telemetry', error);
46 }
47 }
48
49 /**
50 * Sends an error log to the Telemetry API.
51 * @param payload - The payload to send to the Telemetry API.
52 * @returns The response from the Telemetry API.
53 */
54 static async sendErrorLog(payload: TelemetryPayload) {
55 try {
56 const result = await this.postWithTimeout(`${TELEMETRY_URL}/errors`, payload);
57 return result;
58 } catch (error) {
59 // ignore error for now; telemetry failures should never affect SDK calls
60 logger.debug('Error sending error telemetry', error);
61 }
62 }
63 }