autotel-cloudflare 3.1.0 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents.d.ts.map +1 -1
- package/dist/agents.js +7 -0
- package/dist/agents.js.map +1 -1
- package/package.json +8 -4
- package/src/actors/alarms.ts +0 -225
- package/src/actors/index.ts +0 -36
- package/src/actors/instrument-actor.test.ts +0 -179
- package/src/actors/instrument-actor.ts +0 -574
- package/src/actors/sockets.ts +0 -217
- package/src/actors/storage.ts +0 -263
- package/src/actors/traced-handler.ts +0 -300
- package/src/actors/types.ts +0 -98
- package/src/actors.ts +0 -50
- package/src/agents/agent.ts +0 -327
- package/src/agents/base.ts +0 -26
- package/src/agents/index.ts +0 -46
- package/src/agents/mcp.ts +0 -38
- package/src/agents/observability.ts +0 -145
- package/src/agents/otel-observability.test.ts +0 -269
- package/src/agents/otel-observability.ts +0 -557
- package/src/agents/types.ts +0 -61
- package/src/agents.ts +0 -87
- package/src/bindings/ai.test.ts +0 -170
- package/src/bindings/ai.ts +0 -73
- package/src/bindings/analytics-engine.test.ts +0 -175
- package/src/bindings/analytics-engine.ts +0 -78
- package/src/bindings/bindings-cache.test.ts +0 -80
- package/src/bindings/bindings-detection.test.ts +0 -235
- package/src/bindings/bindings-this-binding.test.ts +0 -294
- package/src/bindings/bindings.ts +0 -720
- package/src/bindings/browser-rendering.test.ts +0 -160
- package/src/bindings/browser-rendering.ts +0 -70
- package/src/bindings/common.ts +0 -84
- package/src/bindings/hyperdrive.test.ts +0 -176
- package/src/bindings/hyperdrive.ts +0 -74
- package/src/bindings/images.test.ts +0 -262
- package/src/bindings/images.ts +0 -182
- package/src/bindings/index.ts +0 -20
- package/src/bindings/queue-producer.test.ts +0 -224
- package/src/bindings/queue-producer.ts +0 -105
- package/src/bindings/rate-limiter.test.ts +0 -140
- package/src/bindings/rate-limiter.ts +0 -69
- package/src/bindings/vectorize.test.ts +0 -362
- package/src/bindings/vectorize.ts +0 -86
- package/src/bindings.ts +0 -6
- package/src/events.ts +0 -6
- package/src/execution-logger.test.ts +0 -127
- package/src/execution-logger.ts +0 -112
- package/src/global/cache.test.ts +0 -292
- package/src/global/cache.ts +0 -164
- package/src/global/fetch.test.ts +0 -399
- package/src/global/fetch.ts +0 -136
- package/src/global/index.ts +0 -7
- package/src/handlers/durable-objects.test.ts +0 -524
- package/src/handlers/durable-objects.ts +0 -250
- package/src/handlers/index.ts +0 -6
- package/src/handlers/workflows.test.ts +0 -411
- package/src/handlers/workflows.ts +0 -345
- package/src/handlers.ts +0 -6
- package/src/index.ts +0 -91
- package/src/logger.ts +0 -15
- package/src/native/native-tracing.test.ts +0 -54
- package/src/native/native-tracing.ts +0 -83
- package/src/native.ts +0 -12
- package/src/parse-error.ts +0 -1
- package/src/sampling.ts +0 -6
- package/src/testing.ts +0 -6
- package/src/wrappers/cf-attributes.test.ts +0 -334
- package/src/wrappers/define-worker-fetch.test.ts +0 -102
- package/src/wrappers/define-worker-fetch.ts +0 -71
- package/src/wrappers/index.ts +0 -13
- package/src/wrappers/instrument.integration.test.ts +0 -578
- package/src/wrappers/instrument.ts +0 -806
- package/src/wrappers/native-instrument.test.ts +0 -153
- package/src/wrappers/wrap-do.ts +0 -34
- package/src/wrappers/wrap-module.ts +0 -37
|
@@ -1,557 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OpenTelemetry-based Observability implementation for Cloudflare Agents SDK
|
|
3
|
-
*
|
|
4
|
-
* Converts Agent events into OpenTelemetry spans for distributed tracing.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
SpanKind,
|
|
9
|
-
SpanStatusCode,
|
|
10
|
-
trace,
|
|
11
|
-
type Attributes,
|
|
12
|
-
type Span,
|
|
13
|
-
} from '@opentelemetry/api';
|
|
14
|
-
import { resourceFromAttributes } from '@opentelemetry/resources';
|
|
15
|
-
import {
|
|
16
|
-
WorkerTracer,
|
|
17
|
-
WorkerTracerProvider,
|
|
18
|
-
createInitialiser,
|
|
19
|
-
type ResolvedEdgeConfig,
|
|
20
|
-
} from 'autotel-edge';
|
|
21
|
-
import type {
|
|
22
|
-
AgentInstrumentationOptions,
|
|
23
|
-
MCPObservabilityEvent,
|
|
24
|
-
Observability,
|
|
25
|
-
ObservabilityEvent,
|
|
26
|
-
ObservabilityExecutionContext,
|
|
27
|
-
OtelObservabilityConfig,
|
|
28
|
-
} from './types';
|
|
29
|
-
|
|
30
|
-
let providerInitialized = false;
|
|
31
|
-
|
|
32
|
-
type EventCategory =
|
|
33
|
-
| 'state'
|
|
34
|
-
| 'rpc'
|
|
35
|
-
| 'schedule'
|
|
36
|
-
| 'queue'
|
|
37
|
-
| 'submission'
|
|
38
|
-
| 'message'
|
|
39
|
-
| 'chat'
|
|
40
|
-
| 'transcript'
|
|
41
|
-
| 'fiber'
|
|
42
|
-
| 'agentTool'
|
|
43
|
-
| 'lifecycle'
|
|
44
|
-
| 'workflow'
|
|
45
|
-
| 'mcp'
|
|
46
|
-
| 'email';
|
|
47
|
-
|
|
48
|
-
function initProvider(config: ResolvedEdgeConfig): void {
|
|
49
|
-
if (providerInitialized) return;
|
|
50
|
-
|
|
51
|
-
const resource = resourceFromAttributes({
|
|
52
|
-
'service.name': config.service.name,
|
|
53
|
-
'service.version': config.service.version,
|
|
54
|
-
'service.namespace': config.service.namespace,
|
|
55
|
-
'cloud.provider': 'cloudflare',
|
|
56
|
-
'cloud.platform': 'cloudflare.workers',
|
|
57
|
-
'telemetry.sdk.name': 'autotel-cloudflare',
|
|
58
|
-
'telemetry.sdk.language': 'js',
|
|
59
|
-
'agent.framework': 'cloudflare-agents',
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
const provider = new WorkerTracerProvider(config.spanProcessors, resource);
|
|
63
|
-
provider.register();
|
|
64
|
-
|
|
65
|
-
const tracer = trace.getTracer('autotel-cloudflare/agents') as WorkerTracer;
|
|
66
|
-
tracer.setHeadSampler(config.sampling.headSampler);
|
|
67
|
-
|
|
68
|
-
providerInitialized = true;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function classifyEvent(type: ObservabilityEvent['type']): EventCategory {
|
|
72
|
-
if (type.startsWith('mcp:')) return 'mcp';
|
|
73
|
-
if (type.startsWith('workflow:')) return 'workflow';
|
|
74
|
-
if (type.startsWith('fiber:')) return 'fiber';
|
|
75
|
-
if (type.startsWith('agent_tool:')) return 'agentTool';
|
|
76
|
-
if (type.startsWith('chat:transcript:')) return 'transcript';
|
|
77
|
-
if (type.startsWith('chat:')) return 'chat';
|
|
78
|
-
if (type.startsWith('submission:')) return 'submission';
|
|
79
|
-
if (type.startsWith('queue:')) return 'queue';
|
|
80
|
-
if (type.startsWith('schedule:')) return 'schedule';
|
|
81
|
-
if (
|
|
82
|
-
type.startsWith('message:') ||
|
|
83
|
-
type.startsWith('tool:') ||
|
|
84
|
-
type === 'rpc:error'
|
|
85
|
-
) {
|
|
86
|
-
return 'message';
|
|
87
|
-
}
|
|
88
|
-
if (type === 'rpc') return 'rpc';
|
|
89
|
-
if (type.startsWith('state:')) return 'state';
|
|
90
|
-
if (type.startsWith('email:')) return 'email';
|
|
91
|
-
if (type === 'connect' || type === 'disconnect' || type === 'destroy') {
|
|
92
|
-
return 'lifecycle';
|
|
93
|
-
}
|
|
94
|
-
return 'message';
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function shouldTraceEvent(
|
|
98
|
-
event: ObservabilityEvent,
|
|
99
|
-
options: AgentInstrumentationOptions,
|
|
100
|
-
): boolean {
|
|
101
|
-
const opts: Required<
|
|
102
|
-
Pick<
|
|
103
|
-
AgentInstrumentationOptions,
|
|
104
|
-
| 'traceRpc'
|
|
105
|
-
| 'traceSchedule'
|
|
106
|
-
| 'traceQueue'
|
|
107
|
-
| 'traceSubmissions'
|
|
108
|
-
| 'traceMcp'
|
|
109
|
-
| 'traceStateUpdates'
|
|
110
|
-
| 'traceMessages'
|
|
111
|
-
| 'traceLifecycle'
|
|
112
|
-
| 'traceChat'
|
|
113
|
-
| 'traceTranscripts'
|
|
114
|
-
| 'traceFibers'
|
|
115
|
-
| 'traceToolRecovery'
|
|
116
|
-
| 'traceWorkflow'
|
|
117
|
-
| 'traceEmail'
|
|
118
|
-
>
|
|
119
|
-
> = {
|
|
120
|
-
traceRpc: true,
|
|
121
|
-
traceSchedule: true,
|
|
122
|
-
traceQueue: true,
|
|
123
|
-
traceSubmissions: true,
|
|
124
|
-
traceMcp: true,
|
|
125
|
-
traceStateUpdates: false,
|
|
126
|
-
traceMessages: true,
|
|
127
|
-
traceLifecycle: true,
|
|
128
|
-
traceChat: true,
|
|
129
|
-
traceTranscripts: true,
|
|
130
|
-
traceFibers: true,
|
|
131
|
-
traceToolRecovery: true,
|
|
132
|
-
traceWorkflow: true,
|
|
133
|
-
traceEmail: true,
|
|
134
|
-
};
|
|
135
|
-
const merged = { ...opts, ...options };
|
|
136
|
-
|
|
137
|
-
switch (classifyEvent(event.type)) {
|
|
138
|
-
case 'rpc': {
|
|
139
|
-
return merged.traceRpc;
|
|
140
|
-
}
|
|
141
|
-
case 'schedule': {
|
|
142
|
-
return merged.traceSchedule;
|
|
143
|
-
}
|
|
144
|
-
case 'queue': {
|
|
145
|
-
return merged.traceQueue;
|
|
146
|
-
}
|
|
147
|
-
case 'submission': {
|
|
148
|
-
return merged.traceSubmissions;
|
|
149
|
-
}
|
|
150
|
-
case 'mcp': {
|
|
151
|
-
return merged.traceMcp;
|
|
152
|
-
}
|
|
153
|
-
case 'state': {
|
|
154
|
-
return merged.traceStateUpdates;
|
|
155
|
-
}
|
|
156
|
-
case 'message': {
|
|
157
|
-
return merged.traceMessages;
|
|
158
|
-
}
|
|
159
|
-
case 'lifecycle': {
|
|
160
|
-
return merged.traceLifecycle;
|
|
161
|
-
}
|
|
162
|
-
case 'chat': {
|
|
163
|
-
return merged.traceChat;
|
|
164
|
-
}
|
|
165
|
-
case 'transcript': {
|
|
166
|
-
return merged.traceTranscripts;
|
|
167
|
-
}
|
|
168
|
-
case 'fiber': {
|
|
169
|
-
return merged.traceFibers;
|
|
170
|
-
}
|
|
171
|
-
case 'agentTool': {
|
|
172
|
-
return merged.traceToolRecovery;
|
|
173
|
-
}
|
|
174
|
-
case 'workflow': {
|
|
175
|
-
return merged.traceWorkflow;
|
|
176
|
-
}
|
|
177
|
-
case 'email': {
|
|
178
|
-
return merged.traceEmail;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
function formatTypeSegment(value: string): string {
|
|
184
|
-
return value.replaceAll(/[:_]/g, '.');
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function getPayloadName(
|
|
188
|
-
payload: Record<string, unknown>,
|
|
189
|
-
...keys: string[]
|
|
190
|
-
): string | undefined {
|
|
191
|
-
for (const key of keys) {
|
|
192
|
-
const value = payload[key];
|
|
193
|
-
if (typeof value === 'string' && value.length > 0) {
|
|
194
|
-
return value;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return undefined;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
function getDefaultSpanName(event: ObservabilityEvent): string {
|
|
201
|
-
const payload = event.payload;
|
|
202
|
-
|
|
203
|
-
switch (event.type) {
|
|
204
|
-
case 'rpc':
|
|
205
|
-
case 'rpc:error': {
|
|
206
|
-
return `agent.rpc ${(payload as { method: string }).method}`;
|
|
207
|
-
}
|
|
208
|
-
case 'schedule:create':
|
|
209
|
-
case 'schedule:execute':
|
|
210
|
-
case 'schedule:cancel':
|
|
211
|
-
case 'schedule:retry':
|
|
212
|
-
case 'schedule:error':
|
|
213
|
-
case 'schedule:duplicate_warning': {
|
|
214
|
-
return `agent.${formatTypeSegment(event.type)} ${getPayloadName(payload, 'callback') ?? 'unknown'}`;
|
|
215
|
-
}
|
|
216
|
-
case 'queue:create':
|
|
217
|
-
case 'queue:retry':
|
|
218
|
-
case 'queue:error': {
|
|
219
|
-
return `agent.${formatTypeSegment(event.type)} ${getPayloadName(payload, 'callback') ?? 'unknown'}`;
|
|
220
|
-
}
|
|
221
|
-
case 'submission:create':
|
|
222
|
-
case 'submission:status':
|
|
223
|
-
case 'submission:error': {
|
|
224
|
-
return `agent.${formatTypeSegment(event.type)} ${getPayloadName(payload, 'submissionId') ?? 'unknown'}`;
|
|
225
|
-
}
|
|
226
|
-
case 'connect':
|
|
227
|
-
case 'disconnect': {
|
|
228
|
-
return `agent.${event.type} ${getPayloadName(payload, 'connectionId') ?? 'unknown'}`;
|
|
229
|
-
}
|
|
230
|
-
case 'mcp:client:preconnect': {
|
|
231
|
-
return `mcp.client.preconnect ${getPayloadName(payload, 'serverId') ?? 'unknown'}`;
|
|
232
|
-
}
|
|
233
|
-
case 'mcp:client:authorize': {
|
|
234
|
-
return `mcp.client.authorize ${getPayloadName(payload, 'serverId') ?? 'unknown'}`;
|
|
235
|
-
}
|
|
236
|
-
case 'mcp:client:connect':
|
|
237
|
-
case 'mcp:client:discover':
|
|
238
|
-
case 'mcp:client:close': {
|
|
239
|
-
return `mcp.client.${formatTypeSegment(event.type).split('.').slice(-1)[0]} ${
|
|
240
|
-
getPayloadName(payload, 'url', 'capability', 'state') ?? 'unknown'
|
|
241
|
-
}`;
|
|
242
|
-
}
|
|
243
|
-
case 'workflow:start':
|
|
244
|
-
case 'workflow:event':
|
|
245
|
-
case 'workflow:approved':
|
|
246
|
-
case 'workflow:rejected':
|
|
247
|
-
case 'workflow:terminated':
|
|
248
|
-
case 'workflow:paused':
|
|
249
|
-
case 'workflow:resumed':
|
|
250
|
-
case 'workflow:restarted': {
|
|
251
|
-
return `agent.${formatTypeSegment(event.type)} ${
|
|
252
|
-
getPayloadName(payload, 'workflowName', 'workflowId', 'eventType') ?? 'unknown'
|
|
253
|
-
}`;
|
|
254
|
-
}
|
|
255
|
-
case 'email:receive':
|
|
256
|
-
case 'email:reply':
|
|
257
|
-
case 'email:send': {
|
|
258
|
-
return `agent.${formatTypeSegment(event.type)} ${getPayloadName(payload, 'subject') ?? 'message'}`;
|
|
259
|
-
}
|
|
260
|
-
case 'destroy': {
|
|
261
|
-
return 'agent.destroy';
|
|
262
|
-
}
|
|
263
|
-
default: {
|
|
264
|
-
return `agent.${formatTypeSegment(event.type)}`;
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
function isPrimitiveAttributeValue(
|
|
270
|
-
value: unknown,
|
|
271
|
-
): value is string | number | boolean | string[] | number[] | boolean[] {
|
|
272
|
-
if (
|
|
273
|
-
typeof value === 'string' ||
|
|
274
|
-
typeof value === 'number' ||
|
|
275
|
-
typeof value === 'boolean'
|
|
276
|
-
) {
|
|
277
|
-
return true;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
if (!Array.isArray(value)) {
|
|
281
|
-
return false;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
return value.every(
|
|
285
|
-
(entry) =>
|
|
286
|
-
typeof entry === 'string' ||
|
|
287
|
-
typeof entry === 'number' ||
|
|
288
|
-
typeof entry === 'boolean',
|
|
289
|
-
);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
function setIfPresent(
|
|
293
|
-
attrs: Attributes,
|
|
294
|
-
key: string,
|
|
295
|
-
value: unknown,
|
|
296
|
-
): void {
|
|
297
|
-
if (value === undefined || value === null) return;
|
|
298
|
-
if (isPrimitiveAttributeValue(value)) {
|
|
299
|
-
attrs[key] = value;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
function getDefaultAttributes(event: ObservabilityEvent): Attributes {
|
|
304
|
-
const attrs: Attributes = {
|
|
305
|
-
'agent.event.type': event.type,
|
|
306
|
-
};
|
|
307
|
-
|
|
308
|
-
setIfPresent(attrs, 'agent.event.id', event.id);
|
|
309
|
-
setIfPresent(attrs, 'agent.display_message', event.displayMessage);
|
|
310
|
-
setIfPresent(attrs, 'agent.class', event.agent);
|
|
311
|
-
setIfPresent(attrs, 'agent.instance.name', event.name);
|
|
312
|
-
setIfPresent(attrs, 'gen_ai.agent.name', event.agent);
|
|
313
|
-
|
|
314
|
-
switch (event.type) {
|
|
315
|
-
case 'rpc': {
|
|
316
|
-
attrs['agent.rpc.method'] = event.payload.method;
|
|
317
|
-
setIfPresent(attrs, 'agent.rpc.streaming', event.payload.streaming);
|
|
318
|
-
break;
|
|
319
|
-
}
|
|
320
|
-
case 'rpc:error': {
|
|
321
|
-
attrs['agent.rpc.method'] = event.payload.method;
|
|
322
|
-
break;
|
|
323
|
-
}
|
|
324
|
-
case 'schedule:create':
|
|
325
|
-
case 'schedule:execute':
|
|
326
|
-
case 'schedule:cancel':
|
|
327
|
-
case 'schedule:retry':
|
|
328
|
-
case 'schedule:error':
|
|
329
|
-
case 'queue:create':
|
|
330
|
-
case 'queue:retry':
|
|
331
|
-
case 'queue:error': {
|
|
332
|
-
setIfPresent(attrs, 'agent.schedule.callback', event.payload.callback);
|
|
333
|
-
setIfPresent(attrs, 'agent.schedule.id', event.payload.id);
|
|
334
|
-
break;
|
|
335
|
-
}
|
|
336
|
-
case 'schedule:duplicate_warning': {
|
|
337
|
-
setIfPresent(attrs, 'agent.schedule.callback', event.payload.callback);
|
|
338
|
-
break;
|
|
339
|
-
}
|
|
340
|
-
case 'connect':
|
|
341
|
-
case 'disconnect': {
|
|
342
|
-
setIfPresent(attrs, 'agent.connection.id', event.payload.connectionId);
|
|
343
|
-
break;
|
|
344
|
-
}
|
|
345
|
-
case 'mcp:client:preconnect': {
|
|
346
|
-
setIfPresent(attrs, 'agent.mcp.server_id', event.payload.serverId);
|
|
347
|
-
break;
|
|
348
|
-
}
|
|
349
|
-
case 'mcp:client:connect':
|
|
350
|
-
case 'mcp:client:close': {
|
|
351
|
-
setIfPresent(attrs, 'agent.mcp.url', event.payload.url);
|
|
352
|
-
setIfPresent(attrs, 'agent.mcp.transport', event.payload.transport);
|
|
353
|
-
setIfPresent(attrs, 'agent.mcp.state', event.payload.state);
|
|
354
|
-
break;
|
|
355
|
-
}
|
|
356
|
-
case 'mcp:client:authorize': {
|
|
357
|
-
setIfPresent(attrs, 'agent.mcp.server_id', event.payload.serverId);
|
|
358
|
-
setIfPresent(attrs, 'agent.mcp.auth_url', event.payload.authUrl);
|
|
359
|
-
setIfPresent(attrs, 'agent.mcp.client_id', event.payload.clientId);
|
|
360
|
-
break;
|
|
361
|
-
}
|
|
362
|
-
case 'mcp:client:discover': {
|
|
363
|
-
setIfPresent(attrs, 'agent.mcp.url', event.payload.url);
|
|
364
|
-
setIfPresent(attrs, 'agent.mcp.state', event.payload.state);
|
|
365
|
-
setIfPresent(attrs, 'agent.mcp.capability', event.payload.capability);
|
|
366
|
-
break;
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
for (const [key, value] of Object.entries(event.payload)) {
|
|
371
|
-
setIfPresent(attrs, `agent.payload.${key}`, value);
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
return attrs;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
function getSpanKind(event: ObservabilityEvent): SpanKind {
|
|
378
|
-
switch (classifyEvent(event.type)) {
|
|
379
|
-
case 'rpc':
|
|
380
|
-
case 'lifecycle':
|
|
381
|
-
case 'email': {
|
|
382
|
-
return SpanKind.SERVER;
|
|
383
|
-
}
|
|
384
|
-
case 'mcp': {
|
|
385
|
-
return SpanKind.CLIENT;
|
|
386
|
-
}
|
|
387
|
-
case 'schedule':
|
|
388
|
-
case 'queue': {
|
|
389
|
-
return SpanKind.CONSUMER;
|
|
390
|
-
}
|
|
391
|
-
default: {
|
|
392
|
-
return SpanKind.INTERNAL;
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
function inferErrorMessage(event: ObservabilityEvent): string | undefined {
|
|
398
|
-
const payload = event.payload as Record<string, unknown>;
|
|
399
|
-
const direct = payload.error;
|
|
400
|
-
if (typeof direct === 'string' && direct.length > 0) {
|
|
401
|
-
return direct;
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
if (event.type === 'workflow:rejected') {
|
|
405
|
-
const reason = payload.reason;
|
|
406
|
-
if (typeof reason === 'string' && reason.length > 0) {
|
|
407
|
-
return reason;
|
|
408
|
-
}
|
|
409
|
-
return 'workflow rejected';
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
if (event.type.endsWith(':failed') || event.type.endsWith(':error')) {
|
|
413
|
-
return event.type;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
return undefined;
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
function isErrorEvent(event: ObservabilityEvent): boolean {
|
|
420
|
-
if (event.type === 'workflow:rejected') return true;
|
|
421
|
-
if (event.type.endsWith(':error') || event.type.endsWith(':failed')) return true;
|
|
422
|
-
|
|
423
|
-
if ('error' in event.payload) {
|
|
424
|
-
return typeof (event.payload as Record<string, unknown>).error === 'string';
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
return false;
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
function applyEventStatus(span: Span, event: ObservabilityEvent): void {
|
|
431
|
-
if (!isErrorEvent(event)) {
|
|
432
|
-
span.setStatus({ code: SpanStatusCode.OK });
|
|
433
|
-
return;
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
const message = inferErrorMessage(event);
|
|
437
|
-
span.setStatus({
|
|
438
|
-
code: SpanStatusCode.ERROR,
|
|
439
|
-
message,
|
|
440
|
-
});
|
|
441
|
-
span.setAttribute('error', true);
|
|
442
|
-
|
|
443
|
-
if (message) {
|
|
444
|
-
span.setAttribute('exception.message', message);
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
async function exportSpans(
|
|
449
|
-
traceId: string,
|
|
450
|
-
ctx?: ObservabilityExecutionContext,
|
|
451
|
-
): Promise<void> {
|
|
452
|
-
const tracer = trace.getTracer('autotel-cloudflare/agents');
|
|
453
|
-
if (tracer instanceof WorkerTracer) {
|
|
454
|
-
try {
|
|
455
|
-
const scheduler = ctx && 'scheduler' in ctx
|
|
456
|
-
? (ctx.scheduler as { wait?: (ms: number) => Promise<void> } | undefined)
|
|
457
|
-
: undefined;
|
|
458
|
-
if (scheduler?.wait) {
|
|
459
|
-
await scheduler.wait(1);
|
|
460
|
-
}
|
|
461
|
-
await tracer.forceFlush(traceId);
|
|
462
|
-
} catch (error) {
|
|
463
|
-
console.error('[autotel-cloudflare/agents] Failed to export spans:', error);
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
/**
|
|
469
|
-
* OpenTelemetry-based Observability implementation
|
|
470
|
-
*
|
|
471
|
-
* Implements the Agents SDK Observability interface and converts
|
|
472
|
-
* events into OpenTelemetry spans.
|
|
473
|
-
*/
|
|
474
|
-
export class OtelObservability implements Observability {
|
|
475
|
-
private readonly config: ResolvedEdgeConfig;
|
|
476
|
-
private readonly options: AgentInstrumentationOptions;
|
|
477
|
-
private initialized = false;
|
|
478
|
-
|
|
479
|
-
constructor(config: OtelObservabilityConfig) {
|
|
480
|
-
const initialiser = createInitialiser(config);
|
|
481
|
-
this.config = initialiser({}, undefined);
|
|
482
|
-
this.options = config.agents ?? {};
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
private initialize(): void {
|
|
486
|
-
if (this.initialized) return;
|
|
487
|
-
initProvider(this.config);
|
|
488
|
-
this.initialized = true;
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
emit(event: ObservabilityEvent, ctx?: ObservabilityExecutionContext): void {
|
|
492
|
-
this.initialize();
|
|
493
|
-
|
|
494
|
-
if (!shouldTraceEvent(event, this.options)) {
|
|
495
|
-
return;
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
const tracer = trace.getTracer('autotel-cloudflare/agents');
|
|
499
|
-
const spanName = this.options.spanNameFormatter
|
|
500
|
-
? this.options.spanNameFormatter(event)
|
|
501
|
-
: getDefaultSpanName(event);
|
|
502
|
-
const defaultAttrs = getDefaultAttributes(event);
|
|
503
|
-
const customAttrs = this.options.attributeExtractor
|
|
504
|
-
? this.options.attributeExtractor(event)
|
|
505
|
-
: {};
|
|
506
|
-
const span = tracer.startSpan(spanName, {
|
|
507
|
-
kind: getSpanKind(event),
|
|
508
|
-
attributes: { ...defaultAttrs, ...customAttrs },
|
|
509
|
-
startTime: event.timestamp,
|
|
510
|
-
});
|
|
511
|
-
|
|
512
|
-
applyEventStatus(span, event);
|
|
513
|
-
span.end(event.timestamp + 1);
|
|
514
|
-
|
|
515
|
-
const traceId = span.spanContext().traceId;
|
|
516
|
-
if (ctx && 'waitUntil' in ctx && typeof ctx.waitUntil === 'function') {
|
|
517
|
-
ctx.waitUntil(exportSpans(traceId, ctx));
|
|
518
|
-
return;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
void exportSpans(traceId, ctx);
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
export function createOtelObservability(
|
|
526
|
-
config: OtelObservabilityConfig,
|
|
527
|
-
): OtelObservability {
|
|
528
|
-
return new OtelObservability(config);
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
export function createOtelObservabilityFromEnv(
|
|
532
|
-
env: Record<string, unknown>,
|
|
533
|
-
options?: AgentInstrumentationOptions,
|
|
534
|
-
): OtelObservability {
|
|
535
|
-
const endpoint = (env.OTEL_EXPORTER_OTLP_ENDPOINT as string) || undefined;
|
|
536
|
-
const serviceName = (env.OTEL_SERVICE_NAME as string) || 'cloudflare-agent';
|
|
537
|
-
|
|
538
|
-
let headers: Record<string, string> | undefined;
|
|
539
|
-
const headersStr = env.OTEL_EXPORTER_OTLP_HEADERS as string;
|
|
540
|
-
if (headersStr) {
|
|
541
|
-
headers = {};
|
|
542
|
-
for (const pair of headersStr.split(',')) {
|
|
543
|
-
const [key, value] = pair.split('=');
|
|
544
|
-
if (key && value) {
|
|
545
|
-
headers[key.trim()] = value.trim();
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
return createOtelObservability({
|
|
551
|
-
service: { name: serviceName },
|
|
552
|
-
exporter: { url: endpoint || 'http://localhost:4318/v1/traces', headers },
|
|
553
|
-
agents: options,
|
|
554
|
-
});
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
export type { MCPObservabilityEvent };
|
package/src/agents/types.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Type definitions for Cloudflare Agents SDK observability integration
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import type { Attributes } from '@opentelemetry/api';
|
|
6
|
-
import type { ConfigurationOption } from 'autotel-edge';
|
|
7
|
-
import type { AgentObservabilityEvent } from './agent';
|
|
8
|
-
import type { BaseEvent } from './base';
|
|
9
|
-
import type { MCPObservabilityEvent } from './mcp';
|
|
10
|
-
import type {
|
|
11
|
-
ChannelEventMap,
|
|
12
|
-
Observability,
|
|
13
|
-
ObservabilityEvent,
|
|
14
|
-
} from './observability';
|
|
15
|
-
|
|
16
|
-
export type { BaseEvent, AgentObservabilityEvent, MCPObservabilityEvent };
|
|
17
|
-
export type { Observability, ObservabilityEvent, ChannelEventMap };
|
|
18
|
-
|
|
19
|
-
export type ObservabilityExecutionContext = DurableObjectState | ExecutionContext;
|
|
20
|
-
|
|
21
|
-
export interface AgentInstrumentationOptions {
|
|
22
|
-
traceRpc?: boolean;
|
|
23
|
-
traceSchedule?: boolean;
|
|
24
|
-
traceQueue?: boolean;
|
|
25
|
-
traceSubmissions?: boolean;
|
|
26
|
-
traceMcp?: boolean;
|
|
27
|
-
traceStateUpdates?: boolean;
|
|
28
|
-
traceMessages?: boolean;
|
|
29
|
-
traceLifecycle?: boolean;
|
|
30
|
-
traceChat?: boolean;
|
|
31
|
-
traceTranscripts?: boolean;
|
|
32
|
-
traceFibers?: boolean;
|
|
33
|
-
traceToolRecovery?: boolean;
|
|
34
|
-
traceWorkflow?: boolean;
|
|
35
|
-
traceEmail?: boolean;
|
|
36
|
-
attributeExtractor?: (event: ObservabilityEvent) => Attributes;
|
|
37
|
-
spanNameFormatter?: (event: ObservabilityEvent) => string;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export type OtelObservabilityConfig = ConfigurationOption & {
|
|
41
|
-
agents?: AgentInstrumentationOptions;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export interface AgentSpanAttributes {
|
|
45
|
-
'agent.event.type': string;
|
|
46
|
-
'agent.event.id'?: string;
|
|
47
|
-
'agent.class'?: string;
|
|
48
|
-
'agent.instance.name'?: string;
|
|
49
|
-
'agent.display_message'?: string;
|
|
50
|
-
'agent.framework'?: string;
|
|
51
|
-
'gen_ai.agent.name'?: string;
|
|
52
|
-
'agent.rpc.method'?: string;
|
|
53
|
-
'agent.rpc.streaming'?: boolean;
|
|
54
|
-
'agent.schedule.callback'?: string;
|
|
55
|
-
'agent.schedule.id'?: string;
|
|
56
|
-
'agent.connection.id'?: string;
|
|
57
|
-
'agent.mcp.server_id'?: string;
|
|
58
|
-
'agent.mcp.url'?: string;
|
|
59
|
-
'agent.mcp.transport'?: string;
|
|
60
|
-
'agent.mcp.state'?: string;
|
|
61
|
-
}
|
package/src/agents.ts
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cloudflare Agents SDK integration entry point
|
|
3
|
-
*
|
|
4
|
-
* Provides an OpenTelemetry-based Observability implementation for the
|
|
5
|
-
* Cloudflare Agents SDK (https://github.com/cloudflare/agents).
|
|
6
|
-
*
|
|
7
|
-
* @example Basic Usage
|
|
8
|
-
* ```typescript
|
|
9
|
-
* import { Agent } from 'agents'
|
|
10
|
-
* import { createOtelObservability } from 'autotel-cloudflare/agents'
|
|
11
|
-
*
|
|
12
|
-
* class MyAgent extends Agent<Env> {
|
|
13
|
-
* // Replace default observability with OpenTelemetry
|
|
14
|
-
* observability = createOtelObservability({
|
|
15
|
-
* service: { name: 'my-agent' },
|
|
16
|
-
* exporter: { url: env.OTLP_ENDPOINT }
|
|
17
|
-
* })
|
|
18
|
-
*
|
|
19
|
-
* @callable()
|
|
20
|
-
* async processTask(task: string) {
|
|
21
|
-
* // All RPC calls are automatically traced
|
|
22
|
-
* return { result: 'done' }
|
|
23
|
-
* }
|
|
24
|
-
* }
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* @example Environment-Based Configuration
|
|
28
|
-
* ```typescript
|
|
29
|
-
* import { Agent } from 'agents'
|
|
30
|
-
* import { createOtelObservabilityFromEnv, OtelObservability } from 'autotel-cloudflare/agents'
|
|
31
|
-
*
|
|
32
|
-
* class MyAgent extends Agent<Env> {
|
|
33
|
-
* observability?: OtelObservability
|
|
34
|
-
*
|
|
35
|
-
* constructor(state: DurableObjectState, env: Env) {
|
|
36
|
-
* super(state, env)
|
|
37
|
-
* // Automatically reads OTEL_* environment variables
|
|
38
|
-
* this.observability = createOtelObservabilityFromEnv(env)
|
|
39
|
-
* }
|
|
40
|
-
* }
|
|
41
|
-
* ```
|
|
42
|
-
*
|
|
43
|
-
* @example Selective Tracing
|
|
44
|
-
* ```typescript
|
|
45
|
-
* import { createOtelObservability } from 'autotel-cloudflare/agents'
|
|
46
|
-
*
|
|
47
|
-
* const observability = createOtelObservability({
|
|
48
|
-
* service: { name: 'my-agent' },
|
|
49
|
-
* agents: {
|
|
50
|
-
* traceRpc: true, // RPC and rpc:error
|
|
51
|
-
* traceSchedule: true, // schedule:* events
|
|
52
|
-
* traceQueue: true, // queue:* events
|
|
53
|
-
* traceSubmissions: true, // submission:* events
|
|
54
|
-
* traceMcp: true, // mcp:* events
|
|
55
|
-
* traceStateUpdates: false, // state:update (off by default; noisy)
|
|
56
|
-
* traceMessages: true, // message:* and tool:* events
|
|
57
|
-
* traceChat: true, // chat:* recovery/stream/context events
|
|
58
|
-
* traceTranscripts: true, // chat:transcript:* events
|
|
59
|
-
* traceFibers: true, // fiber:* events
|
|
60
|
-
* traceToolRecovery: true, // agent_tool:* recovery events
|
|
61
|
-
* traceWorkflow: true, // workflow:* events
|
|
62
|
-
* traceEmail: true, // email:* events
|
|
63
|
-
* traceLifecycle: true, // connect/disconnect/destroy
|
|
64
|
-
* }
|
|
65
|
-
* })
|
|
66
|
-
* ```
|
|
67
|
-
*
|
|
68
|
-
* @packageDocumentation
|
|
69
|
-
*/
|
|
70
|
-
|
|
71
|
-
export {
|
|
72
|
-
createOtelObservability,
|
|
73
|
-
createOtelObservabilityFromEnv,
|
|
74
|
-
OtelObservability,
|
|
75
|
-
} from './agents/otel-observability';
|
|
76
|
-
export { channels, genericObservability, subscribe } from './agents/observability';
|
|
77
|
-
export type {
|
|
78
|
-
BaseEvent,
|
|
79
|
-
OtelObservabilityConfig,
|
|
80
|
-
AgentObservabilityEvent,
|
|
81
|
-
MCPObservabilityEvent,
|
|
82
|
-
ObservabilityEvent,
|
|
83
|
-
Observability,
|
|
84
|
-
ChannelEventMap,
|
|
85
|
-
AgentInstrumentationOptions,
|
|
86
|
-
AgentSpanAttributes,
|
|
87
|
-
} from './agents/types';
|