Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
Add telemetry proxy server
haxzie8848c214eb78
diff --git a/apps/telemetry-proxy/.gitignore b/apps/telemetry-proxy/.gitignorenew file mode 100644index 000000000..af5b77da1--- /dev/null+++ b/apps/telemetry-proxy/.gitignore@@ -0,0 +1,38 @@+# prod+dist/++# dev+.yarn/+!.yarn/releases+.vscode/*+!.vscode/launch.json+!.vscode/*.code-snippets+.idea/workspace.xml+.idea/usage.statistics.xml+.idea/shelf++# deps+node_modules/+.wrangler++# env+.env+.env.production+.dev.vars++# logs+logs/+*.log+npm-debug.log*+yarn-debug.log*+yarn-error.log*+pnpm-debug.log*+lerna-debug.log*++# misc+.DS_Store+++# env+.dev.vars+.env\ No newline at end of filediff --git a/apps/telemetry-proxy/README.md b/apps/telemetry-proxy/README.mdnew file mode 100644index 000000000..eba2b1e47--- /dev/null+++ b/apps/telemetry-proxy/README.md@@ -0,0 +1,21 @@+```txt+npm install+npm run dev+```++```txt+npm run deploy+```++[For generating/synchronizing types based on your Worker configuration run](https://developers.cloudflare.com/workers/wrangler/commands/#types):++```txt+npm run cf-typegen+```++Pass the `CloudflareBindings` as generics when instantiation `Hono`:++```ts+// src/index.ts+const app = new Hono<{ Bindings: CloudflareBindings }>()+```diff --git a/apps/telemetry-proxy/package.json b/apps/telemetry-proxy/package.jsonnew file mode 100644index 000000000..4adf2c4b9--- /dev/null+++ b/apps/telemetry-proxy/package.json@@ -0,0 +1,19 @@+{+ "name": "@composio/telemetry-proxy",+ "version": "0.0.1",+ "private": true,+ "type": "module",+ "scripts": {+ "dev": "wrangler dev",+ "deploy": "wrangler deploy --minify",+ "cf-typegen": "wrangler types --env-interface CloudflareBindings"+ },+ "dependencies": {+ "@hono/zod-openapi": "^0.19.8",+ "hono": "^4.7.11",+ "zod": "^3.24.2"+ },+ "devDependencies": {+ "wrangler": "^4.4.0"+ }+}diff --git a/apps/telemetry-proxy/src/index.ts b/apps/telemetry-proxy/src/index.tsnew file mode 100644index 000000000..4e8fae413--- /dev/null+++ b/apps/telemetry-proxy/src/index.ts@@ -0,0 +1,15 @@+import telemetry from './routes/v1/telemetry';+import { AppBindings } from './types/app.types';+import { OpenAPIHono } from '@hono/zod-openapi';++const app = new OpenAPIHono<{ Bindings: AppBindings }>();+app.route('/v1', telemetry);+app.doc('/docs', {+ openapi: '3.0.0',+ info: {+ title: 'Composio Telemetry API',+ version: '1.0.0',+ },+});++export default app;diff --git a/apps/telemetry-proxy/src/lib/errors.ts b/apps/telemetry-proxy/src/lib/errors.tsnew file mode 100644index 000000000..623379231--- /dev/null+++ b/apps/telemetry-proxy/src/lib/errors.ts@@ -0,0 +1,57 @@+import { ErrorLogPayload, ErrorLogPayloadSchema } from '../types/errors.types';++const getLogTags = (errorData: ErrorLogPayload) => {+ const { source, metadata } = errorData;+ return [+ ...(source?.name ? [`source:${source.name}`] : []),+ ...(source?.service ? [`service:${source.service}`] : []),+ ...(source?.language ? [`language:${source.language}`] : []),+ ...(source?.version ? [`version:${source.version}`] : []),+ ...(source?.platform ? [`platform:${source.platform}`] : []),+ ...(metadata?.projectId ? [`project_id:${metadata.projectId}`] : []),+ ...(metadata?.provider ? [`provider:${metadata.provider}`] : []),+ 'status:error',+ 'level:error',+ ]+ .filter(Boolean)+ .join(',');+};++const getLogHost = (errorData: ErrorLogPayload) => {+ const { source } = errorData;+ return `composio-${source?.service}-${source?.language}`;+};++export const createErrorLogPayload = (errorData: ErrorLogPayload) => {+ const logBody = {+ message: errorData.message,+ ddtags: getLogTags(errorData),+ hostname: getLogHost(errorData),+ service: getLogHost(errorData),+ };+ return logBody;+};++export async function sendErrorLogToDatadog(errorData: ErrorLogPayload, datadogApiKey: string) {+ const parsedPayload = ErrorLogPayloadSchema.safeParse(errorData);+ if (parsedPayload.error) {+ throw parsedPayload.error;+ }++ const logBody = createErrorLogPayload(parsedPayload.data);++ const res = await fetch('https://api.datadoghq.com/api/v1/series', {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ 'DD-API-KEY': datadogApiKey,+ },+ body: JSON.stringify(logBody),+ });++ if (!res.ok) {+ console.error('Failed to send error log to Datadog:', await res.text());+ }++ return res.ok;+}diff --git a/apps/telemetry-proxy/src/lib/metrics.ts b/apps/telemetry-proxy/src/lib/metrics.tsnew file mode 100644index 000000000..c7ea0dc2a--- /dev/null+++ b/apps/telemetry-proxy/src/lib/metrics.ts@@ -0,0 +1,105 @@+import {+ DDMetricPayload,+ DDMetricPayloadSchema,+ TelemetryMetricPayload,+ TelemetryMetricPayloadSchema,+} from '../types/metrics.types';++const getMetricName = (+ type: 'function_invocation' | 'function_duration',+ payload: TelemetryMetricPayload+) => {+ const { functionName, source } = payload;+ if (source?.service === 'sdk') {+ return `composio.${source.service}.${type}`;+ }+ return `composio.sdk.${type}`;+};++const getMetricTags = (payload: TelemetryMetricPayload) => {+ const { functionName, source, metadata, error } = payload;+ return [+ `function:${functionName}`,+ ...(source?.name ? [`source:${source.name}`] : []),+ ...(source?.service ? [`service:${source.service}`] : []),+ ...(source?.language ? [`language:${source.language}`] : []),+ ...(source?.version ? [`version:${source.version}`] : []),+ ...(source?.platform ? [`platform:${source.platform}`] : []),+ ...(metadata?.projectId ? [`project_id:${metadata.projectId}`] : []),+ ...(metadata?.provider ? [`provider:${metadata.provider}`] : []),+ ...(error ? ['status:error'] : ['status:success']),+ ];+};++const getMetricHost = (payload: TelemetryMetricPayload) => {+ const { source } = payload;+ return `composio-${source?.service}-${source?.language}`;+};++const createDDMetricPayload = (+ type: 'count' | 'gauge',+ payload: TelemetryMetricPayload+): DDMetricPayload => {+ const { durationMs, timestamp } = payload;+ const executionTimestamp = timestamp ?? Math.floor(Date.now() / 1000);+ const executionDuration = durationMs ?? 1;++ const parsedPayload = DDMetricPayloadSchema.safeParse({+ host: getMetricHost(payload),+ metric: getMetricName('function_invocation', payload),+ points: [[executionTimestamp, executionDuration]],+ tags: getMetricTags(payload),+ type,+ });++ if (parsedPayload.error) {+ throw parsedPayload.error;+ }++ return parsedPayload.data;+};++/**+ * Creates a metric payload for Datadog.+ *+ * @description This function creates a metric payload for Datadog. It returns an array of two+ * objects, one for the count metric and one for the duration metric.+ *+ * @param payload - The payload to create a metric for.+ * @returns An array of two objects, one for the count metric and one for the duration metric.+ */+export const createMetricPayload = (payload: TelemetryMetricPayload): Array<DDMetricPayload> => {+ const parsedPayload = TelemetryMetricPayloadSchema.safeParse(payload);+ if (parsedPayload.error) {+ throw parsedPayload.error;+ }++ const DDMetricCountPayload = createDDMetricPayload('count', payload);+ const DDMetricDurationPayload = createDDMetricPayload('gauge', payload);++ return [DDMetricCountPayload, DDMetricDurationPayload];+};++export const sendMetricToDatadog = async (+ payload: Array<TelemetryMetricPayload>,+ datadogApiKey: string+) => {+ const parsedPayload = payload.map(item => createMetricPayload(item)).flat();+ console.log(JSON.stringify(parsedPayload, null, 2));+ const res = await fetch('https://api.datadoghq.com/api/v1/series', {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ 'DD-API-KEY': datadogApiKey,+ },+ body: JSON.stringify({+ series: parsedPayload,+ }),+ });++ if (!res.ok) {+ console.error('Failed to send metric payload to Datadog:', await res.text());+ }++ return res.ok;+};diff --git a/apps/telemetry-proxy/src/routes/v1/telemetry.ts b/apps/telemetry-proxy/src/routes/v1/telemetry.tsnew file mode 100644index 000000000..b6e749445--- /dev/null+++ b/apps/telemetry-proxy/src/routes/v1/telemetry.ts@@ -0,0 +1,120 @@+import {+ TelemetryMetricPayload,+ TelemetryMetricPayloadSchema,+ TelemetryMetricRequestSchema,+} from '../../types/metrics.types';+import { AppBindings } from '../../types/app.types';+import { sendMetricToDatadog } from '../../lib/metrics';+import { sendErrorLogToDatadog } from '../../lib/errors';+import { ErrorLogPayload, ErrorLogPayloadSchema } from '../../types/errors.types';+import { createRoute, OpenAPIHono, z } from '@hono/zod-openapi';++const app = new OpenAPIHono<{ Bindings: AppBindings }>();++const metricsRoute = createRoute({+ method: 'post',+ path: '/metrics/invocations',+ tags: ['telemetry'],+ request: {+ body: {+ content: {+ 'application/json': {+ schema: TelemetryMetricRequestSchema,+ },+ },+ },+ },+ responses: {+ 200: {+ description: 'The success of the request',+ content: {+ 'application/json': {+ schema: z.object({ success: z.boolean() }),+ },+ },+ },+ 400: {+ description: 'Invalid request body',+ content: {+ 'application/json': {+ schema: z.object({+ success: z.boolean(),+ error: z.string(),+ }),+ },+ },+ },+ },+});++const errorsRoute = createRoute({+ method: 'post',+ path: '/errors',+ tags: ['telemetry'],+ request: {+ body: {+ content: {+ 'application/json': {+ schema: ErrorLogPayloadSchema,+ },+ },+ },+ },+ responses: {+ 200: {+ description: 'The success of the request',+ content: {+ 'application/json': {+ schema: z.object({ success: z.boolean() }),+ },+ },+ },+ 400: {+ description: 'Invalid request body',+ content: {+ 'application/json': {+ schema: z.object({+ success: z.boolean(),+ error: z.string(),+ }),+ },+ },+ },+ },+});++app.openapi(metricsRoute, async c => {+ try {+ const rawBody = await c.req.json();+ const result = TelemetryMetricRequestSchema.safeParse(rawBody);++ if (!result.success) {+ return c.json({ success: false, error: 'Invalid request body' }, 400);+ }++ const apiKey = c.env.DATADOG_API_KEY as string;+ const res = await sendMetricToDatadog(result.data, apiKey);+ return c.json({ success: res, error: '' }, res ? 200 : 400);+ } catch (error) {+ return c.json({ success: false, error: 'Failed to process request' }, 400);+ }+});++app.openapi(errorsRoute, async c => {+ try {+ const rawBody = await c.req.json();+ const result = ErrorLogPayloadSchema.safeParse(rawBody);++ if (!result.success) {+ return c.json({ success: false, error: 'Invalid request body' }, 400);+ }++ const apiKey = c.env.DATADOG_API_KEY as string;+ const res = await sendErrorLogToDatadog(result.data, apiKey);+ return c.json({ success: res, error: '' }, res ? 200 : 400);+ } catch (error) {+ return c.json({ success: false, error: 'Failed to process request' }, 400);+ }+});++export default app;diff --git a/apps/telemetry-proxy/src/types/app.types.ts b/apps/telemetry-proxy/src/types/app.types.tsnew file mode 100644index 000000000..1fe7a1e1f--- /dev/null+++ b/apps/telemetry-proxy/src/types/app.types.ts@@ -0,0 +1,3 @@+export interface AppBindings {+ DATADOG_API_KEY: string;+}diff --git a/apps/telemetry-proxy/src/types/errors.types.ts b/apps/telemetry-proxy/src/types/errors.types.tsnew file mode 100644index 000000000..fe39cadb4--- /dev/null+++ b/apps/telemetry-proxy/src/types/errors.types.ts@@ -0,0 +1,14 @@+import { z } from '@hono/zod-openapi';+import { TelemetryMetricMetadataSchema, TelemetryMetricSourceSchema } from './telemetry.types';++export const ErrorLogPayloadSchema = z.object({+ functionName: z.string().describe('The name of the function that was invoked'),+ errorName: z.string().describe('The name of the error').optional(),+ message: z.string().describe('The message of the error').optional(),+ stack: z.string().describe('The stack trace of the error').optional(),+ timestamp: z.number().describe('The timestamp of the error in epoch milliseconds').optional(),+ props: z.record(z.any()).optional().describe('The properties of the error'),+ source: TelemetryMetricSourceSchema.optional(),+ metadata: TelemetryMetricMetadataSchema.optional(),+});+export type ErrorLogPayload = z.infer<typeof ErrorLogPayloadSchema>;diff --git a/apps/telemetry-proxy/src/types/metrics.types.ts b/apps/telemetry-proxy/src/types/metrics.types.tsnew file mode 100644index 000000000..cafbd8409--- /dev/null+++ b/apps/telemetry-proxy/src/types/metrics.types.ts@@ -0,0 +1,47 @@+import { z } from '@hono/zod-openapi';+import { TelemetryMetricSourceSchema } from './telemetry.types';+import { TelemetryMetricMetadataSchema } from './telemetry.types';++export const TelemetryMetricPayloadSchema = z.object({+ functionName: z.string().describe('The name of the function that was invoked'),+ durationMs: z.number().describe('The duration of the function invocation in milliseconds'),+ timestamp: z.number().describe('The timestamp of the function invocation in epoch milliseconds'),+ props: z.record(z.any()).optional().describe('The properties of the function invocation'),+ source: TelemetryMetricSourceSchema.optional(),+ metadata: TelemetryMetricMetadataSchema.optional(),+ error: z+ .object({+ name: z.string().describe('The name of the error'),+ message: z.string().describe('The message of the error'),+ })+ .optional()+ .describe('The error that occurred during the function invocation'),+});++export type TelemetryMetricSource = z.infer<typeof TelemetryMetricSourceSchema>;+export type TelemetryMetricMetadata = z.infer<typeof TelemetryMetricMetadataSchema>;+export type TelemetryMetricPayload = z.infer<typeof TelemetryMetricPayloadSchema>;++export const TelemetryMetricRequestSchema = z.array(TelemetryMetricPayloadSchema);+export type TelemetryMetricRequest = z.infer<typeof TelemetryMetricRequestSchema>;++// {+// metric: 'composio.sdk.function_invocation',+// type: 'count',+// points: [[Math.floor(Date.now() / 1000), 1]],+// tags: [+// `function:${body.functionName}`,+// ...(body.error ? ['status:error'] : ['status:success']),+// ],+// host: 'composio-sdk',+// },+export const DDMetricPayloadSchema = z.object({+ metric: z.string().describe('The name of the metric'),+ type: z+ .enum(['count', 'gauge', 'histogram', 'distribution', 'rate'])+ .describe('The type of the metric'),+ points: z.array(z.array(z.number())).describe('The points of the metric'),+ tags: z.array(z.string()).describe('The tags of the metric'),+ host: z.string().describe('The host of the metric'),+});+export type DDMetricPayload = z.infer<typeof DDMetricPayloadSchema>;diff --git a/apps/telemetry-proxy/src/types/telemetry.types.ts b/apps/telemetry-proxy/src/types/telemetry.types.tsnew file mode 100644index 000000000..ec6ea7e19--- /dev/null+++ b/apps/telemetry-proxy/src/types/telemetry.types.ts@@ -0,0 +1,20 @@+import { z } from '@hono/zod-openapi';++export const TelemetryMetricSourceSchema = z.object({+ name: z.string().describe('The name of the source').optional(),+ service: z+ .enum(['sdk', 'apollo', 'hermes', 'thermos'])+ .describe('The service of the source')+ .optional(),+ language: z+ .enum(['python', 'typescript', 'go', 'rust'])+ .describe('The language of the function that was invoked')+ .optional(),+ version: z.string().describe('The version of the source').optional(),+ platform: z.string().describe('The platform of the source').optional(),+});++export const TelemetryMetricMetadataSchema = z.object({+ projectId: z.string().describe('The project ID of the source').optional(),+ provider: z.string().describe('The provider used in the source').optional(),+});diff --git a/apps/telemetry-proxy/tsconfig.json b/apps/telemetry-proxy/tsconfig.jsonnew file mode 100644index 000000000..23345376b--- /dev/null+++ b/apps/telemetry-proxy/tsconfig.json@@ -0,0 +1,14 @@+{+ "compilerOptions": {+ "target": "ESNext",+ "module": "ESNext",+ "moduleResolution": "Bundler",+ "strict": true,+ "skipLibCheck": true,+ "lib": [+ "ESNext"+ ],+ "jsx": "react-jsx",+ "jsxImportSource": "hono/jsx"+ },+}\ No newline at end of filediff --git a/apps/telemetry-proxy/wrangler.jsonc b/apps/telemetry-proxy/wrangler.jsoncnew file mode 100644index 000000000..d2fba5215--- /dev/null+++ b/apps/telemetry-proxy/wrangler.jsonc@@ -0,0 +1,38 @@+{+ "$schema": "node_modules/wrangler/config-schema.json",+ "name": "telemetry-proxy",+ "main": "src/index.ts",+ "compatibility_date": "2025-06-11",+ // "compatibility_flags": [+ // "nodejs_compat"+ // ],+ // "vars": {+ // "MY_VAR": "my-variable"+ // },+ // "kv_namespaces": [+ // {+ // "binding": "MY_KV_NAMESPACE",+ // "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"+ // }+ // ],+ // "r2_buckets": [+ // {+ // "binding": "MY_BUCKET",+ // "bucket_name": "my-bucket"+ // }+ // ],+ // "d1_databases": [+ // {+ // "binding": "MY_DB",+ // "database_name": "my-database",+ // "database_id": ""+ // }+ // ],+ // "ai": {+ // "binding": "AI"+ // },+ // "observability": {+ // "enabled": true,+ // "head_sampling_rate": 1+ // }+}