autotel-cloudflare 2.1.0
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/LICENSE +21 -0
- package/README.md +432 -0
- package/dist/actors.d.ts +248 -0
- package/dist/actors.js +1030 -0
- package/dist/actors.js.map +1 -0
- package/dist/agents.d.ts +219 -0
- package/dist/agents.js +276 -0
- package/dist/agents.js.map +1 -0
- package/dist/bindings.d.ts +40 -0
- package/dist/bindings.js +4 -0
- package/dist/bindings.js.map +1 -0
- package/dist/chunk-JDPN3HND.js +520 -0
- package/dist/chunk-JDPN3HND.js.map +1 -0
- package/dist/chunk-QXFYTHQF.js +298 -0
- package/dist/chunk-QXFYTHQF.js.map +1 -0
- package/dist/chunk-SKKRPS5K.js +50 -0
- package/dist/chunk-SKKRPS5K.js.map +1 -0
- package/dist/events.d.ts +1 -0
- package/dist/events.js +3 -0
- package/dist/events.js.map +1 -0
- package/dist/handlers.d.ts +121 -0
- package/dist/handlers.js +4 -0
- package/dist/handlers.js.map +1 -0
- package/dist/index.d.ts +144 -0
- package/dist/index.js +576 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +1 -0
- package/dist/logger.js +3 -0
- package/dist/logger.js.map +1 -0
- package/dist/sampling.d.ts +4 -0
- package/dist/sampling.js +3 -0
- package/dist/sampling.js.map +1 -0
- package/dist/testing.d.ts +1 -0
- package/dist/testing.js +3 -0
- package/dist/testing.js.map +1 -0
- package/package.json +107 -0
- package/src/actors/alarms.ts +225 -0
- package/src/actors/index.ts +36 -0
- package/src/actors/instrument-actor.test.ts +179 -0
- package/src/actors/instrument-actor.ts +574 -0
- package/src/actors/sockets.ts +217 -0
- package/src/actors/storage.ts +263 -0
- package/src/actors/traced-handler.ts +300 -0
- package/src/actors/types.ts +98 -0
- package/src/actors.ts +50 -0
- package/src/agents/index.ts +42 -0
- package/src/agents/otel-observability.test.ts +329 -0
- package/src/agents/otel-observability.ts +465 -0
- package/src/agents/types.ts +167 -0
- package/src/agents.ts +76 -0
- package/src/bindings/bindings.ts +621 -0
- package/src/bindings/common.ts +75 -0
- package/src/bindings/index.ts +12 -0
- package/src/bindings.ts +6 -0
- package/src/events.ts +6 -0
- package/src/global/cache.test.ts +292 -0
- package/src/global/cache.ts +164 -0
- package/src/global/fetch.test.ts +344 -0
- package/src/global/fetch.ts +134 -0
- package/src/global/index.ts +7 -0
- package/src/handlers/durable-objects.test.ts +524 -0
- package/src/handlers/durable-objects.ts +250 -0
- package/src/handlers/index.ts +6 -0
- package/src/handlers/workflows.ts +318 -0
- package/src/handlers.ts +6 -0
- package/src/index.ts +57 -0
- package/src/logger.ts +6 -0
- package/src/sampling.ts +6 -0
- package/src/testing.ts +6 -0
- package/src/wrappers/index.ts +8 -0
- package/src/wrappers/instrument.integration.test.ts +468 -0
- package/src/wrappers/instrument.ts +643 -0
- package/src/wrappers/wrap-do.ts +34 -0
- package/src/wrappers/wrap-module.ts +37 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable Objects instrumentation for Cloudflare Workers
|
|
3
|
+
*
|
|
4
|
+
* Note: This file uses Cloudflare Workers types (DurableObjectId, DurableObjectState, etc.)
|
|
5
|
+
* which are globally available via @cloudflare/workers-types when listed in tsconfig.json.
|
|
6
|
+
* These types are devDependencies only - they're not runtime dependencies.
|
|
7
|
+
* At runtime, Cloudflare Workers runtime provides the actual implementations.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
trace,
|
|
12
|
+
context as api_context,
|
|
13
|
+
propagation,
|
|
14
|
+
SpanStatusCode,
|
|
15
|
+
SpanKind,
|
|
16
|
+
} from '@opentelemetry/api';
|
|
17
|
+
import type { ConfigurationOption } from 'autotel-edge';
|
|
18
|
+
import { createInitialiser, setConfig, WorkerTracer } from 'autotel-edge';
|
|
19
|
+
import { wrap } from '../bindings/common';
|
|
20
|
+
|
|
21
|
+
// Durable Object types
|
|
22
|
+
type DOFetchFn = (request: Request) => Response | Promise<Response>;
|
|
23
|
+
type DOAlarmFn = () => void | Promise<void>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Track cold starts per DO class
|
|
27
|
+
*/
|
|
28
|
+
const coldStarts = new WeakMap<any, boolean>();
|
|
29
|
+
|
|
30
|
+
function isColdStart(doClass: any): boolean {
|
|
31
|
+
if (!coldStarts.has(doClass)) {
|
|
32
|
+
coldStarts.set(doClass, true);
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Instrument a Durable Object fetch method
|
|
40
|
+
*/
|
|
41
|
+
function instrumentDOFetch(
|
|
42
|
+
fetchFn: DOFetchFn,
|
|
43
|
+
id: DurableObjectId,
|
|
44
|
+
doClass: any,
|
|
45
|
+
): DOFetchFn {
|
|
46
|
+
return async function instrumentedFetch(
|
|
47
|
+
this: any,
|
|
48
|
+
request: Request,
|
|
49
|
+
): Promise<Response> {
|
|
50
|
+
const tracer = trace.getTracer('autotel-edge') as WorkerTracer;
|
|
51
|
+
|
|
52
|
+
// Extract parent context from request headers
|
|
53
|
+
const parentContext = propagation.extract(
|
|
54
|
+
api_context.active(),
|
|
55
|
+
request.headers,
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const url = new URL(request.url);
|
|
59
|
+
const spanName = `DO ${id.name || id.toString()}: ${request.method} ${url.pathname}`;
|
|
60
|
+
|
|
61
|
+
return tracer.startActiveSpan(
|
|
62
|
+
spanName,
|
|
63
|
+
{
|
|
64
|
+
kind: SpanKind.SERVER,
|
|
65
|
+
attributes: {
|
|
66
|
+
'http.request.method': request.method,
|
|
67
|
+
'url.full': request.url,
|
|
68
|
+
'do.id': id.toString(),
|
|
69
|
+
'do.id.name': id.name || '',
|
|
70
|
+
'faas.trigger': 'http',
|
|
71
|
+
'faas.coldstart': isColdStart(doClass),
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
parentContext,
|
|
75
|
+
async (span) => {
|
|
76
|
+
try {
|
|
77
|
+
const response = await fetchFn.call(this, request);
|
|
78
|
+
|
|
79
|
+
span.setAttributes({
|
|
80
|
+
'http.response.status_code': response.status,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
if (response.ok) {
|
|
84
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
85
|
+
} else {
|
|
86
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return response;
|
|
90
|
+
} catch (error) {
|
|
91
|
+
span.recordException(error as Error);
|
|
92
|
+
span.setStatus({
|
|
93
|
+
code: SpanStatusCode.ERROR,
|
|
94
|
+
message: error instanceof Error ? error.message : String(error),
|
|
95
|
+
});
|
|
96
|
+
throw error;
|
|
97
|
+
} finally {
|
|
98
|
+
span.end();
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
);
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Instrument a Durable Object alarm method
|
|
107
|
+
*/
|
|
108
|
+
function instrumentDOAlarm(
|
|
109
|
+
alarmFn: DOAlarmFn,
|
|
110
|
+
id: DurableObjectId,
|
|
111
|
+
doClass: any,
|
|
112
|
+
): DOAlarmFn {
|
|
113
|
+
return async function instrumentedAlarm(this: any): Promise<void> {
|
|
114
|
+
const tracer = trace.getTracer('autotel-edge') as WorkerTracer;
|
|
115
|
+
|
|
116
|
+
const spanName = `DO ${id.name || id.toString()}: alarm`;
|
|
117
|
+
|
|
118
|
+
return tracer.startActiveSpan(
|
|
119
|
+
spanName,
|
|
120
|
+
{
|
|
121
|
+
kind: SpanKind.INTERNAL,
|
|
122
|
+
attributes: {
|
|
123
|
+
'do.id': id.toString(),
|
|
124
|
+
'do.id.name': id.name || '',
|
|
125
|
+
'faas.trigger': 'timer',
|
|
126
|
+
'faas.coldstart': isColdStart(doClass),
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
async (span) => {
|
|
130
|
+
try {
|
|
131
|
+
await alarmFn.call(this);
|
|
132
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
133
|
+
} catch (error) {
|
|
134
|
+
span.recordException(error as Error);
|
|
135
|
+
span.setStatus({
|
|
136
|
+
code: SpanStatusCode.ERROR,
|
|
137
|
+
message: error instanceof Error ? error.message : String(error),
|
|
138
|
+
});
|
|
139
|
+
throw error;
|
|
140
|
+
} finally {
|
|
141
|
+
span.end();
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
);
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Instrument a Durable Object instance
|
|
150
|
+
*/
|
|
151
|
+
function instrumentDOInstance(
|
|
152
|
+
doInstance: any,
|
|
153
|
+
state: DurableObjectState,
|
|
154
|
+
_env: any,
|
|
155
|
+
doClass: any,
|
|
156
|
+
): any {
|
|
157
|
+
const instanceHandler: ProxyHandler<any> = {
|
|
158
|
+
get(target, prop) {
|
|
159
|
+
const value = Reflect.get(target, prop);
|
|
160
|
+
|
|
161
|
+
if (prop === 'fetch' && typeof value === 'function') {
|
|
162
|
+
return instrumentDOFetch(value.bind(target), state.id, doClass);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (prop === 'alarm' && typeof value === 'function') {
|
|
166
|
+
return instrumentDOAlarm(value.bind(target), state.id, doClass);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Bind other methods to the target
|
|
170
|
+
if (typeof value === 'function') {
|
|
171
|
+
return value.bind(target);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return value;
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
return wrap(doInstance, instanceHandler);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Instrument a Durable Object class
|
|
183
|
+
*
|
|
184
|
+
* This wraps the DO class to automatically trace all fetch and alarm calls,
|
|
185
|
+
* as well as initialize the telemetry configuration.
|
|
186
|
+
*
|
|
187
|
+
* **Usage:**
|
|
188
|
+
* ```typescript
|
|
189
|
+
* import { DurableObject } from 'cloudflare:workers'
|
|
190
|
+
* import { instrumentDO } from 'autotel-edge'
|
|
191
|
+
*
|
|
192
|
+
* export class Counter extends DurableObject<Env> {
|
|
193
|
+
* async fetch(request: Request) {
|
|
194
|
+
* // Your DO logic here
|
|
195
|
+
* return new Response('OK')
|
|
196
|
+
* }
|
|
197
|
+
* }
|
|
198
|
+
*
|
|
199
|
+
* // Wrap the class before exporting
|
|
200
|
+
* export const CounterDO = instrumentDO(Counter, (env: Env) => ({
|
|
201
|
+
* exporter: {
|
|
202
|
+
* url: env.OTLP_ENDPOINT,
|
|
203
|
+
* headers: { 'x-api-key': env.API_KEY }
|
|
204
|
+
* },
|
|
205
|
+
* service: {
|
|
206
|
+
* name: 'my-durable-object',
|
|
207
|
+
* version: '1.0.0'
|
|
208
|
+
* }
|
|
209
|
+
* }))
|
|
210
|
+
* ```
|
|
211
|
+
*
|
|
212
|
+
* **What you get:**
|
|
213
|
+
* - 🎯 Automatic spans for fetch() calls with HTTP attributes
|
|
214
|
+
* - ⏰ Automatic spans for alarm() calls
|
|
215
|
+
* - 🥶 Cold start tracking
|
|
216
|
+
* - 🔗 Context propagation from incoming requests
|
|
217
|
+
* - ⚡ Automatic span lifecycle management
|
|
218
|
+
*
|
|
219
|
+
* @param doClass - The Durable Object class to instrument
|
|
220
|
+
* @param config - Configuration or configuration function
|
|
221
|
+
* @returns Instrumented Durable Object class
|
|
222
|
+
*/
|
|
223
|
+
export function instrumentDO<C extends new (state: DurableObjectState, env: any) => any>(
|
|
224
|
+
doClass: C,
|
|
225
|
+
config: ConfigurationOption,
|
|
226
|
+
): C {
|
|
227
|
+
const initialiser = createInitialiser(config);
|
|
228
|
+
|
|
229
|
+
const classHandler: ProxyHandler<C> = {
|
|
230
|
+
construct(target, [state, env]: [DurableObjectState, any]) {
|
|
231
|
+
// Initialize config for this DO instance
|
|
232
|
+
const trigger = {
|
|
233
|
+
id: state.id.toString(),
|
|
234
|
+
name: state.id.name,
|
|
235
|
+
};
|
|
236
|
+
const doConfig = initialiser(env, trigger);
|
|
237
|
+
const context = setConfig(doConfig);
|
|
238
|
+
|
|
239
|
+
// Create the DO instance within the config context
|
|
240
|
+
const doInstance = api_context.with(context, () => {
|
|
241
|
+
return new target(state, env);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// Instrument the instance
|
|
245
|
+
return instrumentDOInstance(doInstance, state, env, doClass);
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
return wrap(doClass, classHandler);
|
|
250
|
+
}
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cloudflare Workflows instrumentation for autotel-edge
|
|
3
|
+
*
|
|
4
|
+
* Instruments WorkflowEntrypoint classes to automatically trace workflow execution,
|
|
5
|
+
* step operations, retries, and sleeps.
|
|
6
|
+
*
|
|
7
|
+
* Based on Cloudflare Workflows API:
|
|
8
|
+
* https://developers.cloudflare.com/workflows/
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
trace,
|
|
13
|
+
context as api_context,
|
|
14
|
+
SpanStatusCode,
|
|
15
|
+
SpanKind,
|
|
16
|
+
} from '@opentelemetry/api';
|
|
17
|
+
import type { ConfigurationOption } from 'autotel-edge';
|
|
18
|
+
import { createInitialiser, setConfig, WorkerTracer } from 'autotel-edge';
|
|
19
|
+
import { wrap } from '../bindings/common';
|
|
20
|
+
|
|
21
|
+
// Workflow types (these would come from @cloudflare/workers-types when available)
|
|
22
|
+
type WorkflowEvent = any;
|
|
23
|
+
type WorkflowStep = any;
|
|
24
|
+
|
|
25
|
+
type WorkflowRunFn = (
|
|
26
|
+
event: WorkflowEvent,
|
|
27
|
+
step: WorkflowStep,
|
|
28
|
+
) => Promise<void> | void;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Track cold starts per Workflow class
|
|
32
|
+
*/
|
|
33
|
+
const coldStarts = new WeakMap<any, boolean>();
|
|
34
|
+
|
|
35
|
+
function isColdStart(workflowClass: any): boolean {
|
|
36
|
+
if (!coldStarts.has(workflowClass)) {
|
|
37
|
+
coldStarts.set(workflowClass, true);
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Proxy the step object to instrument step.do() calls
|
|
45
|
+
*/
|
|
46
|
+
function instrumentWorkflowStep(
|
|
47
|
+
step: WorkflowStep,
|
|
48
|
+
workflowName: string,
|
|
49
|
+
): WorkflowStep {
|
|
50
|
+
const stepHandler: ProxyHandler<WorkflowStep> = {
|
|
51
|
+
get(target, prop) {
|
|
52
|
+
const value = Reflect.get(target, prop);
|
|
53
|
+
|
|
54
|
+
// Instrument step.do() to create spans for each workflow step
|
|
55
|
+
if (prop === 'do' && typeof value === 'function') {
|
|
56
|
+
return new Proxy(value, {
|
|
57
|
+
apply: (fnTarget, thisArg, args) => {
|
|
58
|
+
const [stepName] = args as [string, () => Promise<any>];
|
|
59
|
+
|
|
60
|
+
const tracer = trace.getTracer('autotel-edge') as WorkerTracer;
|
|
61
|
+
|
|
62
|
+
return tracer.startActiveSpan(
|
|
63
|
+
`Workflow ${workflowName}: ${stepName}`,
|
|
64
|
+
{
|
|
65
|
+
kind: SpanKind.INTERNAL,
|
|
66
|
+
attributes: {
|
|
67
|
+
'workflow.step.name': stepName,
|
|
68
|
+
'workflow.name': workflowName,
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
async (span) => {
|
|
72
|
+
try {
|
|
73
|
+
const result = await Reflect.apply(fnTarget, thisArg, args);
|
|
74
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
75
|
+
return result;
|
|
76
|
+
} catch (error) {
|
|
77
|
+
span.recordException(error as Error);
|
|
78
|
+
span.setStatus({
|
|
79
|
+
code: SpanStatusCode.ERROR,
|
|
80
|
+
message:
|
|
81
|
+
error instanceof Error ? error.message : String(error),
|
|
82
|
+
});
|
|
83
|
+
throw error;
|
|
84
|
+
} finally {
|
|
85
|
+
span.end();
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
);
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Instrument step.sleep() to track workflow delays
|
|
94
|
+
if (prop === 'sleep' && typeof value === 'function') {
|
|
95
|
+
return new Proxy(value, {
|
|
96
|
+
apply: (fnTarget, thisArg, args) => {
|
|
97
|
+
const [sleepName, duration] = args as [string, string | number];
|
|
98
|
+
|
|
99
|
+
const tracer = trace.getTracer('autotel-edge') as WorkerTracer;
|
|
100
|
+
|
|
101
|
+
return tracer.startActiveSpan(
|
|
102
|
+
`Workflow ${workflowName}: sleep ${sleepName}`,
|
|
103
|
+
{
|
|
104
|
+
kind: SpanKind.INTERNAL,
|
|
105
|
+
attributes: {
|
|
106
|
+
'workflow.sleep.name': sleepName,
|
|
107
|
+
'workflow.sleep.duration': String(duration),
|
|
108
|
+
'workflow.name': workflowName,
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
async (span) => {
|
|
112
|
+
try {
|
|
113
|
+
const result = await Reflect.apply(fnTarget, thisArg, args);
|
|
114
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
115
|
+
return result;
|
|
116
|
+
} catch (error) {
|
|
117
|
+
span.recordException(error as Error);
|
|
118
|
+
span.setStatus({
|
|
119
|
+
code: SpanStatusCode.ERROR,
|
|
120
|
+
message:
|
|
121
|
+
error instanceof Error ? error.message : String(error),
|
|
122
|
+
});
|
|
123
|
+
throw error;
|
|
124
|
+
} finally {
|
|
125
|
+
span.end();
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
);
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Pass through other step methods
|
|
134
|
+
if (typeof value === 'function') {
|
|
135
|
+
return value.bind(target);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return value;
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
return wrap(step, stepHandler);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Instrument a Workflow run method
|
|
147
|
+
*/
|
|
148
|
+
function instrumentWorkflowRun(
|
|
149
|
+
runFn: WorkflowRunFn,
|
|
150
|
+
workflowName: string,
|
|
151
|
+
workflowClass: any,
|
|
152
|
+
): WorkflowRunFn {
|
|
153
|
+
return async function instrumentedRun(
|
|
154
|
+
this: any,
|
|
155
|
+
event: WorkflowEvent,
|
|
156
|
+
step: WorkflowStep,
|
|
157
|
+
): Promise<void> {
|
|
158
|
+
const tracer = trace.getTracer('autotel-edge') as WorkerTracer;
|
|
159
|
+
|
|
160
|
+
// Instrument the step object to track individual operations
|
|
161
|
+
const instrumentedStep = instrumentWorkflowStep(step, workflowName);
|
|
162
|
+
|
|
163
|
+
const spanName = `Workflow ${workflowName}: run`;
|
|
164
|
+
|
|
165
|
+
return tracer.startActiveSpan(
|
|
166
|
+
spanName,
|
|
167
|
+
{
|
|
168
|
+
kind: SpanKind.INTERNAL,
|
|
169
|
+
attributes: {
|
|
170
|
+
'workflow.name': workflowName,
|
|
171
|
+
'faas.trigger': 'workflow',
|
|
172
|
+
'faas.coldstart': isColdStart(workflowClass),
|
|
173
|
+
// Add workflow event attributes if available
|
|
174
|
+
...(event?.workflowId && { 'workflow.id': event.workflowId }),
|
|
175
|
+
...(event?.runId && { 'workflow.run_id': event.runId }),
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
async (span) => {
|
|
179
|
+
try {
|
|
180
|
+
await runFn.call(this, event, instrumentedStep);
|
|
181
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
182
|
+
} catch (error) {
|
|
183
|
+
span.recordException(error as Error);
|
|
184
|
+
span.setStatus({
|
|
185
|
+
code: SpanStatusCode.ERROR,
|
|
186
|
+
message: error instanceof Error ? error.message : String(error),
|
|
187
|
+
});
|
|
188
|
+
throw error;
|
|
189
|
+
} finally {
|
|
190
|
+
span.end();
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
);
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Instrument a Workflow instance
|
|
199
|
+
*/
|
|
200
|
+
function instrumentWorkflowInstance(
|
|
201
|
+
workflowInstance: any,
|
|
202
|
+
workflowName: string,
|
|
203
|
+
workflowClass: any,
|
|
204
|
+
): any {
|
|
205
|
+
const instanceHandler: ProxyHandler<any> = {
|
|
206
|
+
get(target, prop) {
|
|
207
|
+
const value = Reflect.get(target, prop);
|
|
208
|
+
|
|
209
|
+
if (prop === 'run' && typeof value === 'function') {
|
|
210
|
+
return instrumentWorkflowRun(
|
|
211
|
+
value.bind(target),
|
|
212
|
+
workflowName,
|
|
213
|
+
workflowClass,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Bind other methods to the target
|
|
218
|
+
if (typeof value === 'function') {
|
|
219
|
+
return value.bind(target);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return value;
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
return wrap(workflowInstance, instanceHandler);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Instrument a Cloudflare Workflow class
|
|
231
|
+
*
|
|
232
|
+
* This wraps the WorkflowEntrypoint class to automatically trace workflow execution,
|
|
233
|
+
* step operations, retries, and sleeps.
|
|
234
|
+
*
|
|
235
|
+
* **Usage:**
|
|
236
|
+
* ```typescript
|
|
237
|
+
* import { WorkflowEntrypoint } from 'cloudflare:workers'
|
|
238
|
+
* import { instrumentWorkflow } from 'autotel-edge'
|
|
239
|
+
*
|
|
240
|
+
* export class CheckoutWorkflow extends WorkflowEntrypoint {
|
|
241
|
+
* async run(event, step) {
|
|
242
|
+
* await step.do('submit payment', async () => {
|
|
243
|
+
* return await submitToPaymentProcessor(event.params.payment)
|
|
244
|
+
* })
|
|
245
|
+
*
|
|
246
|
+
* await step.sleep('wait for feedback', '2 days')
|
|
247
|
+
*
|
|
248
|
+
* await step.do('send feedback email', sendFeedbackEmail)
|
|
249
|
+
* }
|
|
250
|
+
* }
|
|
251
|
+
*
|
|
252
|
+
* // Wrap the class before exporting
|
|
253
|
+
* export const CheckoutWorkflowInstrumented = instrumentWorkflow(
|
|
254
|
+
* CheckoutWorkflow,
|
|
255
|
+
* 'checkout-workflow',
|
|
256
|
+
* (env: Env) => ({
|
|
257
|
+
* exporter: {
|
|
258
|
+
* url: env.OTLP_ENDPOINT,
|
|
259
|
+
* headers: { 'x-api-key': env.API_KEY }
|
|
260
|
+
* },
|
|
261
|
+
* service: {
|
|
262
|
+
* name: 'checkout-workflow',
|
|
263
|
+
* version: '1.0.0'
|
|
264
|
+
* }
|
|
265
|
+
* })
|
|
266
|
+
* )
|
|
267
|
+
* ```
|
|
268
|
+
*
|
|
269
|
+
* **What you get:**
|
|
270
|
+
* - 🎯 Automatic spans for workflow.run() execution
|
|
271
|
+
* - 📋 Automatic spans for each step.do() operation
|
|
272
|
+
* - ⏸️ Automatic spans for step.sleep() operations
|
|
273
|
+
* - 🔄 Automatic retry tracking (via step.do retries)
|
|
274
|
+
* - 🥶 Cold start tracking
|
|
275
|
+
* - ⚡ Automatic span lifecycle management
|
|
276
|
+
*
|
|
277
|
+
* @param workflowClass - The WorkflowEntrypoint class to instrument
|
|
278
|
+
* @param workflowName - The name of the workflow (used in span names)
|
|
279
|
+
* @param config - Configuration or configuration function
|
|
280
|
+
* @returns Instrumented Workflow class
|
|
281
|
+
*/
|
|
282
|
+
export function instrumentWorkflow<
|
|
283
|
+
C extends new (...args: any[]) => any,
|
|
284
|
+
>(
|
|
285
|
+
workflowClass: C,
|
|
286
|
+
workflowName: string,
|
|
287
|
+
config: ConfigurationOption,
|
|
288
|
+
): C {
|
|
289
|
+
const initialiser = createInitialiser(config);
|
|
290
|
+
|
|
291
|
+
const classHandler: ProxyHandler<C> = {
|
|
292
|
+
construct(target, args: any[]) {
|
|
293
|
+
// Extract env from constructor args (typically last arg)
|
|
294
|
+
const env = args[args.length - 1] || {};
|
|
295
|
+
|
|
296
|
+
// Initialize config for this workflow instance
|
|
297
|
+
// Use Request as trigger type since workflows don't have a standard Trigger type yet
|
|
298
|
+
const trigger = new Request('https://workflow.local/run');
|
|
299
|
+
const workflowConfig = initialiser(env, trigger);
|
|
300
|
+
const context = setConfig(workflowConfig);
|
|
301
|
+
|
|
302
|
+
// Create the workflow instance within the config context
|
|
303
|
+
const workflowInstance = api_context.with(context, () => {
|
|
304
|
+
return new target(...args);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
// Instrument the instance
|
|
308
|
+
return instrumentWorkflowInstance(
|
|
309
|
+
workflowInstance,
|
|
310
|
+
workflowName,
|
|
311
|
+
workflowClass,
|
|
312
|
+
);
|
|
313
|
+
},
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
return wrap(workflowClass, classHandler);
|
|
317
|
+
}
|
|
318
|
+
|
package/src/handlers.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* autotel-cloudflare
|
|
3
|
+
*
|
|
4
|
+
* The #1 OpenTelemetry package for Cloudflare Workers
|
|
5
|
+
*
|
|
6
|
+
* Features:
|
|
7
|
+
* - Native Cloudflare OTel integration (works with wrangler.toml destinations)
|
|
8
|
+
* - Complete bindings coverage (KV, R2, D1, DO, AI, Vectorize, etc.)
|
|
9
|
+
* - Multiple API styles (instrument, wrapModule, functional)
|
|
10
|
+
* - Advanced sampling strategies
|
|
11
|
+
* - Events integration
|
|
12
|
+
* - Zero vendor lock-in (OTLP compatible)
|
|
13
|
+
*
|
|
14
|
+
* @example Quick Start
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { wrapModule, trace } from 'autotel-cloudflare'
|
|
17
|
+
*
|
|
18
|
+
* const processOrder = trace(async (orderId: string) => {
|
|
19
|
+
* const order = await env.ORDERS_KV.get(orderId)
|
|
20
|
+
* return order
|
|
21
|
+
* })
|
|
22
|
+
*
|
|
23
|
+
* export default wrapModule(
|
|
24
|
+
* {
|
|
25
|
+
* service: { name: 'my-worker' },
|
|
26
|
+
* instrumentBindings: true,
|
|
27
|
+
* sampling: 'adaptive'
|
|
28
|
+
* },
|
|
29
|
+
* {
|
|
30
|
+
* async fetch(req, env, ctx) {
|
|
31
|
+
* return Response.json(await processOrder('123'))
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
* )
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
// Re-export EVERYTHING from autotel-edge (vendor-agnostic foundation)
|
|
39
|
+
export * from 'autotel-edge';
|
|
40
|
+
|
|
41
|
+
// Cloudflare-specific wrappers
|
|
42
|
+
export { instrument, wrapModule, wrapDurableObject } from './wrappers';
|
|
43
|
+
|
|
44
|
+
// Cloudflare-specific handlers
|
|
45
|
+
export { instrumentDO, instrumentWorkflow } from './handlers';
|
|
46
|
+
|
|
47
|
+
// Cloudflare-specific bindings
|
|
48
|
+
export {
|
|
49
|
+
instrumentKV,
|
|
50
|
+
instrumentR2,
|
|
51
|
+
instrumentD1,
|
|
52
|
+
instrumentServiceBinding,
|
|
53
|
+
instrumentBindings,
|
|
54
|
+
} from './bindings';
|
|
55
|
+
|
|
56
|
+
// Global instrumentations
|
|
57
|
+
export { instrumentGlobalFetch, instrumentGlobalCache } from './global';
|
package/src/logger.ts
ADDED
package/src/sampling.ts
ADDED
package/src/testing.ts
ADDED