Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import atexit |
| 2 | import functools |
| 3 | import queue as q |
| 4 | import threading as tr |
| 5 | import time |
| 6 | import typing as t |
| 7 | |
| 8 | import httpx |
| 9 | import typing_extensions as te |
| 10 | |
| 11 | TELEMETRY_URL = "https://telemetry.composio.dev/v1" |
| 12 | METRIC_ENDPOINT = f"{TELEMETRY_URL}/metrics/invocations" |
| 13 | ERROR_ENDPOINT = f"{TELEMETRY_URL}/errors" |
| 14 | |
| 15 | |
| 16 | class ErrorData(te.TypedDict): |
| 17 | name: str |
| 18 | "The name of the error" |
| 19 | |
| 20 | code: te.NotRequired[str] |
| 21 | "The code of the error" |
| 22 | |
| 23 | errorId: te.NotRequired[str] |
| 24 | "The error ID of the error" |
| 25 | |
| 26 | message: te.NotRequired[str] |
| 27 | "The message of the error" |
| 28 | |
| 29 | stack: te.NotRequired[str] |
| 30 | "The stack trace of the error" |
| 31 | |
| 32 | |
| 33 | class SourceData(te.TypedDict): |
| 34 | host: te.NotRequired[str] |
| 35 | "The name of the source/host" |
| 36 | |
| 37 | service: te.NotRequired[te.Literal["sdk", "apollo", "hermes", "thermos"]] |
| 38 | "The service of the source" |
| 39 | |
| 40 | language: te.NotRequired[te.Literal["python", "typescript", "go", "rust"]] |
| 41 | "The language of the function that was invoked" |
| 42 | |
| 43 | version: te.NotRequired[str] |
| 44 | "The version of the source" |
| 45 | |
| 46 | platform: te.NotRequired[str] |
| 47 | "The platform of the source" |
| 48 | |
| 49 | environment: te.NotRequired[ |
| 50 | te.Literal["development", "production", "ci", "staging", "test"] |
| 51 | ] |
| 52 | "The environment of the source, eg: development, production, ci etc" |
| 53 | |
| 54 | |
| 55 | class Metadata(te.TypedDict): |
| 56 | projectId: te.NotRequired[str] |
| 57 | "The project ID of the source" |
| 58 | |
| 59 | provider: te.NotRequired[str] |
| 60 | "The provider used in the source" |
| 61 | |
| 62 | |
| 63 | class TelemetryData(te.TypedDict): |
| 64 | functionName: str |
| 65 | "The name of the function that was invoked" |
| 66 | |
| 67 | durationMs: te.NotRequired[float] |
| 68 | "The duration of the function invocation in milliseconds" |
| 69 | |
| 70 | timestamp: te.NotRequired[float] |
| 71 | "The timestamp of the function invocation in epoch seconds" |
| 72 | |
| 73 | props: te.NotRequired[t.Dict] |
| 74 | "The properties of the function invocation" |
| 75 | |
| 76 | source: te.NotRequired[SourceData] |
| 77 | """Source of the metric""" |
| 78 | |
| 79 | metadata: te.NotRequired[Metadata] |
| 80 | """Runtime metadata""" |
| 81 | |
| 82 | error: te.NotRequired[ErrorData] |
| 83 | """Error data.""" |
| 84 | |
| 85 | |
| 86 | EventType: t.TypeAlias = t.Literal["metric", "error"] |
| 87 | Event = t.Tuple[EventType, TelemetryData] |
| 88 | EventQueue: t.TypeAlias = q.Queue[Event] |
| 89 | |
| 90 | _queue: t.Optional[EventQueue] = None |
| 91 | _event: t.Optional[tr.Event] = None |
| 92 | _thread: t.Optional[tr.Thread] = None |
| 93 | |
| 94 | |
| 95 | def _setup(): |
| 96 | global _queue, _event, _thread |
| 97 | if _queue is None: |
| 98 | _queue = q.Queue[Event]() |
| 99 | |
| 100 | if _event is None: |
| 101 | _event = tr.Event() |
| 102 | |
| 103 | if _thread is None: |
| 104 | _thread = tr.Thread( |
| 105 | target=_thread_loop, |
| 106 | kwargs={ |
| 107 | "queue": _queue, |
| 108 | "event": _event, |
| 109 | }, |
| 110 | daemon=True, |
| 111 | ) |
| 112 | _thread.start() |
| 113 | atexit.register( |
| 114 | functools.partial( |
| 115 | _teardown, |
| 116 | queue=_queue, |
| 117 | event=_event, |
| 118 | thread=_thread, |
| 119 | ) |
| 120 | ) |
| 121 | |
| 122 | return _queue, _event, _thread |
| 123 | |
| 124 | |
| 125 | def _teardown(queue: EventQueue, event: tr.Event, thread: tr.Thread): |
| 126 | # Wait max 2 seconds for queue to empty |
| 127 | deadline = time.time() + 2.0 |
| 128 | while queue.qsize() and time.time() < deadline: |
| 129 | time.sleep(0.1) |
| 130 | |
| 131 | event.set() |
| 132 | # Join with timeout to prevent infinite waiting |
| 133 | thread.join(timeout=3.0) |
| 134 | |
| 135 | |
| 136 | def _push(event: Event): |
| 137 | try: |
| 138 | _ = ( |
| 139 | httpx.post( |
| 140 | url=METRIC_ENDPOINT, |
| 141 | json=[event[1]], |
| 142 | timeout=2.0, # 2 second timeout to prevent hanging |
| 143 | ) |
| 144 | if event[0] == "metric" |
| 145 | else httpx.post( |
| 146 | url=ERROR_ENDPOINT, |
| 147 | json=event[1], |
| 148 | timeout=2.0, # 2 second timeout to prevent hanging |
| 149 | ) |
| 150 | ) |
| 151 | # Telemetry failures must not fail an otherwise successful SDK operation. |
| 152 | # ast-grep-ignore: py-empty-except |
| 153 | except (httpx.TimeoutException, httpx.HTTPError, Exception): |
| 154 | pass |
| 155 | |
| 156 | |
| 157 | def _thread_loop(queue: EventQueue, event: tr.Event): |
| 158 | while not event.is_set(): |
| 159 | try: |
| 160 | _push(queue.get(timeout=0.1)) |
| 161 | except q.Empty: |
| 162 | continue |
| 163 | |
| 164 | |
| 165 | def push_event(event: Event): |
| 166 | q, _, _ = _setup() |
| 167 | q.put(event) |
| 168 | |
| 169 | |
| 170 | def create_event(type: EventType, **payload: te.Unpack[TelemetryData]) -> Event: |
| 171 | return type, payload |