autotel-cloudflare 2.18.18 → 2.19.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.
Files changed (58) hide show
  1. package/dist/actors.d.ts +54 -89
  2. package/dist/actors.d.ts.map +1 -0
  3. package/dist/actors.js +990 -986
  4. package/dist/actors.js.map +1 -1
  5. package/dist/agents.d.ts +102 -128
  6. package/dist/agents.d.ts.map +1 -0
  7. package/dist/agents.js +275 -259
  8. package/dist/agents.js.map +1 -1
  9. package/dist/bindings-C10RI6Il.js +975 -0
  10. package/dist/bindings-C10RI6Il.js.map +1 -0
  11. package/dist/bindings-xEZcXo5r.d.ts +149 -0
  12. package/dist/bindings-xEZcXo5r.d.ts.map +1 -0
  13. package/dist/bindings.d.ts +2 -139
  14. package/dist/bindings.js +3 -4
  15. package/dist/common-DiWH6nmG.js +63 -0
  16. package/dist/common-DiWH6nmG.js.map +1 -0
  17. package/dist/events.d.ts +1 -1
  18. package/dist/events.js +3 -3
  19. package/dist/execution-logger-BxmgP8jF.js +65 -0
  20. package/dist/execution-logger-BxmgP8jF.js.map +1 -0
  21. package/dist/{logger-Cm_73k3-.d.ts → execution-logger-tgQmJeeU.d.ts} +9 -7
  22. package/dist/execution-logger-tgQmJeeU.d.ts.map +1 -0
  23. package/dist/handlers-CMg9l_T-.js +378 -0
  24. package/dist/handlers-CMg9l_T-.js.map +1 -0
  25. package/dist/handlers-C_VojUeA.d.ts +96 -0
  26. package/dist/handlers-C_VojUeA.d.ts.map +1 -0
  27. package/dist/handlers.d.ts +2 -112
  28. package/dist/handlers.js +3 -4
  29. package/dist/index.d.ts +20 -99
  30. package/dist/index.d.ts.map +1 -0
  31. package/dist/index.js +644 -600
  32. package/dist/index.js.map +1 -1
  33. package/dist/logger.d.ts +3 -3
  34. package/dist/logger.js +5 -4
  35. package/dist/parse-error.d.ts +2 -1
  36. package/dist/parse-error.js +3 -3
  37. package/dist/sampling.d.ts +1 -4
  38. package/dist/sampling.js +3 -3
  39. package/dist/testing.d.ts +1 -1
  40. package/dist/testing.js +3 -3
  41. package/package.json +6 -6
  42. package/src/bindings/ai.test.ts +1 -1
  43. package/src/bindings/ai.ts +3 -1
  44. package/dist/bindings.js.map +0 -1
  45. package/dist/chunk-KAUHT25H.js +0 -1084
  46. package/dist/chunk-KAUHT25H.js.map +0 -1
  47. package/dist/chunk-MIDMNKDC.js +0 -333
  48. package/dist/chunk-MIDMNKDC.js.map +0 -1
  49. package/dist/chunk-O4IYKWPJ.js +0 -55
  50. package/dist/chunk-O4IYKWPJ.js.map +0 -1
  51. package/dist/chunk-RVVMMPWN.js +0 -63
  52. package/dist/chunk-RVVMMPWN.js.map +0 -1
  53. package/dist/events.js.map +0 -1
  54. package/dist/handlers.js.map +0 -1
  55. package/dist/logger.js.map +0 -1
  56. package/dist/parse-error.js.map +0 -1
  57. package/dist/sampling.js.map +0 -1
  58. package/dist/testing.js.map +0 -1
package/dist/actors.js CHANGED
@@ -1,1030 +1,1034 @@
1
- import { wrap } from './chunk-O4IYKWPJ.js';
2
- import { propagation, SpanKind, SpanStatusCode, context, trace } from '@opentelemetry/api';
3
- import { createInitialiser, setConfig } from 'autotel-edge';
1
+ import { a as wrap } from "./common-DiWH6nmG.js";
2
+ import { createInitialiser, setConfig } from "autotel-edge";
3
+ import { SpanKind, SpanStatusCode, context, propagation, trace } from "@opentelemetry/api";
4
4
 
