@devries-cameron20 / composio-1 / commits / 33700dd9b
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.

fix: calculate telemetry durationMs after method execution (#2261)

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: rahul@composio.dev <rahultarak12345@gmail.com>

devin-ai-integration[bot] committed Dec 13, 2025 at 13:00 UTC 33700dd9b4255f2c57ae42a2cea3a952fb1e134e
1 file changed +46 -26
ts/packages/core/src/telemetry/Telemetry.ts
+46 -26
@@ -104,36 +104,51 @@ export class TelemetryTransport {
104 ) => Promise<unknown>;
105
106 (instance as unknown as Record<string, Function>)[name] = async (...args: unknown[]) => {
107 - // Only collect telemetry if setup() has been called (telemetry is enabled)
108 - if (this.shouldSendTelemetry()) {
109 - const telemetryPayload: TelemetryPayload = {
110 - functionName: `${instrumentedClassName}.${name}`,
111 - durationMs: 0,
112 - timestamp: Date.now() / 1000,
113 - props: {
114 - fileName: instrumentedClassName,
115 - method: name,
116 - params: args,
117 - },
118 - metadata: {
119 - provider: this.telemetryMetadata?.provider ?? 'openai',
120 - },
121 - error: undefined,
122 - source: this.telemetrySource,
123 - };
124 -
125 - this.batchProcessor.pushItem(telemetryPayload);
126 - }
107 + // Check telemetry status once at the start to ensure consistent behavior
108 + const telemetryEnabled = this.shouldSendTelemetry();
109 + const startTime = telemetryEnabled ? Date.now() : undefined;
110
111 try {
129 - return await originalMethod.apply(instance, args);
112 + const result = await originalMethod.apply(instance, args);
113 +
114 + // Only collect telemetry if setup() has been called (telemetry is enabled)
115 + if (telemetryEnabled && startTime !== undefined) {
116 + const durationMs = Date.now() - startTime;
117 + const telemetryPayload: TelemetryPayload = {
118 + functionName: `${instrumentedClassName}.${name}`,
119 + durationMs,
120 + timestamp: startTime / 1000,
121 + props: {
122 + fileName: instrumentedClassName,
123 + method: name,
124 + params: args,
125 + },
126 + metadata: {
127 + provider: this.telemetryMetadata?.provider ?? 'openai',
128 + },
129 + error: undefined,
130 + source: this.telemetrySource,
131 + };
132 +
133 + this.batchProcessor.pushItem(telemetryPayload);
134 + }
135 +
136 + return result;
137 } catch (error) {
138 if (error instanceof Error) {
139 if (!error.errorId) {
140 error.errorId = getRandomUUID();
141 // Only send error telemetry if telemetry is enabled
135 - if (this.shouldSendTelemetry()) {
136 - await this.prepareAndSendErrorTelemetry(error, instrumentedClassName, name, args);
142 + if (telemetryEnabled && startTime !== undefined) {
143 + const durationMs = Date.now() - startTime;
144 + await this.prepareAndSendErrorTelemetry(
145 + error,
146 + instrumentedClassName,
147 + name,
148 + args,
149 + startTime,
150 + durationMs
151 + );
152 }
153 }
154 }
@@ -166,17 +181,22 @@ export class TelemetryTransport {
181 * @param {unknown} error - The error to send.
182 * @param {string} instrumentedClassName - The class name of the instrumented class.
183 * @param {string} name - The name of the method that threw the error.
184 + * @param {unknown[]} args - The arguments passed to the method.
185 + * @param {number} startTime - The start time of the method invocation in milliseconds.
186 + * @param {number} durationMs - The duration of the method invocation in milliseconds.
187 */
188 private async prepareAndSendErrorTelemetry(
189 error: unknown,
190 instrumentedClassName: string,
191 name: string,
174 - args: unknown[]
192 + args: unknown[],
193 + startTime: number,
194 + durationMs: number
195 ) {
196 const telemetryPayload: TelemetryPayload = {
197 functionName: `${instrumentedClassName}.${name}`,
178 - durationMs: 0,
179 - timestamp: Date.now() / 1000,
198 + durationMs,
199 + timestamp: startTime / 1000,
200 props: {
201 fileName: instrumentedClassName,
202 method: name,