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

Add telemetry docs

haxzie committed Apr 18, 2025 at 13:37 UTC 3e209e68784a4b2e5c10de0c1051c608d091105a
1 file changed +208
packages/core/src/telemetry/README.md new
+208
@@ -0,0 +1,208 @@
1 +# Telemetry Transports
2 +
3 +This directory contains implementations of different telemetry transport mechanisms for sending telemetry data to various endpoints.
4 +
5 +## Available Transports
6 +
7 +- `BrowserTransport`: Uses XMLHttpRequest for browser environments
8 +- `ProcessTransport`: Uses Node.js child processes for server-side environments
9 +- `ConsoleTransport`: Logs telemetry data to console (for development/debugging)
10 +
11 +## Using with Composio
12 +
13 +To use a custom telemetry transport with the Composio instance:
14 +
15 +```typescript
16 +import { Composio, BaseTelemetryTransport } from '@composio/core';
17 +
18 +// Create your custom transport
19 +class CustomTelemetryTransport implements BaseTelemetryTransport {
20 + async send(payload: TelemetryTransportParams): Promise<void> {
21 + // Your custom implementation
22 + console.log('Sending telemetry:', payload);
23 + return Promise.resolve();
24 + }
25 +}
26 +
27 +// Initialize Composio with your custom transport
28 +const composio = new Composio({
29 + apiKey: 'your-api-key',
30 + baseURL: 'https://api.composio.dev',
31 + allowTracking: true, // Enable telemetry
32 + telemetryTransport: new CustomTelemetryTransport() // Pass your custom transport
33 +});
34 +```
35 +
36 +You can also use the built-in transports:
37 +
38 +```typescript
39 +import { Composio, BrowserTelemetryTransport, ProcessTelemetryTransport } from '@composio/core';
40 +
41 +// For browser environments
42 +const composio = new Composio({
43 + apiKey: 'your-api-key',
44 + telemetryTransport: new BrowserTelemetryTransport()
45 +});
46 +
47 +// For Node.js environments
48 +const composio = new Composio({
49 + apiKey: 'your-api-key',
50 + telemetryTransport: new ProcessTelemetryTransport()
51 +});
52 +```
53 +
54 +Note: The telemetry system will automatically use the appropriate transport based on the environment if you don't specify one, but you can override this behavior by providing your own transport.
55 +
56 +## Creating a New Transport
57 +
58 +To create a new telemetry transport, follow these steps:
59 +
60 +1. Create a new file in the `transports` directory (e.g., `YourNewTransport.ts`)
61 +
62 +2. Implement the `BaseTelemetryTransport` interface:
63 +
64 +```typescript
65 +import { TelemetryTransportParams } from "../../types/telemetry.types";
66 +import { BaseTelemetryTransport } from "../TelemetryTransport";
67 +
68 +export class YourNewTransport implements BaseTelemetryTransport {
69 + send(payload: TelemetryTransportParams): Promise<void> {
70 + return new Promise((resolve) => {
71 + try {
72 + // Your transport-specific implementation
73 + // e.g., using fetch, WebSocket, or any other transport mechanism
74 +
75 + // Always resolve the promise, even on error
76 + resolve();
77 + } catch (error) {
78 + // Handle errors silently
79 + resolve();
80 + }
81 + });
82 + }
83 +}
84 +```
85 +
86 +### Required Interface
87 +
88 +Your transport must implement the following interface:
89 +
90 +```typescript
91 +interface TelemetryTransportParams {
92 + url: string; // The endpoint to send data to
93 + method: string; // HTTP method (e.g., 'POST')
94 + headers: Record<string, string>; // HTTP headers
95 + data: AcceptableJSONValue; // The actual telemetry data
96 +}
97 +```
98 +
99 +### Implementation Guidelines
100 +
101 +1. **Error Handling**
102 + - Always resolve the promise, even when errors occur
103 + - Don't throw errors that could affect the main application flow
104 + - Log errors appropriately for debugging
105 +
106 +2. **Environment Awareness**
107 + - Add environment-specific checks if your transport is meant for a specific runtime
108 + - Example:
109 + ```typescript
110 + if (typeof window === 'undefined') {
111 + return Promise.reject(new Error('YourTransport can only be used in specific environment'));
112 + }
113 + ```
114 +
115 +3. **Performance**
116 + - Keep the implementation lightweight
117 + - Ensure non-blocking operation
118 + - Consider using background processes or workers for heavy operations
119 +
120 +### Example: WebSocket Transport
121 +
122 +Here's an example of a WebSocket-based transport:
123 +
124 +```typescript
125 +export class WebSocketTransport implements BaseTelemetryTransport {
126 + private ws: WebSocket | null = null;
127 + private queue: TelemetryTransportParams[] = [];
128 + private isConnecting = false;
129 +
130 + constructor(private url: string) {}
131 +
132 + private async connect(): Promise<void> {
133 + if (this.isConnecting) return;
134 + this.isConnecting = true;
135 +
136 + return new Promise((resolve) => {
137 + this.ws = new WebSocket(this.url);
138 +
139 + this.ws.onopen = () => {
140 + this.isConnecting = false;
141 + this.processQueue();
142 + resolve();
143 + };
144 +
145 + this.ws.onerror = () => {
146 + this.isConnecting = false;
147 + resolve();
148 + };
149 + });
150 + }
151 +
152 + private async processQueue() {
153 + while (this.queue.length > 0 && this.ws?.readyState === WebSocket.OPEN) {
154 + const payload = this.queue.shift();
155 + if (payload) {
156 + this.ws.send(JSON.stringify(payload));
157 + }
158 + }
159 + }
160 +
161 + async send(payload: TelemetryTransportParams): Promise<void> {
162 + return new Promise((resolve) => {
163 + try {
164 + if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
165 + this.queue.push(payload);
166 + this.connect().then(resolve);
167 + } else {
168 + this.ws.send(JSON.stringify(payload));
169 + resolve();
170 + }
171 + } catch (error) {
172 + resolve();
173 + }
174 + });
175 + }
176 +}
177 +```
178 +
179 +### Integration
180 +
181 +To use your new transport:
182 +
183 +```typescript
184 +import { YourNewTransport } from './transports/YourNewTransport';
185 +
186 +// Create an instance
187 +const transport = new YourNewTransport();
188 +
189 +// Use with telemetry system
190 +telemetrySystem.setTransport(transport);
191 +```
192 +
193 +## Best Practices
194 +
195 +1. **Testing**
196 + - Write unit tests for your transport
197 + - Test error scenarios
198 + - Verify environment-specific behavior
199 +
200 +2. **Documentation**
201 + - Document any specific requirements or dependencies
202 + - Include usage examples
203 + - Note any environment limitations
204 +
205 +3. **Maintenance**
206 + - Keep dependencies up to date
207 + - Monitor for deprecation warnings
208 + - Update documentation as needed