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

refactor telemetry

haxzie committed May 21, 2025 at 16:27 UTC 6c5a029e2606b5f7437af064af260d9fbb5ccccc
14 files changed +51 -45
packages/core/src/composio.ts
+10 -26
@@ -5,9 +5,8 @@ import { Triggers } from './models/Triggers';
5 import { AuthConfigs } from './models/AuthConfigs';
6 import { ConnectedAccounts } from './models/ConnectedAccounts';
7 import { BaseComposioProvider } from './provider/BaseProvider';
8 -import { Telemetry } from './telemetry/Telemetry';
8 +import { telemetry } from './telemetry/Telemetry';
9 import { BaseTelemetryTransport } from './telemetry/TelemetryTransport';
10 -import type { TelemetryMetadata } from './types/telemetry.types';
10 import { getSDKConfig } from './utils/sdk';
11 import logger from './utils/logger';
12 import { IS_DEVELOPMENT_OR_CI } from './utils/constants';
@@ -15,6 +14,7 @@ import { checkForLatestVersionFromNPM } from './utils/version';
14 import { OpenAIProvider } from './provider/OpenAIProvider';
15 import { version } from '../package.json';
16 import { getRandomUUID } from './utils/uuid';
17 +import { getEnvVariable } from './utils/env';
18
19 export type ComposioConfig<
20 TProvider extends BaseComposioProvider<unknown, unknown> = OpenAIProvider,
@@ -44,8 +44,6 @@ export class Composio<TProvider extends BaseComposioProvider<unknown, unknown> =
44 */
45 private config: ComposioConfig<TProvider>;
46
47 - private telemetry: Telemetry | undefined;
48 -
47 /**
48 * Core models for Composio.
49 */
@@ -73,6 +71,7 @@ export class Composio<TProvider extends BaseComposioProvider<unknown, unknown> =
71 config?.apiKey
72 );
73
74 + console.log(`Process.env.`, getEnvVariable('COMPOSIO_LOG_LEVEL'));
75 if (IS_DEVELOPMENT_OR_CI) {
76 logger.info(`Initializing Composio w API Key: [REDACTED] and baseURL: ${baseURLParsed}`);
77 }
@@ -112,43 +111,28 @@ export class Composio<TProvider extends BaseComposioProvider<unknown, unknown> =
111 * Initialize the client telemetry.
112 */
113 if (this.config.allowTracking ?? true) {
115 - this.initializeTelemetry(
114 + telemetry.setup(
115 {
116 apiKey: apiKeyParsed ?? '',
117 baseUrl: baseURLParsed ?? '',
119 - frameworkRuntime: 'node',
118 + framework: this.provider?.name || 'unknown',
119 + isAgentic: this.provider?._isAgentic || false,
120 source: 'node', // @TODO: get the source
121 - composioVersion: version,
121 + version: version,
122 isBrowser: typeof window !== 'undefined',
123 sessionId: getRandomUUID(), // @TODO: get the session id
124 },
125 config.telemetryTransport
126 );
127 }
128 + telemetry.instrument(this);
129 + // instrument the provider since we are not using the provider class directly
130 + telemetry.instrument(this.provider);
131
132 // Check for the latest version of the Composio SDK from NPM.
133 checkForLatestVersionFromNPM(version);
134 }
135
133 - /**
134 - * Initialize the instrumentations and telemetry for the Composio SDK.
135 - * @param {TelemetryMetadata} config - The configuration for the telemetry.
136 - * @param {BaseTelemetryTransport} transport - The transport for the telemetry.
137 - */
138 - private initializeTelemetry(config: TelemetryMetadata, transport?: BaseTelemetryTransport) {
139 - this.telemetry = new Telemetry(config, transport);
140 - /**
141 - * Instrument the instance and all the models with telemetry.
142 - */
143 - this.telemetry.instrumentTelemetry(this);
144 - this.telemetry.instrumentTelemetry(this.tools);
145 - this.telemetry.instrumentTelemetry(this.toolkits);
146 - this.telemetry.instrumentTelemetry(this.triggers);
147 - this.telemetry.instrumentTelemetry(this.authConfigs);
148 - this.telemetry.instrumentTelemetry(this.connectedAccounts);
149 - this.telemetry.instrumentTelemetry(this.provider);
150 - }
151 -
136 /**
137 * Get the Composio SDK client.
138 * @returns {ComposioClient} The Composio API client.
packages/core/src/models/AuthConfigs.ts
+2 -1
@@ -27,7 +27,7 @@ import {
27 CreateAuthConfigResponseSchema,
28 } from '../types/authConfigs.types';
29 import { ValidationError } from '../errors/ValidationError';
30 -
30 +import { telemetry } from '../telemetry/Telemetry';
31 /**
32 * AuthConfigs class
33 *
@@ -39,6 +39,7 @@ export class AuthConfigs {
39
40 constructor(client: ComposioClient) {
41 this.client = client;
42 + telemetry.instrument(this);
43 }
44
45 /**
packages/core/src/models/ConnectedAccounts.ts
+2 -1
@@ -26,7 +26,7 @@ import {
26 } from '../types/connectedAccounts.types';
27 import { ConnectionRequest } from './ConnectionRequest';
28 import { ValidationError } from '../errors/ValidationError';
29 -
29 +import { telemetry } from '../telemetry/Telemetry';
30 /**
31 * ConnectedAccounts class
32 *
@@ -38,6 +38,7 @@ export class ConnectedAccounts {
38
39 constructor(client: ComposioClient) {
40 this.client = client;
41 + telemetry.instrument(this);
42 }
43
44 transformConnectedAccountRetrieveResponse(
packages/core/src/models/ConnectionRequest.ts
+2
@@ -20,6 +20,7 @@ import {
20 ConnectionRequestTimeoutError,
21 } from '../errors/ConnectionRequestError';
22 import { ComposioConnectedAccountNotFoundError } from '../errors/ConnectedAccountsError';
23 +import { telemetry } from '../telemetry/Telemetry';
24 export class ConnectionRequest {
25 private client: ComposioClient;
26 public id: string;
@@ -36,6 +37,7 @@ export class ConnectionRequest {
37 this.id = connectedAccountId;
38 this.status = status || ConnectedAccountStatuses.INITIATED;
39 this.redirectUrl = redirectUrl;
40 + telemetry.instrument(this);
41 }
42
43 /**
packages/core/src/models/CustomTools.ts
+2
@@ -24,6 +24,7 @@ import {
24 } from '../errors/ToolErrors';
25 import { ComposioConnectedAccountNotFoundError } from '../errors/ConnectedAccountsError';
26 import { ComposioError } from '../errors/ComposioError';
27 +import { telemetry } from '../telemetry/Telemetry';
28
29 export class CustomTools {
30 private readonly client: ComposioClient;
@@ -35,6 +36,7 @@ export class CustomTools {
36 }
37 this.client = client;
38 this.customToolsRegistry = new Map();
39 + telemetry.instrument(this);
40 }
41
42 /**
packages/core/src/models/Session.ts
+2
@@ -1,11 +1,13 @@
1 import { SessionGetInfoResponse } from '@composio/client/resources/auth/session';
2 import ComposioClient from '@composio/client';
3 +import { telemetry } from '../telemetry/Telemetry';
4
5 export class Session {
6 private client: ComposioClient;
7
8 constructor(client: ComposioClient) {
9 this.client = client;
10 + telemetry.instrument(this);
11 }
12
13 /**
packages/core/src/models/Toolkits.ts
+2 -1
@@ -18,7 +18,7 @@ import { AuthConfigs } from './AuthConfigs';
18 import { ComposioAuthConfigNotFoundError } from '../errors/AuthConfigErrors';
19 import { ConnectedAccounts } from './ConnectedAccounts';
20 import { ConnectionRequest } from './ConnectionRequest';
21 -
21 +import { telemetry } from '../telemetry/Telemetry';
22 /**
23 * Toolkits class
24 *
@@ -31,6 +31,7 @@ export class Toolkits {
31 constructor(client: ComposioClient) {
32 this.client = client;
33 this.authorize = this.authorize.bind(this);
34 + telemetry.instrument(this);
35 }
36 /**
37 * Retrieves a list of toolkits based on the provided query parameters.
packages/core/src/models/Tools.ts
+3 -1
@@ -37,7 +37,7 @@ import {
37 ComposioProviderNotDefinedError,
38 } from '../errors/ToolErrors';
39 import { ValidationError } from '../errors/ValidationError';
40 -
40 +import { telemetry } from '../telemetry/Telemetry';
41 /**
42 * This class is used to manage tools in the Composio SDK.
43 * It provides methods to list, get, and execute tools.
@@ -70,6 +70,8 @@ export class Tools<
70 // Bind methods that use customTools to ensure correct 'this' context
71 this.getRawComposioToolBySlug = this.getRawComposioToolBySlug.bind(this);
72 this.getRawComposioTools = this.getRawComposioTools.bind(this);
73 +
74 + telemetry.instrument(this);
75 }
76
77 /**
packages/core/src/models/Triggers.ts
+2 -1
@@ -15,7 +15,7 @@ import { TriggerStatusEnum, TriggerSubscribeParams } from '../types/triggers.typ
15 import { PusherUtils, TriggerData } from '../utils/pusher';
16 import logger from '../utils/logger';
17 import { Session } from './Session';
18 -
18 +import { telemetry } from '../telemetry/Telemetry';
19 /**
20 * Trigger (Instance) class
21 * /api/v3/trigger_instances
@@ -26,6 +26,7 @@ export class Triggers {
26
27 constructor(client: ComposioClient) {
28 this.client = client;
29 + telemetry.instrument(this);
30 }
31
32 /**
packages/core/src/telemetry/Telemetry.ts
+11 -9
@@ -16,21 +16,21 @@ import { ProcessTelemetryTransport } from './transports/ProcessTransport';
16 * const telemetry = new Telemetry({...});
17 * const composio = new Composio({...})
18 *
19 - * telemetry.instrumentTelemetry(composio);
20 - * telemetry.instrumentTelemetry(composio.tools);
21 - * telemetry.instrumentTelemetry(composio.toolkits);
22 - * telemetry.instrumentTelemetry(composio.triggers);
19 + * telemetry.instrument(composio);
20 + * telemetry.instrument(composio.tools);
21 + * telemetry.instrument(composio.toolkits);
22 + * telemetry.instrument(composio.triggers);
23 *
24 */
25 -export class Telemetry {
26 - private telemetryMetadata: TelemetryMetadata;
27 - private transport: BaseTelemetryTransport;
25 +export class TelemetryService {
26 + private telemetryMetadata!: TelemetryMetadata;
27 + private transport!: BaseTelemetryTransport;
28
29 private batchProcessor = new BatchProcessor(100, 10, async data => {
30 await this.sendTelemetry(data as TelemetryPayload[]);
31 });
32
33 - constructor(metadata: TelemetryMetadata, transport?: BaseTelemetryTransport) {
33 + setup(metadata: TelemetryMetadata, transport?: BaseTelemetryTransport) {
34 this.telemetryMetadata = metadata;
35
36 const isBrowser = typeof window !== 'undefined';
@@ -61,7 +61,7 @@ export class Telemetry {
61 * @param fileName - the file name of the instance
62 * @returns
63 */
64 - instrumentTelemetry<T extends object>(instance: T, fileName?: string) {
64 + instrument<T extends object>(instance: T, fileName?: string) {
65 const proto = Object.getPrototypeOf(instance);
66 const methodNames = Object.getOwnPropertyNames(proto).filter(key => {
67 const descriptor = Object.getOwnPropertyDescriptor(proto, key);
@@ -139,3 +139,5 @@ export class Telemetry {
139 this.transport.send(reqPayload);
140 }
141 }
142 +
143 +export const telemetry = new TelemetryService();
packages/core/src/telemetry/transports/BrowserTransport.ts
+4 -1
@@ -1,8 +1,11 @@
1 import { TelemetryTransportParams } from '../../types/telemetry.types';
2 import logger from '../../utils/logger';
3 import { BaseTelemetryTransport } from '../TelemetryTransport';
4 -
4 +import { telemetry } from '../Telemetry';
5 export class BrowserTelemetryTransport implements BaseTelemetryTransport {
6 + constructor() {
7 + telemetry.instrument(this);
8 + }
9 send(payload: TelemetryTransportParams): Promise<void> {
10 if (typeof window === 'undefined') {
11 return Promise.reject(
packages/core/src/telemetry/transports/ProcessTransport.ts
+5 -1
@@ -1,8 +1,12 @@
1 import { TelemetryTransportParams } from '../../types/telemetry.types';
2 import logger from '../../utils/logger';
3 import { BaseTelemetryTransport } from '../TelemetryTransport';
4 -
4 +import { telemetry } from '../Telemetry';
5 export class ProcessTelemetryTransport implements BaseTelemetryTransport {
6 + constructor() {
7 + telemetry.instrument(this);
8 + }
9 +
10 send(payload: TelemetryTransportParams): Promise<void> {
11 if (typeof window !== 'undefined') {
12 return Promise.reject(
packages/core/src/types/telemetry.types.ts
+3 -2
@@ -13,8 +13,9 @@ export type TelemetryEvent = (typeof TELEMETRY_EVENTS)[keyof typeof TELEMETRY_EV
13 export type TelemetryMetadata = {
14 apiKey: string;
15 baseUrl: string;
16 - composioVersion: string;
17 - frameworkRuntime: string;
16 + version: string;
17 + framework: string;
18 + isAgentic: boolean;
19 source: string;
20 sessionId: string;
21 isBrowser?: boolean;
packages/core/src/utils/logger.ts
+1 -1
@@ -17,7 +17,7 @@ export type LogLevel = keyof typeof LOG_LEVELS;
17 * @returns {LogLevel} The current log level
18 */
19 export const getLogLevel = (): LogLevel => {
20 - const envLevel = getEnvVariable('COMPOSIO_LOGGING_LEVEL', 'info')?.toLowerCase();
20 + const envLevel = getEnvVariable('COMPOSIO_LOG_LEVEL', 'info')?.toLowerCase();
21 return envLevel && envLevel in LOG_LEVELS ? (envLevel as LogLevel) : 'info';
22 };
23