5
- function getTracer() {
6
- return trace.getTracer("autotel-cloudflare-actors");
5
+ //#region src/actors/storage.ts
6
+ /**
7
+ * Actor storage instrumentation
8
+ *
9
+ * Traces operations on actor.storage including SQL queries
10
+ */
11
+ /**
12
+ * Get the tracer instance
13
+ */
14
+ function getTracer$4() {
15
+ return trace.getTracer("autotel-cloudflare-actors");
7
16
  }
17
+ /**
18
+ * Instrument Actor storage for tracing
19
+ *
20
+ * Captures:
21
+ * - SQL query operations
22
+ * - Key-value operations (if available)
23
+ */
8
24
  function instrumentActorStorage(storage, actorInstance, actorClass) {
9
- if (!storage || typeof storage !== "object") {
10
- return storage;
11
- }
12
- const actorClassName = actorClass.name || "Actor";
13
- const actorName = actorInstance.name || actorClassName;
14
- const storageHandler = {
15
- get(target, prop) {
16
- const value = Reflect.get(target, prop);
17
- if (prop === "exec" && typeof value === "function") {
18
- return function instrumentedExec(query, ...params) {
19
- const tracer = getTracer();
20
- const spanName = `Actor ${actorName}: storage.exec`;
21
- return tracer.startActiveSpan(
22
- spanName,
23
- {
24
- kind: SpanKind.CLIENT,
25
- attributes: {
26
- "actor.name": actorName,
27
- "actor.class": actorClassName,
28
- "db.system": "sqlite",
29
- "db.operation": "exec",
30
- "db.statement": query,
31
- "db.statement.params_count": params.length
32
- }
33
- },
34
- (span) => {
35
- try {
36
- const result = value.call(target, query, ...params);
37
- span.setStatus({ code: SpanStatusCode.OK });
38
- return result;
39
- } catch (error) {
40
- span.recordException(error);
41
- span.setStatus({
42
- code: SpanStatusCode.ERROR,
43
- message: error instanceof Error ? error.message : String(error)
44
- });
45
- throw error;
46
- } finally {
47
- span.end();
48
- }
49
- }
50
- );
51
- };
52
- }
53
- if (prop === "query" && typeof value === "function") {
54
- return function instrumentedQuery(query, ...params) {
55
- const tracer = getTracer();
56
- const spanName = `Actor ${actorName}: storage.query`;
57
- return tracer.startActiveSpan(
58
- spanName,
59
- {
60
- kind: SpanKind.CLIENT,
61
- attributes: {
62
- "actor.name": actorName,
63
- "actor.class": actorClassName,
64
- "db.system": "sqlite",
65
- "db.operation": "query",
66
- "db.statement": query,
67
- "db.statement.params_count": params.length
68
- }
69
- },
70
- (span) => {
71
- try {
72
- const result = value.call(target, query, ...params);
73
- span.setStatus({ code: SpanStatusCode.OK });
74
- return result;
75
- } catch (error) {
76
- span.recordException(error);
77
- span.setStatus({
78
- code: SpanStatusCode.ERROR,
79
- message: error instanceof Error ? error.message : String(error)
80
- });
81
- throw error;
82
- } finally {
83
- span.end();
84
- }
85
- }
86
- );
87
- };
88
- }
89
- if (prop === "get" && typeof value === "function") {
90
- return async function instrumentedGet(key) {
91
- const tracer = getTracer();
92
- const spanName = `Actor ${actorName}: storage.get`;
93
- return tracer.startActiveSpan(
94
- spanName,
95
- {
96
- kind: SpanKind.CLIENT,
97
- attributes: {
98
- "actor.name": actorName,
99
- "actor.class": actorClassName,
100
- "db.system": "durable_object_storage",
101
- "db.operation": "get",
102
- "db.key": key
103
- }
104
- },
105
- async (span) => {
106
- try {
107
- const result = await value.call(target, key);
108
- span.setAttributes({
109
- "db.result.found": result !== null && result !== void 0
110
- });
111
- span.setStatus({ code: SpanStatusCode.OK });
112
- return result;
113
- } catch (error) {
114
- span.recordException(error);
115
- span.setStatus({
116
- code: SpanStatusCode.ERROR,
117
- message: error instanceof Error ? error.message : String(error)
118
- });
119
- throw error;
120
- } finally {
121
- span.end();
122
- }
123
- }
124
- );
125
- };
126
- }
127
- if (prop === "put" && typeof value === "function") {
128
- return async function instrumentedPut(key, val) {
129
- const tracer = getTracer();
130
- const spanName = `Actor ${actorName}: storage.put`;
131
- return tracer.startActiveSpan(
132
- spanName,
133
- {
134
- kind: SpanKind.CLIENT,
135
- attributes: {
136
- "actor.name": actorName,
137
- "actor.class": actorClassName,
138
- "db.system": "durable_object_storage",
139
- "db.operation": "put",
140
- "db.key": key,
141
- "db.value_type": typeof val
142
- }
143
- },
144
- async (span) => {
145
- try {
146
- await value.call(target, key, val);
147
- span.setStatus({ code: SpanStatusCode.OK });
148
- } catch (error) {
149
- span.recordException(error);
150
- span.setStatus({
151
- code: SpanStatusCode.ERROR,
152
- message: error instanceof Error ? error.message : String(error)
153
- });
154
- throw error;
155
- } finally {
156
- span.end();
157
- }
158
- }
159
- );
160
- };
161
- }
162
- if (prop === "delete" && typeof value === "function") {
163
- return async function instrumentedDelete(key) {
164
- const tracer = getTracer();
165
- const spanName = `Actor ${actorName}: storage.delete`;
166
- return tracer.startActiveSpan(
167
- spanName,
168
- {
169
- kind: SpanKind.CLIENT,
170
- attributes: {
171
- "actor.name": actorName,
172
- "actor.class": actorClassName,
173
- "db.system": "durable_object_storage",
174
- "db.operation": "delete",
175
- "db.key": key
176
- }
177
- },
178
- async (span) => {
179
- try {
180
- const result = await value.call(target, key);
181
- span.setAttributes({
182
- "db.result.deleted": result
183
- });
184
- span.setStatus({ code: SpanStatusCode.OK });
185
- return result;
186
- } catch (error) {
187
- span.recordException(error);
188
- span.setStatus({
189
- code: SpanStatusCode.ERROR,
190
- message: error instanceof Error ? error.message : String(error)
191
- });
192
- throw error;
193
- } finally {
194
- span.end();
195
- }
196
- }
197
- );
198
- };
199
- }
200
- if (typeof value === "function") {
201
- return value.bind(target);
202
- }
203
- return value;
204
- }
205
- };
206
- return wrap(storage, storageHandler);
25
+ if (!storage || typeof storage !== "object") return storage;
26
+ const actorClassName = actorClass.name || "Actor";
27
+ const actorName = actorInstance.name || actorClassName;
28
+ return wrap(storage, { get(target, prop) {
29
+ const value = Reflect.get(target, prop);
30
+ if (prop === "exec" && typeof value === "function") return function instrumentedExec(query, ...params) {
31
+ const tracer = getTracer$4();
32
+ const spanName = `Actor ${actorName}: storage.exec`;
33
+ return tracer.startActiveSpan(spanName, {
34
+ kind: SpanKind.CLIENT,
35
+ attributes: {
36
+ "actor.name": actorName,
37
+ "actor.class": actorClassName,
38
+ "db.system": "sqlite",
39
+ "db.operation": "exec",
40
+ "db.statement": query,
41
+ "db.statement.params_count": params.length
42
+ }
43
+ }, (span) => {
44
+ try {
45
+ const result = value.call(target, query, ...params);
46
+ span.setStatus({ code: SpanStatusCode.OK });
47
+ return result;
48
+ } catch (error) {
49
+ span.recordException(error);
50
+ span.setStatus({
51
+ code: SpanStatusCode.ERROR,
52
+ message: error instanceof Error ? error.message : String(error)
53
+ });
54
+ throw error;
55
+ } finally {
56
+ span.end();
57
+ }
58
+ });
59
+ };
60
+ if (prop === "query" && typeof value === "function") return function instrumentedQuery(query, ...params) {
61
+ const tracer = getTracer$4();
62
+ const spanName = `Actor ${actorName}: storage.query`;
63
+ return tracer.startActiveSpan(spanName, {
64
+ kind: SpanKind.CLIENT,
65
+ attributes: {
66
+ "actor.name": actorName,
67
+ "actor.class": actorClassName,
68
+ "db.system": "sqlite",
69
+ "db.operation": "query",
70
+ "db.statement": query,
71
+ "db.statement.params_count": params.length
72
+ }
73
+ }, (span) => {
74
+ try {
75
+ const result = value.call(target, query, ...params);
76
+ span.setStatus({ code: SpanStatusCode.OK });
77
+ return result;
78
+ } catch (error) {
79
+ span.recordException(error);
80
+ span.setStatus({
81
+ code: SpanStatusCode.ERROR,
82
+ message: error instanceof Error ? error.message : String(error)
83
+ });
84
+ throw error;
85
+ } finally {
86
+ span.end();
87
+ }
88
+ });
89
+ };
90
+ if (prop === "get" && typeof value === "function") return async function instrumentedGet(key) {
91
+ const tracer = getTracer$4();
92
+ const spanName = `Actor ${actorName}: storage.get`;
93
+ return tracer.startActiveSpan(spanName, {
94
+ kind: SpanKind.CLIENT,
95
+ attributes: {
96
+ "actor.name": actorName,
97
+ "actor.class": actorClassName,
98
+ "db.system": "durable_object_storage",
99
+ "db.operation": "get",
100
+ "db.key": key
101
+ }
102
+ }, async (span) => {
103
+ try {
104
+ const result = await value.call(target, key);
105
+ span.setAttributes({ "db.result.found": result !== null && result !== void 0 });
106
+ span.setStatus({ code: SpanStatusCode.OK });
107
+ return result;
108
+ } catch (error) {
109
+ span.recordException(error);
110
+ span.setStatus({
111
+ code: SpanStatusCode.ERROR,
112
+ message: error instanceof Error ? error.message : String(error)
113
+ });
114
+ throw error;
115
+ } finally {
116
+ span.end();
117
+ }
118
+ });
119
+ };
120
+ if (prop === "put" && typeof value === "function") return async function instrumentedPut(key, val) {
121
+ const tracer = getTracer$4();
122
+ const spanName = `Actor ${actorName}: storage.put`;
123
+ return tracer.startActiveSpan(spanName, {
124
+ kind: SpanKind.CLIENT,
125
+ attributes: {
126
+ "actor.name": actorName,
127
+ "actor.class": actorClassName,
128
+ "db.system": "durable_object_storage",
129
+ "db.operation": "put",
130
+ "db.key": key,
131
+ "db.value_type": typeof val
132
+ }
133
+ }, async (span) => {
134
+ try {
135
+ await value.call(target, key, val);
136
+ span.setStatus({ code: SpanStatusCode.OK });
137
+ } catch (error) {
138
+ span.recordException(error);
139
+ span.setStatus({
140
+ code: SpanStatusCode.ERROR,
141
+ message: error instanceof Error ? error.message : String(error)
142
+ });
143
+ throw error;
144
+ } finally {
145
+ span.end();
146
+ }
147
+ });
148
+ };
149
+ if (prop === "delete" && typeof value === "function") return async function instrumentedDelete(key) {
150
+ const tracer = getTracer$4();
151
+ const spanName = `Actor ${actorName}: storage.delete`;
152
+ return tracer.startActiveSpan(spanName, {
153
+ kind: SpanKind.CLIENT,
154
+ attributes: {
155
+ "actor.name": actorName,
156
+ "actor.class": actorClassName,
157
+ "db.system": "durable_object_storage",
158
+ "db.operation": "delete",
159
+ "db.key": key
160
+ }
161
+ }, async (span) => {
162
+ try {
163
+ const result = await value.call(target, key);
164
+ span.setAttributes({ "db.result.deleted": result });
165
+ span.setStatus({ code: SpanStatusCode.OK });
166
+ return result;
167
+ } catch (error) {
168
+ span.recordException(error);
169
+ span.setStatus({
170
+ code: SpanStatusCode.ERROR,
171
+ message: error instanceof Error ? error.message : String(error)
172
+ });
173
+ throw error;
174
+ } finally {
175
+ span.end();
176
+ }
177
+ });
178
+ };
179
+ if (typeof value === "function") return value.bind(target);
180
+ return value;
181
+ } });
207
182
  }
208
- function getTracer2() {
209
- return trace.getTracer("autotel-cloudflare-actors");
183
+
184
+ //#endregion
185
+ //#region src/actors/alarms.ts
186
+ /**
187
+ * Actor alarms instrumentation
188
+ *
189
+ * Traces operations on actor.alarms
190
+ */
191
+ /**
192
+ * Get the tracer instance
193
+ */
194
+ function getTracer$3() {
195
+ return trace.getTracer("autotel-cloudflare-actors");
210
196
  }
197
+ /**
198
+ * Instrument Actor alarms for tracing
199
+ *
200
+ * Captures:
201
+ * - set: Schedule a single alarm
202
+ * - setMultiple: Schedule multiple alarms
203
+ * - cancel: Cancel an alarm
204
+ * - cancelAll: Cancel all alarms
205
+ */
211
206
  function instrumentActorAlarms(alarms, actorInstance, actorClass) {
212
- if (!alarms || typeof alarms !== "object") {
213
- return alarms;
214
- }
215
- const actorClassName = actorClass.name || "Actor";
216
- const actorName = actorInstance.name || actorClassName;
217
- const alarmsHandler = {
218
- get(target, prop) {
219
- const value = Reflect.get(target, prop);
220
- if (prop === "set" && typeof value === "function") {
221
- return async function instrumentedSet(...args) {
222
- const tracer = getTracer2();
223
- const spanName = `Actor ${actorName}: alarms.set`;
224
- const alarmAttributes = {
225
- "actor.name": actorName,
226
- "actor.class": actorClassName,
227
- "alarm.operation": "set"
228
- };
229
- if (args.length > 0) {
230
- const firstArg = args[0];
231
- if (typeof firstArg === "number") {
232
- alarmAttributes["alarm.delay_ms"] = firstArg;
233
- } else if (firstArg instanceof Date) {
234
- alarmAttributes["alarm.scheduled_time"] = firstArg.toISOString();
235
- } else if (typeof firstArg === "string") {
236
- alarmAttributes["alarm.cron"] = firstArg;
237
- }
238
- }
239
- return tracer.startActiveSpan(
240
- spanName,
241
- {
242
- kind: SpanKind.CLIENT,
243
- attributes: alarmAttributes
244
- },
245
- async (span) => {
246
- try {
247
- const result = await value.apply(target, args);
248
- span.setStatus({ code: SpanStatusCode.OK });
249
- return result;
250
- } catch (error) {
251
- span.recordException(error);
252
- span.setStatus({
253
- code: SpanStatusCode.ERROR,
254
- message: error instanceof Error ? error.message : String(error)
255
- });
256
- throw error;
257
- } finally {
258
- span.end();
259
- }
260
- }
261
- );
262
- };
263
- }
264
- if (prop === "setMultiple" && typeof value === "function") {
265
- return async function instrumentedSetMultiple(alarmDefs) {
266
- const tracer = getTracer2();
267
- const spanName = `Actor ${actorName}: alarms.setMultiple`;
268
- return tracer.startActiveSpan(
269
- spanName,
270
- {
271
- kind: SpanKind.CLIENT,
272
- attributes: {
273
- "actor.name": actorName,
274
- "actor.class": actorClassName,
275
- "alarm.operation": "setMultiple",
276
- "alarm.count": Array.isArray(alarmDefs) ? alarmDefs.length : 0
277
- }
278
- },
279
- async (span) => {
280
- try {
281
- const result = await value.call(target, alarmDefs);
282
- span.setStatus({ code: SpanStatusCode.OK });
283
- return result;
284
- } catch (error) {
285
- span.recordException(error);
286
- span.setStatus({
287
- code: SpanStatusCode.ERROR,
288
- message: error instanceof Error ? error.message : String(error)
289
- });
290
- throw error;
291
- } finally {
292
- span.end();
293
- }
294
- }
295
- );
296
- };
297
- }
298
- if (prop === "cancel" && typeof value === "function") {
299
- return async function instrumentedCancel(alarmId) {
300
- const tracer = getTracer2();
301
- const spanName = `Actor ${actorName}: alarms.cancel`;
302
- return tracer.startActiveSpan(
303
- spanName,
304
- {
305
- kind: SpanKind.CLIENT,
306
- attributes: {
307
- "actor.name": actorName,
308
- "actor.class": actorClassName,
309
- "alarm.operation": "cancel",
310
- "alarm.id": alarmId
311
- }
312
- },
313
- async (span) => {
314
- try {
315
- const result = await value.call(target, alarmId);
316
- span.setStatus({ code: SpanStatusCode.OK });
317
- return result;
318
- } catch (error) {
319
- span.recordException(error);
320
- span.setStatus({
321
- code: SpanStatusCode.ERROR,
322
- message: error instanceof Error ? error.message : String(error)
323
- });
324
- throw error;
325
- } finally {
326
- span.end();
327
- }
328
- }
329
- );
330
- };
331
- }
332
- if (prop === "cancelAll" && typeof value === "function") {
333
- return async function instrumentedCancelAll() {
334
- const tracer = getTracer2();
335
- const spanName = `Actor ${actorName}: alarms.cancelAll`;
336
- return tracer.startActiveSpan(
337
- spanName,
338
- {
339
- kind: SpanKind.CLIENT,
340
- attributes: {
341
- "actor.name": actorName,
342
- "actor.class": actorClassName,
343
- "alarm.operation": "cancelAll"
344
- }
345
- },
346
- async (span) => {
347
- try {
348
- const result = await value.call(target);
349
- span.setStatus({ code: SpanStatusCode.OK });
350
- return result;
351
- } catch (error) {
352
- span.recordException(error);
353
- span.setStatus({
354
- code: SpanStatusCode.ERROR,
355
- message: error instanceof Error ? error.message : String(error)
356
- });
357
- throw error;
358
- } finally {
359
- span.end();
360
- }
361
- }
362
- );
363
- };
364
- }
365
- if (typeof value === "function") {
366
- return value.bind(target);
367
- }
368
- return value;
369
- }
370
- };
371
- return wrap(alarms, alarmsHandler);
207
+ if (!alarms || typeof alarms !== "object") return alarms;
208
+ const actorClassName = actorClass.name || "Actor";
209
+ const actorName = actorInstance.name || actorClassName;
210
+ return wrap(alarms, { get(target, prop) {
211
+ const value = Reflect.get(target, prop);
212
+ if (prop === "set" && typeof value === "function") return async function instrumentedSet(...args) {
213
+ const tracer = getTracer$3();
214
+ const spanName = `Actor ${actorName}: alarms.set`;
215
+ const alarmAttributes = {
216
+ "actor.name": actorName,
217
+ "actor.class": actorClassName,
218
+ "alarm.operation": "set"
219
+ };
220
+ if (args.length > 0) {
221
+ const firstArg = args[0];
222
+ if (typeof firstArg === "number") alarmAttributes["alarm.delay_ms"] = firstArg;
223
+ else if (firstArg instanceof Date) alarmAttributes["alarm.scheduled_time"] = firstArg.toISOString();
224
+ else if (typeof firstArg === "string") alarmAttributes["alarm.cron"] = firstArg;
225
+ }
226
+ return tracer.startActiveSpan(spanName, {
227
+ kind: SpanKind.CLIENT,
228
+ attributes: alarmAttributes
229
+ }, async (span) => {
230
+ try {
231
+ const result = await value.apply(target, args);
232
+ span.setStatus({ code: SpanStatusCode.OK });
233
+ return result;
234
+ } catch (error) {
235
+ span.recordException(error);
236
+ span.setStatus({
237
+ code: SpanStatusCode.ERROR,
238
+ message: error instanceof Error ? error.message : String(error)
239
+ });
240
+ throw error;
241
+ } finally {
242
+ span.end();
243
+ }
244
+ });
245
+ };
246
+ if (prop === "setMultiple" && typeof value === "function") return async function instrumentedSetMultiple(alarmDefs) {
247
+ const tracer = getTracer$3();
248
+ const spanName = `Actor ${actorName}: alarms.setMultiple`;
249
+ return tracer.startActiveSpan(spanName, {
250
+ kind: SpanKind.CLIENT,
251
+ attributes: {
252
+ "actor.name": actorName,
253
+ "actor.class": actorClassName,
254
+ "alarm.operation": "setMultiple",
255
+ "alarm.count": Array.isArray(alarmDefs) ? alarmDefs.length : 0
256
+ }
257
+ }, async (span) => {
258
+ try {
259
+ const result = await value.call(target, alarmDefs);
260
+ span.setStatus({ code: SpanStatusCode.OK });
261
+ return result;
262
+ } catch (error) {
263
+ span.recordException(error);
264
+ span.setStatus({
265
+ code: SpanStatusCode.ERROR,
266
+ message: error instanceof Error ? error.message : String(error)
267
+ });
268
+ throw error;
269
+ } finally {
270
+ span.end();
271
+ }
272
+ });
273
+ };
274
+ if (prop === "cancel" && typeof value === "function") return async function instrumentedCancel(alarmId) {
275
+ const tracer = getTracer$3();
276
+ const spanName = `Actor ${actorName}: alarms.cancel`;
277
+ return tracer.startActiveSpan(spanName, {
278
+ kind: SpanKind.CLIENT,
279
+ attributes: {
280
+ "actor.name": actorName,
281
+ "actor.class": actorClassName,
282
+ "alarm.operation": "cancel",
283
+ "alarm.id": alarmId
284
+ }
285
+ }, async (span) => {
286
+ try {
287
+ const result = await value.call(target, alarmId);
288
+ span.setStatus({ code: SpanStatusCode.OK });
289
+ return result;
290
+ } catch (error) {
291
+ span.recordException(error);
292
+ span.setStatus({
293
+ code: SpanStatusCode.ERROR,
294
+ message: error instanceof Error ? error.message : String(error)
295
+ });
296
+ throw error;
297
+ } finally {
298
+ span.end();
299
+ }
300
+ });
301
+ };
302
+ if (prop === "cancelAll" && typeof value === "function") return async function instrumentedCancelAll() {
303
+ const tracer = getTracer$3();
304
+ const spanName = `Actor ${actorName}: alarms.cancelAll`;
305
+ return tracer.startActiveSpan(spanName, {
306
+ kind: SpanKind.CLIENT,
307
+ attributes: {
308
+ "actor.name": actorName,
309
+ "actor.class": actorClassName,
310
+ "alarm.operation": "cancelAll"
311
+ }
312
+ }, async (span) => {
313
+ try {
314
+ const result = await value.call(target);
315
+ span.setStatus({ code: SpanStatusCode.OK });
316
+ return result;
317
+ } catch (error) {
318
+ span.recordException(error);
319
+ span.setStatus({
320
+ code: SpanStatusCode.ERROR,
321
+ message: error instanceof Error ? error.message : String(error)
322
+ });
323
+ throw error;
324
+ } finally {
325
+ span.end();
326
+ }
327
+ });
328
+ };
329
+ if (typeof value === "function") return value.bind(target);
330
+ return value;
331
+ } });
372
332
  }
373
- function getTracer3() {
374
- return trace.getTracer("autotel-cloudflare-actors");
333
+
334
+ //#endregion
335
+ //#region src/actors/sockets.ts
336
+ /**
337
+ * Actor sockets instrumentation
338
+ *
339
+ * Traces operations on actor.sockets
340
+ */
341
+ /**
342
+ * Get the tracer instance
343
+ */
344
+ function getTracer$2() {
345
+ return trace.getTracer("autotel-cloudflare-actors");
375
346
  }
347
+ /**
348
+ * Instrument Actor sockets for tracing
349
+ *
350
+ * Captures:
351
+ * - acceptWebSocket: Accept an incoming WebSocket connection
352
+ * - broadcast: Send message to all connected sockets
353
+ * - send: Send message to a specific socket
354
+ */
376
355
  function instrumentActorSockets(sockets, actorInstance, actorClass) {
377
- if (!sockets || typeof sockets !== "object") {
378
- return sockets;
379
- }
380
- const actorClassName = actorClass.name || "Actor";
381
- const actorName = actorInstance.name || actorClassName;
382
- const socketsHandler = {
383
- get(target, prop) {
384
- const value = Reflect.get(target, prop);
385
- if (prop === "acceptWebSocket" && typeof value === "function") {
386
- return function instrumentedAcceptWebSocket(request) {
387
- const tracer = getTracer3();
388
- const spanName = `Actor ${actorName}: sockets.acceptWebSocket`;
389
- return tracer.startActiveSpan(
390
- spanName,
391
- {
392
- kind: SpanKind.SERVER,
393
- attributes: {
394
- "actor.name": actorName,
395
- "actor.class": actorClassName,
396
- "websocket.operation": "accept",
397
- "url.full": request.url
398
- }
399
- },
400
- (span) => {
401
- try {
402
- const result = value.call(target, request);
403
- span.setStatus({ code: SpanStatusCode.OK });
404
- return result;
405
- } catch (error) {
406
- span.recordException(error);
407
- span.setStatus({
408
- code: SpanStatusCode.ERROR,
409
- message: error instanceof Error ? error.message : String(error)
410
- });
411
- throw error;
412
- } finally {
413
- span.end();
414
- }
415
- }
416
- );
417
- };
418
- }
419
- if (prop === "broadcast" && typeof value === "function") {
420
- return function instrumentedBroadcast(message) {
421
- const tracer = getTracer3();
422
- const spanName = `Actor ${actorName}: sockets.broadcast`;
423
- tracer.startActiveSpan(
424
- spanName,
425
- {
426
- kind: SpanKind.PRODUCER,
427
- attributes: {
428
- "actor.name": actorName,
429
- "actor.class": actorClassName,
430
- "websocket.operation": "broadcast",
431
- "websocket.message.type": typeof message,
432
- "websocket.message.size": typeof message === "string" ? message.length : message instanceof ArrayBuffer ? message.byteLength : 0
433
- }
434
- },
435
- (span) => {
436
- try {
437
- value.call(target, message);
438
- span.setStatus({ code: SpanStatusCode.OK });
439
- } catch (error) {
440
- span.recordException(error);
441
- span.setStatus({
442
- code: SpanStatusCode.ERROR,
443
- message: error instanceof Error ? error.message : String(error)
444
- });
445
- throw error;
446
- } finally {
447
- span.end();
448
- }
449
- }
450
- );
451
- };
452
- }
453
- if (prop === "send" && typeof value === "function") {
454
- return function instrumentedSend(ws, message) {
455
- const tracer = getTracer3();
456
- const spanName = `Actor ${actorName}: sockets.send`;
457
- tracer.startActiveSpan(
458
- spanName,
459
- {
460
- kind: SpanKind.PRODUCER,
461
- attributes: {
462
- "actor.name": actorName,
463
- "actor.class": actorClassName,
464
- "websocket.operation": "send",
465
- "websocket.message.type": typeof message,
466
- "websocket.message.size": typeof message === "string" ? message.length : message instanceof ArrayBuffer ? message.byteLength : 0
467
- }
468
- },
469
- (span) => {
470
- try {
471
- value.call(target, ws, message);
472
- span.setStatus({ code: SpanStatusCode.OK });
473
- } catch (error) {
474
- span.recordException(error);
475
- span.setStatus({
476
- code: SpanStatusCode.ERROR,
477
- message: error instanceof Error ? error.message : String(error)
478
- });
479
- throw error;
480
- } finally {
481
- span.end();
482
- }
483
- }
484
- );
485
- };
486
- }
487
- if (prop === "getConnections" && typeof value === "function") {
488
- return function instrumentedGetConnections() {
489
- const tracer = getTracer3();
490
- const spanName = `Actor ${actorName}: sockets.getConnections`;
491
- return tracer.startActiveSpan(
492
- spanName,
493
- {
494
- kind: SpanKind.CLIENT,
495
- attributes: {
496
- "actor.name": actorName,
497
- "actor.class": actorClassName,
498
- "websocket.operation": "getConnections"
499
- }
500
- },
501
- (span) => {
502
- try {
503
- const result = value.call(target);
504
- if (Array.isArray(result)) {
505
- span.setAttribute("websocket.connections.count", result.length);
506
- }
507
- span.setStatus({ code: SpanStatusCode.OK });
508
- return result;
509
- } catch (error) {
510
- span.recordException(error);
511
- span.setStatus({
512
- code: SpanStatusCode.ERROR,
513
- message: error instanceof Error ? error.message : String(error)
514
- });
515
- throw error;
516
- } finally {
517
- span.end();
518
- }
519
- }
520
- );
521
- };
522
- }
523
- if (typeof value === "function") {
524
- return value.bind(target);
525
- }
526
- return value;
527
- }
528
- };
529
- return wrap(sockets, socketsHandler);
356
+ if (!sockets || typeof sockets !== "object") return sockets;
357
+ const actorClassName = actorClass.name || "Actor";
358
+ const actorName = actorInstance.name || actorClassName;
359
+ return wrap(sockets, { get(target, prop) {
360
+ const value = Reflect.get(target, prop);
361
+ if (prop === "acceptWebSocket" && typeof value === "function") return function instrumentedAcceptWebSocket(request) {
362
+ const tracer = getTracer$2();
363
+ const spanName = `Actor ${actorName}: sockets.acceptWebSocket`;
364
+ return tracer.startActiveSpan(spanName, {
365
+ kind: SpanKind.SERVER,
366
+ attributes: {
367
+ "actor.name": actorName,
368
+ "actor.class": actorClassName,
369
+ "websocket.operation": "accept",
370
+ "url.full": request.url
371
+ }
372
+ }, (span) => {
373
+ try {
374
+ const result = value.call(target, request);
375
+ span.setStatus({ code: SpanStatusCode.OK });
376
+ return result;
377
+ } catch (error) {
378
+ span.recordException(error);
379
+ span.setStatus({
380
+ code: SpanStatusCode.ERROR,
381
+ message: error instanceof Error ? error.message : String(error)
382
+ });
383
+ throw error;
384
+ } finally {
385
+ span.end();
386
+ }
387
+ });
388
+ };
389
+ if (prop === "broadcast" && typeof value === "function") return function instrumentedBroadcast(message) {
390
+ const tracer = getTracer$2();
391
+ const spanName = `Actor ${actorName}: sockets.broadcast`;
392
+ tracer.startActiveSpan(spanName, {
393
+ kind: SpanKind.PRODUCER,
394
+ attributes: {
395
+ "actor.name": actorName,
396
+ "actor.class": actorClassName,
397
+ "websocket.operation": "broadcast",
398
+ "websocket.message.type": typeof message,
399
+ "websocket.message.size": typeof message === "string" ? message.length : message instanceof ArrayBuffer ? message.byteLength : 0
400
+ }
401
+ }, (span) => {
402
+ try {
403
+ value.call(target, message);
404
+ span.setStatus({ code: SpanStatusCode.OK });
405
+ } catch (error) {
406
+ span.recordException(error);
407
+ span.setStatus({
408
+ code: SpanStatusCode.ERROR,
409
+ message: error instanceof Error ? error.message : String(error)
410
+ });
411
+ throw error;
412
+ } finally {
413
+ span.end();
414
+ }
415
+ });
416
+ };
417
+ if (prop === "send" && typeof value === "function") return function instrumentedSend(ws, message) {
418
+ const tracer = getTracer$2();
419
+ const spanName = `Actor ${actorName}: sockets.send`;
420
+ tracer.startActiveSpan(spanName, {
421
+ kind: SpanKind.PRODUCER,
422
+ attributes: {
423
+ "actor.name": actorName,
424
+ "actor.class": actorClassName,
425
+ "websocket.operation": "send",
426
+ "websocket.message.type": typeof message,
427
+ "websocket.message.size": typeof message === "string" ? message.length : message instanceof ArrayBuffer ? message.byteLength : 0
428
+ }
429
+ }, (span) => {
430
+ try {
431
+ value.call(target, ws, message);
432
+ span.setStatus({ code: SpanStatusCode.OK });
433
+ } catch (error) {
434
+ span.recordException(error);
435
+ span.setStatus({
436
+ code: SpanStatusCode.ERROR,
437
+ message: error instanceof Error ? error.message : String(error)
438
+ });
439
+ throw error;
440
+ } finally {
441
+ span.end();
442
+ }
443
+ });
444
+ };
445
+ if (prop === "getConnections" && typeof value === "function") return function instrumentedGetConnections() {
446
+ const tracer = getTracer$2();
447
+ const spanName = `Actor ${actorName}: sockets.getConnections`;
448
+ return tracer.startActiveSpan(spanName, {
449
+ kind: SpanKind.CLIENT,
450
+ attributes: {
451
+ "actor.name": actorName,
452
+ "actor.class": actorClassName,
453
+ "websocket.operation": "getConnections"
454
+ }
455
+ }, (span) => {
456
+ try {
457
+ const result = value.call(target);
458
+ if (Array.isArray(result)) span.setAttribute("websocket.connections.count", result.length);
459
+ span.setStatus({ code: SpanStatusCode.OK });
460
+ return result;
461
+ } catch (error) {
462
+ span.recordException(error);
463
+ span.setStatus({
464
+ code: SpanStatusCode.ERROR,
465
+ message: error instanceof Error ? error.message : String(error)
466
+ });
467
+ throw error;
468
+ } finally {
469
+ span.end();
470
+ }
471
+ });
472
+ };
473
+ if (typeof value === "function") return value.bind(target);
474
+ return value;
475
+ } });
530
476
  }
531
477
 
532
- // src/actors/instrument-actor.ts
533
- var coldStarts = /* @__PURE__ */ new WeakMap();
478
+ //#endregion
479
+ //#region src/actors/instrument-actor.ts
480
+ /**
481
+ * Actor class instrumentation for @cloudflare/actors
482
+ *
483
+ * Wraps Actor lifecycle methods with OpenTelemetry tracing:
484
+ * - onInit: Traced as 'actor.lifecycle': 'init'
485
+ * - onRequest: Traced with full HTTP semantics
486
+ * - onAlarm: Traced as 'actor.lifecycle': 'alarm'
487
+ * - onPersist: Traced as 'actor.lifecycle': 'persist'
488
+ * - WebSocket methods: Traced with socket semantics
489
+ */
490
+ /**
491
+ * Track cold starts per Actor class
492
+ */
493
+ const coldStarts = /* @__PURE__ */ new WeakMap();
534
494
  function isColdStart(actorClass) {
535
- if (!coldStarts.has(actorClass)) {
536
- coldStarts.set(actorClass, true);
537
- return true;
538
- }
539
- return false;
495
+ if (!coldStarts.has(actorClass)) {
496
+ coldStarts.set(actorClass, true);
497
+ return true;
498
+ }
499
+ return false;
540
500
  }
541
- function getTracer4() {
542
- return trace.getTracer("autotel-cloudflare-actors");
501
+ /**
502
+ * Get the tracer instance
503
+ */
504
+ function getTracer$1() {
505
+ return trace.getTracer("autotel-cloudflare-actors");
543
506
  }
507
+ /**
508
+ * Default span name formatter
509
+ */
544
510
  function defaultSpanNameFormatter(actorName, actorClass, lifecycle) {
545
- const displayName = actorName || actorClass;
546
- return `Actor ${displayName}: ${lifecycle}`;
511
+ return `Actor ${actorName || actorClass}: ${lifecycle}`;
547
512
  }
513
+ /**
514
+ * Create base Actor span attributes
515
+ */
548
516
  function createActorAttributes(actorInstance, actorClass, lifecycle) {
549
- return {
550
- "actor.name": actorInstance.name || "unknown",
551
- "actor.class": actorClass.name || "Actor",
552
- "actor.lifecycle": lifecycle,
553
- "actor.coldstart": isColdStart(actorClass),
554
- ...actorInstance.identifier && { "actor.identifier": actorInstance.identifier }
555
- };
517
+ return {
518
+ "actor.name": actorInstance.name || "unknown",
519
+ "actor.class": actorClass.name || "Actor",
520
+ "actor.lifecycle": lifecycle,
521
+ "actor.coldstart": isColdStart(actorClass),
522
+ ...actorInstance.identifier && { "actor.identifier": actorInstance.identifier }
523
+ };
556
524
  }
525
+ /**
526
+ * Instrument the onInit lifecycle method
527
+ */
557
528
  function instrumentOnInit(originalMethod, actorInstance, actorClass, options) {
558
- return async function instrumentedOnInit() {
559
- const tracer = getTracer4();
560
- const actorClassName = actorClass.name || "Actor";
561
- const spanName = options.spanNameFormatter ? options.spanNameFormatter(actorInstance.name || "", "init") : defaultSpanNameFormatter(actorInstance.name || "", actorClassName, "init");
562
- return tracer.startActiveSpan(
563
- spanName,
564
- {
565
- kind: SpanKind.INTERNAL,
566
- attributes: createActorAttributes(actorInstance, actorClass, "init")
567
- },
568
- async (span) => {
569
- try {
570
- await originalMethod.call(actorInstance);
571
- span.setStatus({ code: SpanStatusCode.OK });
572
- } catch (error) {
573
- span.recordException(error);
574
- span.setStatus({
575
- code: SpanStatusCode.ERROR,
576
- message: error instanceof Error ? error.message : String(error)
577
- });
578
- throw error;
579
- } finally {
580
- span.end();
581
- }
582
- }
583
- );
584
- };
529
+ return async function instrumentedOnInit() {
530
+ const tracer = getTracer$1();
531
+ const actorClassName = actorClass.name || "Actor";
532
+ const spanName = options.spanNameFormatter ? options.spanNameFormatter(actorInstance.name || "", "init") : defaultSpanNameFormatter(actorInstance.name || "", actorClassName, "init");
533
+ return tracer.startActiveSpan(spanName, {
534
+ kind: SpanKind.INTERNAL,
535
+ attributes: createActorAttributes(actorInstance, actorClass, "init")
536
+ }, async (span) => {
537
+ try {
538
+ await originalMethod.call(actorInstance);
539
+ span.setStatus({ code: SpanStatusCode.OK });
540
+ } catch (error) {
541
+ span.recordException(error);
542
+ span.setStatus({
543
+ code: SpanStatusCode.ERROR,
544
+ message: error instanceof Error ? error.message : String(error)
545
+ });
546
+ throw error;
547
+ } finally {
548
+ span.end();
549
+ }
550
+ });
551
+ };
585
552
  }
553
+ /**
554
+ * Instrument the onRequest lifecycle method
555
+ */
586
556
  function instrumentOnRequest(originalMethod, actorInstance, actorClass, options) {
587
- return async function instrumentedOnRequest(request) {
588
- const tracer = getTracer4();
589
- const parentContext = propagation.extract(context.active(), request.headers);
590
- const url = new URL(request.url);
591
- const actorClassName = actorClass.name || "Actor";
592
- const spanName = options.spanNameFormatter ? options.spanNameFormatter(actorInstance.name || "", "request") : `Actor ${actorInstance.name || actorClassName}: ${request.method} ${url.pathname}`;
593
- return tracer.startActiveSpan(
594
- spanName,
595
- {
596
- kind: SpanKind.SERVER,
597
- attributes: {
598
- ...createActorAttributes(actorInstance, actorClass, "request"),
599
- "http.request.method": request.method,
600
- "url.full": request.url,
601
- "url.path": url.pathname,
602
- "url.query": url.search
603
- }
604
- },
605
- parentContext,
606
- async (span) => {
607
- try {
608
- const response = await originalMethod.call(actorInstance, request);
609
- span.setAttributes({
610
- "http.response.status_code": response.status
611
- });
612
- if (response.ok) {
613
- span.setStatus({ code: SpanStatusCode.OK });
614
- } else {
615
- span.setStatus({ code: SpanStatusCode.ERROR });
616
- }
617
- return response;
618
- } catch (error) {
619
- span.recordException(error);
620
- span.setStatus({
621
- code: SpanStatusCode.ERROR,
622
- message: error instanceof Error ? error.message : String(error)
623
- });
624
- throw error;
625
- } finally {
626
- span.end();
627
- }
628
- }
629
- );
630
- };
557
+ return async function instrumentedOnRequest(request) {
558
+ const tracer = getTracer$1();
559
+ const parentContext = propagation.extract(context.active(), request.headers);
560
+ const url = new URL(request.url);
561
+ const actorClassName = actorClass.name || "Actor";
562
+ const spanName = options.spanNameFormatter ? options.spanNameFormatter(actorInstance.name || "", "request") : `Actor ${actorInstance.name || actorClassName}: ${request.method} ${url.pathname}`;
563
+ return tracer.startActiveSpan(spanName, {
564
+ kind: SpanKind.SERVER,
565
+ attributes: {
566
+ ...createActorAttributes(actorInstance, actorClass, "request"),
567
+ "http.request.method": request.method,
568
+ "url.full": request.url,
569
+ "url.path": url.pathname,
570
+ "url.query": url.search
571
+ }
572
+ }, parentContext, async (span) => {
573
+ try {
574
+ const response = await originalMethod.call(actorInstance, request);
575
+ span.setAttributes({ "http.response.status_code": response.status });
576
+ if (response.ok) span.setStatus({ code: SpanStatusCode.OK });
577
+ else span.setStatus({ code: SpanStatusCode.ERROR });
578
+ return response;
579
+ } catch (error) {
580
+ span.recordException(error);
581
+ span.setStatus({
582
+ code: SpanStatusCode.ERROR,
583
+ message: error instanceof Error ? error.message : String(error)
584
+ });
585
+ throw error;
586
+ } finally {
587
+ span.end();
588
+ }
589
+ });
590
+ };
631
591
  }
592
+ /**
593
+ * Instrument the onAlarm lifecycle method
594
+ */
632
595
  function instrumentOnAlarm(originalMethod, actorInstance, actorClass, options) {
633
- return async function instrumentedOnAlarm(alarmInfo) {
634
- const tracer = getTracer4();
635
- const actorClassName = actorClass.name || "Actor";
636
- const spanName = options.spanNameFormatter ? options.spanNameFormatter(actorInstance.name || "", "alarm") : defaultSpanNameFormatter(actorInstance.name || "", actorClassName, "alarm");
637
- return tracer.startActiveSpan(
638
- spanName,
639
- {
640
- kind: SpanKind.INTERNAL,
641
- attributes: {
642
- ...createActorAttributes(actorInstance, actorClass, "alarm"),
643
- "faas.trigger": "timer"
644
- }
645
- },
646
- async (span) => {
647
- try {
648
- await originalMethod.call(actorInstance, alarmInfo);
649
- span.setStatus({ code: SpanStatusCode.OK });
650
- } catch (error) {
651
- span.recordException(error);
652
- span.setStatus({
653
- code: SpanStatusCode.ERROR,
654
- message: error instanceof Error ? error.message : String(error)
655
- });
656
- throw error;
657
- } finally {
658
- span.end();
659
- }
660
- }
661
- );
662
- };
596
+ return async function instrumentedOnAlarm(alarmInfo) {
597
+ const tracer = getTracer$1();
598
+ const actorClassName = actorClass.name || "Actor";
599
+ const spanName = options.spanNameFormatter ? options.spanNameFormatter(actorInstance.name || "", "alarm") : defaultSpanNameFormatter(actorInstance.name || "", actorClassName, "alarm");
600
+ return tracer.startActiveSpan(spanName, {
601
+ kind: SpanKind.INTERNAL,
602
+ attributes: {
603
+ ...createActorAttributes(actorInstance, actorClass, "alarm"),
604
+ "faas.trigger": "timer"
605
+ }
606
+ }, async (span) => {
607
+ try {
608
+ await originalMethod.call(actorInstance, alarmInfo);
609
+ span.setStatus({ code: SpanStatusCode.OK });
610
+ } catch (error) {
611
+ span.recordException(error);
612
+ span.setStatus({
613
+ code: SpanStatusCode.ERROR,
614
+ message: error instanceof Error ? error.message : String(error)
615
+ });
616
+ throw error;
617
+ } finally {
618
+ span.end();
619
+ }
620
+ });
621
+ };
663
622
  }
623
+ /**
624
+ * Instrument the onPersist lifecycle method
625
+ */
664
626
  function instrumentOnPersist(originalMethod, actorInstance, actorClass, options) {
665
- if (!options.capturePersistEvents) {
666
- return originalMethod;
667
- }
668
- return function instrumentedOnPersist(key, value) {
669
- const tracer = getTracer4();
670
- const actorClassName = actorClass.name || "Actor";
671
- const spanName = options.spanNameFormatter ? options.spanNameFormatter(actorInstance.name || "", "persist") : `Actor ${actorInstance.name || actorClassName}: persist ${key}`;
672
- tracer.startActiveSpan(
673
- spanName,
674
- {
675
- kind: SpanKind.INTERNAL,
676
- attributes: {
677
- ...createActorAttributes(actorInstance, actorClass, "persist"),
678
- "actor.persist.key": key,
679
- "actor.persist.value_type": typeof value
680
- }
681
- },
682
- (span) => {
683
- try {
684
- originalMethod.call(actorInstance, key, value);
685
- span.setStatus({ code: SpanStatusCode.OK });
686
- } catch (error) {
687
- span.recordException(error);
688
- span.setStatus({
689
- code: SpanStatusCode.ERROR,
690
- message: error instanceof Error ? error.message : String(error)
691
- });
692
- throw error;
693
- } finally {
694
- span.end();
695
- }
696
- }
697
- );
698
- };
627
+ if (!options.capturePersistEvents) return originalMethod;
628
+ return function instrumentedOnPersist(key, value) {
629
+ const tracer = getTracer$1();
630
+ const actorClassName = actorClass.name || "Actor";
631
+ const spanName = options.spanNameFormatter ? options.spanNameFormatter(actorInstance.name || "", "persist") : `Actor ${actorInstance.name || actorClassName}: persist ${key}`;
632
+ tracer.startActiveSpan(spanName, {
633
+ kind: SpanKind.INTERNAL,
634
+ attributes: {
635
+ ...createActorAttributes(actorInstance, actorClass, "persist"),
636
+ "actor.persist.key": key,
637
+ "actor.persist.value_type": typeof value
638
+ }
639
+ }, (span) => {
640
+ try {
641
+ originalMethod.call(actorInstance, key, value);
642
+ span.setStatus({ code: SpanStatusCode.OK });
643
+ } catch (error) {
644
+ span.recordException(error);
645
+ span.setStatus({
646
+ code: SpanStatusCode.ERROR,
647
+ message: error instanceof Error ? error.message : String(error)
648
+ });
649
+ throw error;
650
+ } finally {
651
+ span.end();
652
+ }
653
+ });
654
+ };
699
655
  }
656
+ /**
657
+ * Instrument WebSocket lifecycle methods
658
+ */
700
659
  function instrumentWebSocketConnect(originalMethod, actorInstance, actorClass, options) {
701
- return function instrumentedWebSocketConnect(ws, request) {
702
- const tracer = getTracer4();
703
- const actorClassName = actorClass.name || "Actor";
704
- const spanName = options.spanNameFormatter ? options.spanNameFormatter(actorInstance.name || "", "websocket.connect") : defaultSpanNameFormatter(actorInstance.name || "", actorClassName, "websocket.connect");
705
- tracer.startActiveSpan(
706
- spanName,
707
- {
708
- kind: SpanKind.SERVER,
709
- attributes: {
710
- ...createActorAttributes(actorInstance, actorClass, "websocket.connect"),
711
- "url.full": request.url
712
- }
713
- },
714
- (span) => {
715
- try {
716
- originalMethod.call(actorInstance, ws, request);
717
- span.setStatus({ code: SpanStatusCode.OK });
718
- } catch (error) {
719
- span.recordException(error);
720
- span.setStatus({
721
- code: SpanStatusCode.ERROR,
722
- message: error instanceof Error ? error.message : String(error)
723
- });
724
- throw error;
725
- } finally {
726
- span.end();
727
- }
728
- }
729
- );
730
- };
660
+ return function instrumentedWebSocketConnect(ws, request) {
661
+ const tracer = getTracer$1();
662
+ const actorClassName = actorClass.name || "Actor";
663
+ const spanName = options.spanNameFormatter ? options.spanNameFormatter(actorInstance.name || "", "websocket.connect") : defaultSpanNameFormatter(actorInstance.name || "", actorClassName, "websocket.connect");
664
+ tracer.startActiveSpan(spanName, {
665
+ kind: SpanKind.SERVER,
666
+ attributes: {
667
+ ...createActorAttributes(actorInstance, actorClass, "websocket.connect"),
668
+ "url.full": request.url
669
+ }
670
+ }, (span) => {
671
+ try {
672
+ originalMethod.call(actorInstance, ws, request);
673
+ span.setStatus({ code: SpanStatusCode.OK });
674
+ } catch (error) {
675
+ span.recordException(error);
676
+ span.setStatus({
677
+ code: SpanStatusCode.ERROR,
678
+ message: error instanceof Error ? error.message : String(error)
679
+ });
680
+ throw error;
681
+ } finally {
682
+ span.end();
683
+ }
684
+ });
685
+ };
731
686
  }
732
687
  function instrumentWebSocketMessage(originalMethod, actorInstance, actorClass, options) {
733
- return function instrumentedWebSocketMessage(ws, message) {
734
- const tracer = getTracer4();
735
- const actorClassName = actorClass.name || "Actor";
736
- const spanName = options.spanNameFormatter ? options.spanNameFormatter(actorInstance.name || "", "websocket.message") : defaultSpanNameFormatter(actorInstance.name || "", actorClassName, "websocket.message");
737
- tracer.startActiveSpan(
738
- spanName,
739
- {
740
- kind: SpanKind.SERVER,
741
- attributes: {
742
- ...createActorAttributes(actorInstance, actorClass, "websocket.message"),
743
- "websocket.message.type": typeof message,
744
- "websocket.message.size": typeof message === "string" ? message.length : message instanceof ArrayBuffer ? message.byteLength : 0
745
- }
746
- },
747
- (span) => {
748
- try {
749
- originalMethod.call(actorInstance, ws, message);
750
- span.setStatus({ code: SpanStatusCode.OK });
751
- } catch (error) {
752
- span.recordException(error);
753
- span.setStatus({
754
- code: SpanStatusCode.ERROR,
755
- message: error instanceof Error ? error.message : String(error)
756
- });
757
- throw error;
758
- } finally {
759
- span.end();
760
- }
761
- }
762
- );
763
- };
688
+ return function instrumentedWebSocketMessage(ws, message) {
689
+ const tracer = getTracer$1();
690
+ const actorClassName = actorClass.name || "Actor";
691
+ const spanName = options.spanNameFormatter ? options.spanNameFormatter(actorInstance.name || "", "websocket.message") : defaultSpanNameFormatter(actorInstance.name || "", actorClassName, "websocket.message");
692
+ tracer.startActiveSpan(spanName, {
693
+ kind: SpanKind.SERVER,
694
+ attributes: {
695
+ ...createActorAttributes(actorInstance, actorClass, "websocket.message"),
696
+ "websocket.message.type": typeof message,
697
+ "websocket.message.size": typeof message === "string" ? message.length : message instanceof ArrayBuffer ? message.byteLength : 0
698
+ }
699
+ }, (span) => {
700
+ try {
701
+ originalMethod.call(actorInstance, ws, message);
702
+ span.setStatus({ code: SpanStatusCode.OK });
703
+ } catch (error) {
704
+ span.recordException(error);
705
+ span.setStatus({
706
+ code: SpanStatusCode.ERROR,
707
+ message: error instanceof Error ? error.message : String(error)
708
+ });
709
+ throw error;
710
+ } finally {
711
+ span.end();
712
+ }
713
+ });
714
+ };
764
715
  }
765
716
  function instrumentWebSocketDisconnect(originalMethod, actorInstance, actorClass, options) {
766
- return function instrumentedWebSocketDisconnect(ws) {
767
- const tracer = getTracer4();
768
- const actorClassName = actorClass.name || "Actor";
769
- const spanName = options.spanNameFormatter ? options.spanNameFormatter(actorInstance.name || "", "websocket.disconnect") : defaultSpanNameFormatter(actorInstance.name || "", actorClassName, "websocket.disconnect");
770
- tracer.startActiveSpan(
771
- spanName,
772
- {
773
- kind: SpanKind.SERVER,
774
- attributes: createActorAttributes(actorInstance, actorClass, "websocket.disconnect")
775
- },
776
- (span) => {
777
- try {
778
- originalMethod.call(actorInstance, ws);
779
- span.setStatus({ code: SpanStatusCode.OK });
780
- } catch (error) {
781
- span.recordException(error);
782
- span.setStatus({
783
- code: SpanStatusCode.ERROR,
784
- message: error instanceof Error ? error.message : String(error)
785
- });
786
- throw error;
787
- } finally {
788
- span.end();
789
- }
790
- }
791
- );
792
- };
717
+ return function instrumentedWebSocketDisconnect(ws) {
718
+ const tracer = getTracer$1();
719
+ const actorClassName = actorClass.name || "Actor";
720
+ const spanName = options.spanNameFormatter ? options.spanNameFormatter(actorInstance.name || "", "websocket.disconnect") : defaultSpanNameFormatter(actorInstance.name || "", actorClassName, "websocket.disconnect");
721
+ tracer.startActiveSpan(spanName, {
722
+ kind: SpanKind.SERVER,
723
+ attributes: createActorAttributes(actorInstance, actorClass, "websocket.disconnect")
724
+ }, (span) => {
725
+ try {
726
+ originalMethod.call(actorInstance, ws);
727
+ span.setStatus({ code: SpanStatusCode.OK });
728
+ } catch (error) {
729
+ span.recordException(error);
730
+ span.setStatus({
731
+ code: SpanStatusCode.ERROR,
732
+ message: error instanceof Error ? error.message : String(error)
733
+ });
734
+ throw error;
735
+ } finally {
736
+ span.end();
737
+ }
738
+ });
739
+ };
793
740
  }
741
+ /**
742
+ * Instrument an Actor instance by wrapping all lifecycle methods
743
+ */
794
744
  function instrumentActorInstance(actorInstance, _state, _env, actorClass, options) {
795
- const instanceHandler = {
796
- get(target, prop) {
797
- const value = Reflect.get(target, prop);
798
- if (prop === "onInit" && typeof value === "function") {
799
- return instrumentOnInit(value.bind(target), target, actorClass, options);
800
- }
801
- if (prop === "onRequest" && typeof value === "function") {
802
- return instrumentOnRequest(value.bind(target), target, actorClass, options);
803
- }
804
- if (prop === "onAlarm" && typeof value === "function") {
805
- return instrumentOnAlarm(value.bind(target), target, actorClass, options);
806
- }
807
- if (prop === "onPersist" && typeof value === "function") {
808
- return instrumentOnPersist(value.bind(target), target, actorClass, options);
809
- }
810
- if (prop === "onWebSocketConnect" && typeof value === "function") {
811
- return instrumentWebSocketConnect(value.bind(target), target, actorClass, options);
812
- }
813
- if (prop === "onWebSocketMessage" && typeof value === "function") {
814
- return instrumentWebSocketMessage(value.bind(target), target, actorClass, options);
815
- }
816
- if (prop === "onWebSocketDisconnect" && typeof value === "function") {
817
- return instrumentWebSocketDisconnect(value.bind(target), target, actorClass, options);
818
- }
819
- if (prop === "storage" && value && options.instrumentStorage !== false) {
820
- return instrumentActorStorage(value, target, actorClass);
821
- }
822
- if (prop === "alarms" && value && options.instrumentAlarms !== false) {
823
- return instrumentActorAlarms(value, target, actorClass);
824
- }
825
- if (prop === "sockets" && value && options.instrumentSockets !== false) {
826
- return instrumentActorSockets(value, target, actorClass);
827
- }
828
- if (typeof value === "function") {
829
- return value.bind(target);
830
- }
831
- return value;
832
- }
833
- };
834
- return wrap(actorInstance, instanceHandler);
745
+ return wrap(actorInstance, { get(target, prop) {
746
+ const value = Reflect.get(target, prop);
747
+ if (prop === "onInit" && typeof value === "function") return instrumentOnInit(value.bind(target), target, actorClass, options);
748
+ if (prop === "onRequest" && typeof value === "function") return instrumentOnRequest(value.bind(target), target, actorClass, options);
749
+ if (prop === "onAlarm" && typeof value === "function") return instrumentOnAlarm(value.bind(target), target, actorClass, options);
750
+ if (prop === "onPersist" && typeof value === "function") return instrumentOnPersist(value.bind(target), target, actorClass, options);
751
+ if (prop === "onWebSocketConnect" && typeof value === "function") return instrumentWebSocketConnect(value.bind(target), target, actorClass, options);
752
+ if (prop === "onWebSocketMessage" && typeof value === "function") return instrumentWebSocketMessage(value.bind(target), target, actorClass, options);
753
+ if (prop === "onWebSocketDisconnect" && typeof value === "function") return instrumentWebSocketDisconnect(value.bind(target), target, actorClass, options);
754
+ if (prop === "storage" && value && options.instrumentStorage !== false) return instrumentActorStorage(value, target, actorClass);
755
+ if (prop === "alarms" && value && options.instrumentAlarms !== false) return instrumentActorAlarms(value, target, actorClass);
756
+ if (prop === "sockets" && value && options.instrumentSockets !== false) return instrumentActorSockets(value, target, actorClass);
757
+ if (typeof value === "function") return value.bind(target);
758
+ return value;
759
+ } });
835
760
  }
761
+ /**
762
+ * Instrument an Actor class for comprehensive OpenTelemetry tracing
763
+ *
764
+ * This wraps the Actor class to automatically trace all lifecycle methods:
765
+ * - onInit: Actor initialization
766
+ * - onRequest: HTTP request handling
767
+ * - onAlarm: Alarm triggers
768
+ * - onPersist: Property persistence events
769
+ * - WebSocket methods: Connection, message, disconnect
770
+ *
771
+ * It also optionally instruments:
772
+ * - actor.storage: SQL queries and storage operations
773
+ * - actor.alarms: Alarm scheduling operations
774
+ * - actor.sockets: WebSocket operations
775
+ *
776
+ * @example
777
+ * ```typescript
778
+ * import { Actor } from '@cloudflare/actors'
779
+ * import { instrumentActor } from 'autotel-cloudflare/actors'
780
+ *
781
+ * class Counter extends Actor<Env> {
782
+ * protected onInit() {
783
+ * console.log('Counter initialized')
784
+ * }
785
+ *
786
+ * protected onRequest(request: Request) {
787
+ * return new Response('count: 42')
788
+ * }
789
+ * }
790
+ *
791
+ * // Wrap the class
792
+ * export const InstrumentedCounter = instrumentActor(Counter, (env: Env) => ({
793
+ * service: { name: 'counter-actor' },
794
+ * exporter: { url: env.OTLP_ENDPOINT },
795
+ * actors: {
796
+ * instrumentStorage: true,
797
+ * capturePersistEvents: true
798
+ * }
799
+ * }))
800
+ * ```
801
+ *
802
+ * @param actorClass - The Actor class to instrument
803
+ * @param config - Configuration (static object or function)
804
+ * @returns Instrumented Actor class
805
+ */
836
806
  function instrumentActor(actorClass, config) {
837
- const initialiser = createInitialiser(config);
838
- const defaultOptions = {
839
- instrumentStorage: true,
840
- instrumentAlarms: true,
841
- instrumentSockets: true,
842
- capturePersistEvents: true
843
- };
844
- const classHandler = {
845
- construct(target, [state, env]) {
846
- const resolvedConfig = typeof config === "function" ? config(env, { id: state.id.toString(), name: state.id.name }) : config;
847
- const actorOptions = resolvedConfig && typeof resolvedConfig === "object" && "actors" in resolvedConfig ? resolvedConfig.actors : void 0;
848
- const options = {
849
- ...defaultOptions,
850
- ...actorOptions
851
- };
852
- const trigger = {
853
- id: state.id.toString(),
854
- name: state.id.name
855
- };
856
- const telemetryConfig = initialiser(env, trigger);
857
- const context$1 = setConfig(telemetryConfig);
858
- const actorInstance = context.with(context$1, () => {
859
- return new target(state, env);
860
- });
861
- return instrumentActorInstance(actorInstance, state, env, actorClass, options);
862
- }
863
- };
864
- return wrap(actorClass, classHandler);
807
+ const initialiser = createInitialiser(config);
808
+ const defaultOptions = {
809
+ instrumentStorage: true,
810
+ instrumentAlarms: true,
811
+ instrumentSockets: true,
812
+ capturePersistEvents: true
813
+ };
814
+ return wrap(actorClass, { construct(target, [state, env]) {
815
+ const resolvedConfig = typeof config === "function" ? config(env, {
816
+ id: state.id.toString(),
817
+ name: state.id.name
818
+ }) : config;
819
+ const actorOptions = resolvedConfig && typeof resolvedConfig === "object" && "actors" in resolvedConfig ? resolvedConfig.actors : void 0;
820
+ const options = {
821
+ ...defaultOptions,
822
+ ...actorOptions
823
+ };
824
+ const context$1 = setConfig(initialiser(env, {
825
+ id: state.id.toString(),
826
+ name: state.id.name
827
+ }));
828
+ return instrumentActorInstance(context.with(context$1, () => {
829
+ return new target(state, env);
830
+ }), state, env, actorClass, options);
831
+ } });
865
832
  }
866
- function getTracer5() {
867
- return trace.getTracer("autotel-cloudflare-actors");
833
+
834
+ //#endregion
835
+ //#region src/actors/traced-handler.ts
836
+ /**
837
+ * Traced handler wrapper for @cloudflare/actors
838
+ *
839
+ * Wraps the Actors handler() to provide:
840
+ * - Root span for the entire request lifecycle
841
+ * - Actor name extraction and correlation
842
+ * - Request routing tracing
843
+ */
844
+ /**
845
+ * Get the tracer instance
846
+ */
847
+ function getTracer() {
848
+ return trace.getTracer("autotel-cloudflare-actors");
868
849
  }
850
+ /**
851
+ * Create a traced handler that combines Actor instrumentation with request tracing
852
+ *
853
+ * This is an all-in-one wrapper that:
854
+ * 1. Initializes telemetry for the Worker
855
+ * 2. Creates a root span for each incoming request
856
+ * 3. Extracts the Actor name using `nameFromRequest`
857
+ * 4. Instruments the Actor class with lifecycle tracing
858
+ * 5. Routes the request to the instrumented Actor
859
+ *
860
+ * @example
861
+ * ```typescript
862
+ * import { Actor } from '@cloudflare/actors'
863
+ * import { tracedHandler } from 'autotel-cloudflare/actors'
864
+ *
865
+ * class MyActor extends Actor<Env> {
866
+ * protected onRequest(request: Request) {
867
+ * return new Response('Hello!')
868
+ * }
869
+ * }
870
+ *
871
+ * // Export the Actor class and use tracedHandler
872
+ * export { MyActor }
873
+ * export default tracedHandler(MyActor, (env) => ({
874
+ * service: { name: 'my-actor-service' },
875
+ * exporter: { url: env.OTLP_ENDPOINT }
876
+ * }))
877
+ * ```
878
+ *
879
+ * @param actorClass - The Actor class to handle requests
880
+ * @param config - Configuration (static object or function)
881
+ * @returns A Worker handler with full tracing
882
+ */
869
883
  function tracedHandler(actorClass, config) {
870
- const initialiser = createInitialiser(config);
871
- return {
872
- async fetch(request, env, _ctx) {
873
- const telemetryConfig = initialiser(env, { type: "http" });
874
- const configContext = setConfig(telemetryConfig);
875
- const parentContext = propagation.extract(configContext, request.headers);
876
- const tracer = getTracer5();
877
- const url = new URL(request.url);
878
- const actorClassName = actorClass.name || "Actor";
879
- let actorName;
880
- try {
881
- if (actorClass.nameFromRequest) {
882
- actorName = await actorClass.nameFromRequest(request);
883
- }
884
- } catch {
885
- actorName = void 0;
886
- }
887
- const spanName = `${actorClassName} handler: ${request.method} ${url.pathname}`;
888
- return tracer.startActiveSpan(
889
- spanName,
890
- {
891
- kind: SpanKind.SERVER,
892
- attributes: {
893
- "http.request.method": request.method,
894
- "url.full": request.url,
895
- "url.path": url.pathname,
896
- "url.query": url.search,
897
- "actor.class": actorClassName,
898
- ...actorName && { "actor.name": actorName },
899
- "faas.trigger": "http"
900
- }
901
- },
902
- parentContext,
903
- async (span) => {
904
- try {
905
- const envObj = env;
906
- const bindingName = Object.keys(envObj).find((key) => {
907
- const binding = env.__DURABLE_OBJECT_BINDINGS;
908
- return key === actorClassName || binding?.[key]?.class_name === actorClassName;
909
- });
910
- if (!bindingName) {
911
- span.setStatus({
912
- code: SpanStatusCode.ERROR,
913
- message: `No Durable Object binding found for ${actorClassName}`
914
- });
915
- return Response.json(
916
- {
917
- error: "Configuration Error",
918
- message: `No Durable Object binding found for actor class ${actorClassName}`
919
- },
920
- { status: 500, headers: { "Content-Type": "application/json" } }
921
- );
922
- }
923
- const namespace = envObj[bindingName];
924
- const idString = actorName || "default";
925
- const locationHint = actorClass.configuration?.(request)?.locationHint;
926
- const stub = namespace.getByName(idString, { locationHint });
927
- if ("setName" in stub && typeof stub.setName === "function") {
928
- stub.setName(idString);
929
- }
930
- const headers = new Headers(request.headers);
931
- propagation.inject(context.active(), headers);
932
- const tracedRequest = new Request(request.url, {
933
- method: request.method,
934
- headers,
935
- body: request.body,
936
- redirect: request.redirect
937
- });
938
- const response = await stub.fetch(tracedRequest);
939
- span.setAttributes({
940
- "http.response.status_code": response.status,
941
- "actor.name": idString
942
- });
943
- if (response.ok) {
944
- span.setStatus({ code: SpanStatusCode.OK });
945
- } else {
946
- span.setStatus({ code: SpanStatusCode.ERROR });
947
- }
948
- return response;
949
- } catch (error) {
950
- span.recordException(error);
951
- span.setStatus({
952
- code: SpanStatusCode.ERROR,
953
- message: error instanceof Error ? error.message : String(error)
954
- });
955
- return Response.json(
956
- {
957
- error: "Internal Server Error",
958
- message: error instanceof Error ? error.message : "Unknown error"
959
- },
960
- { status: 500, headers: { "Content-Type": "application/json" } }
961
- );
962
- } finally {
963
- span.end();
964
- }
965
- }
966
- );
967
- }
968
- };
884
+ const initialiser = createInitialiser(config);
885
+ return { async fetch(request, env, _ctx) {
886
+ const configContext = setConfig(initialiser(env, { type: "http" }));
887
+ const parentContext = propagation.extract(configContext, request.headers);
888
+ const tracer = getTracer();
889
+ const url = new URL(request.url);
890
+ const actorClassName = actorClass.name || "Actor";
891
+ let actorName;
892
+ try {
893
+ if (actorClass.nameFromRequest) actorName = await actorClass.nameFromRequest(request);
894
+ } catch {
895
+ actorName = void 0;
896
+ }
897
+ const spanName = `${actorClassName} handler: ${request.method} ${url.pathname}`;
898
+ return tracer.startActiveSpan(spanName, {
899
+ kind: SpanKind.SERVER,
900
+ attributes: {
901
+ "http.request.method": request.method,
902
+ "url.full": request.url,
903
+ "url.path": url.pathname,
904
+ "url.query": url.search,
905
+ "actor.class": actorClassName,
906
+ ...actorName && { "actor.name": actorName },
907
+ "faas.trigger": "http"
908
+ }
909
+ }, parentContext, async (span) => {
910
+ try {
911
+ const envObj = env;
912
+ const bindingName = Object.keys(envObj).find((key) => {
913
+ const binding = env.__DURABLE_OBJECT_BINDINGS;
914
+ return key === actorClassName || binding?.[key]?.class_name === actorClassName;
915
+ });
916
+ if (!bindingName) {
917
+ span.setStatus({
918
+ code: SpanStatusCode.ERROR,
919
+ message: `No Durable Object binding found for ${actorClassName}`
920
+ });
921
+ return Response.json({
922
+ error: "Configuration Error",
923
+ message: `No Durable Object binding found for actor class ${actorClassName}`
924
+ }, {
925
+ status: 500,
926
+ headers: { "Content-Type": "application/json" }
927
+ });
928
+ }
929
+ const namespace = envObj[bindingName];
930
+ const idString = actorName || "default";
931
+ const locationHint = actorClass.configuration?.(request)?.locationHint;
932
+ const stub = namespace.getByName(idString, { locationHint });
933
+ if ("setName" in stub && typeof stub.setName === "function") stub.setName(idString);
934
+ const headers = new Headers(request.headers);
935
+ propagation.inject(context.active(), headers);
936
+ const tracedRequest = new Request(request.url, {
937
+ method: request.method,
938
+ headers,
939
+ body: request.body,
940
+ redirect: request.redirect
941
+ });
942
+ const response = await stub.fetch(tracedRequest);
943
+ span.setAttributes({
944
+ "http.response.status_code": response.status,
945
+ "actor.name": idString
946
+ });
947
+ if (response.ok) span.setStatus({ code: SpanStatusCode.OK });
948
+ else span.setStatus({ code: SpanStatusCode.ERROR });
949
+ return response;
950
+ } catch (error) {
951
+ span.recordException(error);
952
+ span.setStatus({
953
+ code: SpanStatusCode.ERROR,
954
+ message: error instanceof Error ? error.message : String(error)
955
+ });
956
+ return Response.json({
957
+ error: "Internal Server Error",
958
+ message: error instanceof Error ? error.message : "Unknown error"
959
+ }, {
960
+ status: 500,
961
+ headers: { "Content-Type": "application/json" }
962
+ });
963
+ } finally {
964
+ span.end();
965
+ }
966
+ });
967
+ } };
969
968
  }
969
+ /**
970
+ * Alternative: Create a handler wrapper that uses the existing @cloudflare/actors handler
971
+ *
972
+ * This is useful if you want to use the original handler() but add tracing around it.
973
+ *
974
+ * @example
975
+ * ```typescript
976
+ * import { Actor, handler } from '@cloudflare/actors'
977
+ * import { wrapHandler } from 'autotel-cloudflare/actors'
978
+ *
979
+ * class MyActor extends Actor<Env> {}
980
+ *
981
+ * export { MyActor }
982
+ * export default wrapHandler(handler(MyActor), (env) => ({
983
+ * service: { name: 'my-service' }
984
+ * }))
985
+ * ```
986
+ */
970
987
  function wrapHandler(originalHandler, config) {
971
- const initialiser = createInitialiser(config);
972
- return {
973
- async fetch(request, env, ctx) {
974
- const telemetryConfig = initialiser(env, { type: "http" });
975
- const configContext = setConfig(telemetryConfig);
976
- const parentContext = propagation.extract(configContext, request.headers);
977
- const tracer = getTracer5();
978
- const url = new URL(request.url);
979
- return tracer.startActiveSpan(
980
- `Worker: ${request.method} ${url.pathname}`,
981
- {
982
- kind: SpanKind.SERVER,
983
- attributes: {
984
- "http.request.method": request.method,
985
- "url.full": request.url,
986
- "url.path": url.pathname,
987
- "url.query": url.search,
988
- "faas.trigger": "http"
989
- }
990
- },
991
- parentContext,
992
- async (span) => {
993
- try {
994
- const headers = new Headers(request.headers);
995
- propagation.inject(context.active(), headers);
996
- const tracedRequest = new Request(request.url, {
997
- method: request.method,
998
- headers,
999
- body: request.body,
1000
- redirect: request.redirect
1001
- });
1002
- const response = await originalHandler.fetch(tracedRequest, env, ctx);
1003
- span.setAttributes({
1004
- "http.response.status_code": response.status
1005
- });
1006
- if (response.ok) {
1007
- span.setStatus({ code: SpanStatusCode.OK });
1008
- } else {
1009
- span.setStatus({ code: SpanStatusCode.ERROR });
1010
- }
1011
- return response;
1012
- } catch (error) {
1013
- span.recordException(error);
1014
- span.setStatus({
1015
- code: SpanStatusCode.ERROR,
1016
- message: error instanceof Error ? error.message : String(error)
1017
- });
1018
- throw error;
1019
- } finally {
1020
- span.end();
1021
- }
1022
- }
1023
- );
1024
- }
1025
- };
988
+ const initialiser = createInitialiser(config);
989
+ return { async fetch(request, env, ctx) {
990
+ const configContext = setConfig(initialiser(env, { type: "http" }));
991
+ const parentContext = propagation.extract(configContext, request.headers);
992
+ const tracer = getTracer();
993
+ const url = new URL(request.url);
994
+ return tracer.startActiveSpan(`Worker: ${request.method} ${url.pathname}`, {
995
+ kind: SpanKind.SERVER,
996
+ attributes: {
997
+ "http.request.method": request.method,
998
+ "url.full": request.url,
999
+ "url.path": url.pathname,
1000
+ "url.query": url.search,
1001
+ "faas.trigger": "http"
1002
+ }
1003
+ }, parentContext, async (span) => {
1004
+ try {
1005
+ const headers = new Headers(request.headers);
1006
+ propagation.inject(context.active(), headers);
1007
+ const tracedRequest = new Request(request.url, {
1008
+ method: request.method,
1009
+ headers,
1010
+ body: request.body,
1011
+ redirect: request.redirect
1012
+ });
1013
+ const response = await originalHandler.fetch(tracedRequest, env, ctx);
1014
+ span.setAttributes({ "http.response.status_code": response.status });
1015
+ if (response.ok) span.setStatus({ code: SpanStatusCode.OK });
1016
+ else span.setStatus({ code: SpanStatusCode.ERROR });
1017
+ return response;
1018
+ } catch (error) {
1019
+ span.recordException(error);
1020
+ span.setStatus({
1021
+ code: SpanStatusCode.ERROR,
1022
+ message: error instanceof Error ? error.message : String(error)
1023
+ });
1024
+ throw error;
1025
+ } finally {
1026
+ span.end();
1027
+ }
1028
+ });
1029
+ } };
1026
1030
  }
1027
1031
 
1032
+ //#endregion
1028
1033
  export { instrumentActor, instrumentActorAlarms, instrumentActorSockets, instrumentActorStorage, tracedHandler, wrapHandler };
1029
- //# sourceMappingURL=actors.js.map
1030
1034
  //# sourceMappingURL=actors.js.map