All files / sdk/utils/telemetry index.ts

65.38% Statements 17/26
33.33% Branches 1/3
37.5% Functions 3/8
65.38% Lines 17/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 658x 8x 8x 8x 8x   8x 8x 8x                                 8x 8x     8x 8x             8x 8x     8x                     15x         15x              
import { TELEMETRY_URL } from "../constants";
import { sendProcessReq, sendBrowserReq } from "../../../utils/external";
import ComposioSDKContext from "../composioContext";
import { BatchProcessor } from "../base/batchProcessor";
import { getEnvVariable } from "../../../utils/shared";
 
export class TELEMETRY_LOGGER {
    private static batchProcessor = new BatchProcessor(1000, 100, async (data) => {
        await TELEMETRY_LOGGER.sendTelemetry(data as Record<string, unknown>[]);
    });
 
    private static createTelemetryWrapper(method: Function, className: string) {
        return async (...args: unknown[]) => {
            const payload = {
                eventName: method.name,
                data: { className, args },
                sdk_meta: ComposioSDKContext
            };
            
            TELEMETRY_LOGGER.batchProcessor.pushItem(payload);
            return method(...args);
        };
    }
 
    private static async sendTelemetry(payload: Record<string, unknown>[]) {
        const isTelementryDisabled = getEnvVariable("TELEMETRY_DISABLED", "false") === "true";
        Iif(isTelementryDisabled) {
            return;
        }
        const url = `${TELEMETRY_URL}/api/sdk_metrics/telemetry`;
        const reqPayload = {
            data: { events: payload },
            url,
            method: "POST",
            headers: { "Content-Type": "application/json" }
        };
 
        const isBrowser = typeof window !== "undefined";
        Iif (isBrowser) {
            await sendBrowserReq(reqPayload);
        } else {
            await sendProcessReq(reqPayload);
        }
    }
 
    static wrapClassMethodsForTelemetry(classInstance: any, methods: string[]) {
        methods.forEach((method) => {
            classInstance[method] = TELEMETRY_LOGGER.createTelemetryWrapper(classInstance[method], classInstance.constructor.name);
        });
    }
 
    static manualTelemetry(eventName: string, data: Record<string, unknown>) {
        const payload = {
            eventName,
            data,
            sdk_meta: ComposioSDKContext
        };
        TELEMETRY_LOGGER.batchProcessor.pushItem(payload);
    }
 
    static wrapFunctionForTelemetry(func: Function, className: string) {
        return TELEMETRY_LOGGER.createTelemetryWrapper(func, className);
    }
}