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 22, 15:45 UTC
fix(ts): telemetry batch upload
jkomyno committed
Jan 2, 2026 at 16:09 UTC
89b10c6a6f2e8e328ac8547311d4c9c5e5ef5d35
3 files changed
+190
-9
ts/packages/core/src/telemetry/BatchProcessor.ts
+26
-3
@@ -7,13 +7,14 @@ export class BatchProcessor {
7
private batch: TelemetryMetricPayloadBody = [];
8
private time: number;
9
private batchSize: number;
10
- private processBatchCallback: (data: TelemetryMetricPayloadBody) => void;
10
+ private processBatchCallback: (data: TelemetryMetricPayloadBody) => Promise<void>;
11
private timer: NodeJS.Timeout | null = null;
12
+ private pendingFlush: Promise<void> | null = null;
13
14
constructor(
15
time: number = 2000,
16
batchSize: number = 100,
16
- processBatchCallback: (data: TelemetryMetricPayloadBody) => void
17
+ processBatchCallback: (data: TelemetryMetricPayloadBody) => Promise<void>
18
) {
19
this.batch = [];
20
this.time = time;
@@ -32,12 +33,34 @@ export class BatchProcessor {
33
34
processBatch() {
35
if (this.batch.length > 0) {
35
- this.processBatchCallback(this.batch);
36
+ const batchToProcess = this.batch;
37
this.batch = [];
38
+ const pending = this.processBatchCallback(batchToProcess)
39
+ .catch(() => {
40
+ // Silently ignore errors - they should be handled by the callback
41
+ })
42
+ .finally(() => {
43
+ if (this.pendingFlush === pending) {
44
+ this.pendingFlush = null;
45
+ }
46
+ });
47
+
48
+ this.pendingFlush = pending;
49
}
50
if (this.timer) {
51
clearTimeout(this.timer);
52
this.timer = null;
53
}
54
}
55
+
56
+ /**
57
+ * Flush any pending batch and wait for it to complete.
58
+ * Useful for ensuring telemetry is sent before process exit.
59
+ */
60
+ async flush(): Promise<void> {
61
+ this.processBatch();
62
+ if (this.pendingFlush) {
63
+ await this.pendingFlush;
64
+ }
65
+ }
66
}
ts/packages/core/src/telemetry/Telemetry.ts
+8
@@ -265,6 +265,14 @@ export class TelemetryTransport {
265
logger.error('Error sending error telemetry', error);
266
}
267
}
268
+
269
+ /**
270
+ * Flush any pending telemetry and wait for it to complete.
271
+ * Call this before process exit to ensure all telemetry is sent.
272
+ */
273
+ async flush(): Promise<void> {
274
+ await this.batchProcessor.flush();
275
+ }
276
}
277
278
export const telemetry = new TelemetryTransport();
ts/packages/core/test/telemetry/TelemetryService.test.ts
+156
-6
@@ -2,11 +2,7 @@ import { describe, it, expect, vi, beforeEach, afterEach, beforeAll, afterAll }
2
import { BatchProcessor } from '../../src/telemetry/BatchProcessor';
3
import { TelemetryTransport } from '../../src/telemetry/Telemetry';
4
import { TelemetryService } from '../../src/services/telemetry/TelemetryService';
5
-import type {
6
- TelemetryPayload,
7
- TelemetryMetricPayloadBody,
8
- TelemetryMetricMetadata,
9
-} from '../../src/services/telemetry/TelemetryService.types';
5
+import type { TelemetryPayload } from '../../src/services/telemetry/TelemetryService.types';
6
import { TelemetryMetadata } from '../../src/types/telemetry.types';
7
8
// Helper to create a mock telemetry payload
@@ -31,7 +27,7 @@ describe('BatchProcessor', () => {
27
let processor: BatchProcessor;
28
29
beforeEach(() => {
34
- callback = vi.fn();
30
+ callback = vi.fn().mockResolvedValue(undefined);
31
processor = new BatchProcessor(50, 3, callback); // small batch size and time for test
32
});
33
@@ -65,6 +61,144 @@ describe('BatchProcessor', () => {
61
// @ts-expect-error: private access for test
62
expect(processor.batch).toEqual([]);
63
});
64
+
65
+ describe('async callback handling', () => {
66
+ it('should handle async callbacks properly', async () => {
67
+ let resolveCallback: () => void;
68
+ const asyncCallback = vi.fn().mockImplementation(() => {
69
+ return new Promise<void>(resolve => {
70
+ resolveCallback = resolve;
71
+ });
72
+ });
73
+ const asyncProcessor = new BatchProcessor(50, 2, asyncCallback);
74
+
75
+ const payloads = [createPayload(), createPayload()];
76
+ payloads.forEach(p => asyncProcessor.pushItem(p));
77
+
78
+ expect(asyncCallback).toHaveBeenCalledOnce();
79
+ expect(asyncCallback).toHaveBeenCalledWith(payloads);
80
+
81
+ // Resolve the async callback
82
+ resolveCallback!();
83
+ await asyncProcessor.flush();
84
+ });
85
+
86
+ it('should track pending flush promise for async callbacks', async () => {
87
+ let resolveCallback: () => void;
88
+ const asyncCallback = vi.fn().mockImplementation(() => {
89
+ return new Promise<void>(resolve => {
90
+ resolveCallback = resolve;
91
+ });
92
+ });
93
+ const asyncProcessor = new BatchProcessor(50, 2, asyncCallback);
94
+
95
+ asyncProcessor.pushItem(createPayload());
96
+ asyncProcessor.pushItem(createPayload());
97
+
98
+ // @ts-expect-error: private access for test
99
+ expect(asyncProcessor.pendingFlush).toBeInstanceOf(Promise);
100
+
101
+ resolveCallback!();
102
+ await asyncProcessor.flush();
103
+
104
+ // @ts-expect-error: private access for test
105
+ expect(asyncProcessor.pendingFlush).toBeNull();
106
+ });
107
+
108
+ it('should silently handle errors in async callbacks', async () => {
109
+ const asyncCallback = vi.fn().mockRejectedValue(new Error('Network error'));
110
+ const asyncProcessor = new BatchProcessor(50, 2, asyncCallback);
111
+
112
+ asyncProcessor.pushItem(createPayload());
113
+ asyncProcessor.pushItem(createPayload());
114
+
115
+ // Should not throw
116
+ await expect(asyncProcessor.flush()).resolves.toBeUndefined();
117
+ });
118
+
119
+ it('should clear batch before async callback completes', async () => {
120
+ let resolveCallback: () => void;
121
+ const asyncCallback = vi.fn().mockImplementation(() => {
122
+ return new Promise<void>(resolve => {
123
+ resolveCallback = resolve;
124
+ });
125
+ });
126
+ const asyncProcessor = new BatchProcessor(50, 2, asyncCallback);
127
+
128
+ asyncProcessor.pushItem(createPayload());
129
+ asyncProcessor.pushItem(createPayload());
130
+
131
+ // Batch should be cleared immediately, not after callback completes
132
+ // @ts-expect-error: private access for test
133
+ expect(asyncProcessor.batch).toEqual([]);
134
+
135
+ resolveCallback!();
136
+ await asyncProcessor.flush();
137
+ });
138
+ });
139
+
140
+ describe('flush method', () => {
141
+ it('should process remaining items when flush is called', async () => {
142
+ const asyncCallback = vi.fn().mockResolvedValue(undefined);
143
+ const asyncProcessor = new BatchProcessor(50, 10, asyncCallback); // large batch size
144
+
145
+ asyncProcessor.pushItem(createPayload());
146
+ asyncProcessor.pushItem(createPayload());
147
+
148
+ expect(asyncCallback).not.toHaveBeenCalled();
149
+
150
+ await asyncProcessor.flush();
151
+
152
+ expect(asyncCallback).toHaveBeenCalledOnce();
153
+ });
154
+
155
+ it('should wait for pending async callback to complete', async () => {
156
+ let callbackCompleted = false;
157
+ let resolveCallback: () => void;
158
+ const asyncCallback = vi.fn().mockImplementation(() => {
159
+ return new Promise<void>(resolve => {
160
+ resolveCallback = resolve;
161
+ }).then(() => {
162
+ callbackCompleted = true;
163
+ });
164
+ });
165
+ const asyncProcessor = new BatchProcessor(50, 2, asyncCallback);
166
+
167
+ asyncProcessor.pushItem(createPayload());
168
+ asyncProcessor.pushItem(createPayload());
169
+
170
+ expect(callbackCompleted).toBe(false);
171
+
172
+ const flushPromise = asyncProcessor.flush();
173
+ expect(callbackCompleted).toBe(false);
174
+
175
+ resolveCallback!();
176
+ await flushPromise;
177
+
178
+ expect(callbackCompleted).toBe(true);
179
+ });
180
+
181
+ it('should be safe to call flush multiple times', async () => {
182
+ const asyncCallback = vi.fn().mockResolvedValue(undefined);
183
+ const asyncProcessor = new BatchProcessor(50, 10, asyncCallback);
184
+
185
+ asyncProcessor.pushItem(createPayload());
186
+
187
+ await asyncProcessor.flush();
188
+ await asyncProcessor.flush();
189
+ await asyncProcessor.flush();
190
+
191
+ expect(asyncCallback).toHaveBeenCalledOnce();
192
+ });
193
+
194
+ it('should handle flush with no pending items', async () => {
195
+ const asyncCallback = vi.fn().mockResolvedValue(undefined);
196
+ const asyncProcessor = new BatchProcessor(50, 10, asyncCallback);
197
+
198
+ await expect(asyncProcessor.flush()).resolves.toBeUndefined();
199
+ expect(asyncCallback).not.toHaveBeenCalled();
200
+ });
201
+ });
202
});
203
204
describe('TelemetryTransport', () => {
@@ -135,6 +269,22 @@ describe('TelemetryTransport', () => {
269
await expect(instrumented.fail()).rejects.toThrow('fail!');
270
expect(sendErrorLogSpy).toHaveBeenCalled();
271
});
272
+
273
+ it('should flush pending telemetry when flush is called', async () => {
274
+ class TestClass {
275
+ async foo(a: number) {
276
+ return a * 2;
277
+ }
278
+ }
279
+ const instance = new TestClass();
280
+ const instrumented = transport.instrument(instance);
281
+
282
+ // Call the method to generate telemetry
283
+ await instrumented.foo(21);
284
+
285
+ // Flush should complete without error
286
+ await expect(transport.flush()).resolves.toBeUndefined();
287
+ });
288
});
289
290
beforeAll(() => {