All files / sdk/utils/telemetry index.ts

70.83% Statements 17/24
33.33% Branches 1/3
50% Functions 3/6
70.83% Lines 17/24

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 65 66 67 68 69 70 71 72 73 74 75 76 7715x 15x 15x 15x 15x   15x 15x 68x                                                 68x   68x       68x   68x             68x 68x     68x         157x                       157x              
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(100, 10, 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: {
          apiKey: ComposioSDKContext.apiKey,
          baseURL: ComposioSDKContext.baseURL,
          composioVersion: ComposioSDKContext.composioVersion,
          frameworkRuntime: ComposioSDKContext.frameworkRuntime,
          source: ComposioSDKContext.source,
          isBrowser: typeof window !== "undefined",
        },
      };
 
      TELEMETRY_LOGGER.batchProcessor.pushItem(payload);
      return method(...args);
    };
  }
 
  private static async sendTelemetry(payload: Record<string, unknown>[]) {
    const isTelemetryDisabled =
      getEnvVariable("TELEMETRY_DISABLED", "false") === "true";
 
    Iif (isTelemetryDisabled) {
      return;
    }
 
    const url = `${TELEMETRY_URL}/api/sdk_metrics/telemetry`;
 
    const reqPayload = {
      data: payload,
      url,
      method: "POST",
      headers: { "Content-Type": "application/json" },
    };
 
    const isBrowser = typeof window !== "undefined";
    Iif (isBrowser) {
      await sendBrowserReq(reqPayload);
    } else {
      await sendProcessReq(reqPayload);
    }
  }
 
  static manualTelemetry(eventName: string, data: Record<string, unknown>) {
    const payload = {
      eventName,
      data,
      sdk_meta: {
        apiKey: ComposioSDKContext.apiKey,
        baseURL: ComposioSDKContext.baseURL,
        composioVersion: ComposioSDKContext.composioVersion,
        frameworkRuntime: ComposioSDKContext.frameworkRuntime,
        source: ComposioSDKContext.source,
        isBrowser: typeof window !== "undefined",
      },
    };
    TELEMETRY_LOGGER.batchProcessor.pushItem(payload);
  }
 
  static wrapFunctionForTelemetry(func: Function, className: string) {
    return TELEMETRY_LOGGER.createTelemetryWrapper(func, className);
  }
}