pi-cloudflare-agent-tracing 0.2.0 → 0.4.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/README.md +68 -41
- package/extensions/cloudflare-agent-tracing.ts +16 -45
- package/package.json +47 -29
- package/runtime/README.md +18 -11
- package/runtime/cloudflare-telemetry-context.d.ts +58 -0
- package/runtime/cloudflare-telemetry-context.js +139 -0
- package/runtime/cloudflare-telemetry-context.test.mjs +5 -2
- package/skills/cloudflare-agent-tracing/SKILL.md +35 -33
- package/skills/cloudflare-agent-tracing/references/report.example.json +4 -5
- package/skills/cloudflare-agent-tracing/scripts/project-profile.mjs +58 -7
- package/skills/cloudflare-agent-tracing/scripts/project-profile.test.mjs +27 -0
- package/skills/cloudflare-agent-tracing/scripts/render-report.mjs +143 -48
- package/skills/cloudflare-agent-tracing/scripts/render-report.test.mjs +49 -35
- package/runtime/cloudflare-telemetry-context.ts +0 -225
package/README.md
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
# Pi Cloudflare Agent Tracing
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
A Pi package and required Worker runtime adapter for content-safe tracing of Pi Durable applications running on Cloudflare.
|
|
4
4
|
|
|
5
|
-
It adds:
|
|
5
|
+
It adds one command:
|
|
6
6
|
|
|
7
7
|
```text
|
|
8
|
-
/
|
|
9
|
-
/agent-health
|
|
10
|
-
/agent-cost
|
|
11
|
-
/agent-eval
|
|
12
|
-
/agent-incident
|
|
8
|
+
/pi-trace [target] [--scope turn|thread|service] [--html]
|
|
13
9
|
```
|
|
14
10
|
|
|
11
|
+
One report combines the execution path, reliability, latency, usage, cost, fulfillment, recovery, anomalies, and evidence-linked improvements.
|
|
12
|
+
|
|
15
13
|
The package registers Cloudflare's official MCP server, applies an evidence-first investigation skill, and blocks `cloudflare_execute` unless every statically visible API request is an explicit `GET`.
|
|
16
14
|
|
|
17
15
|
## What it does
|
|
@@ -23,13 +21,14 @@ The package registers Cloudflare's official MCP server, applies an evidence-firs
|
|
|
23
21
|
- Queries deployments, versions, bindings, logs, traces, and bounded metrics.
|
|
24
22
|
- Evaluates deterministic fulfillment from normalized, content-free evidence.
|
|
25
23
|
- Summarizes Pi Durable-shaped context exports without transcript or tool content.
|
|
26
|
-
-
|
|
24
|
+
- Emits `pi.durable.*` and `pi.ai.*` spans through Cloudflare Workers custom spans.
|
|
25
|
+
- Requires the runtime adapter on every production `Harness.open()` before investigations run.
|
|
27
26
|
|
|
28
27
|
It does **not** deploy, repair state, mutate Cloudflare resources, or create diagnostic endpoints.
|
|
29
28
|
|
|
30
29
|
## Install
|
|
31
30
|
|
|
32
|
-
### Install
|
|
31
|
+
### 1. Install the Pi investigation package
|
|
33
32
|
|
|
34
33
|
```bash
|
|
35
34
|
pi install npm:pi-cloudflare-agent-tracing
|
|
@@ -37,6 +36,44 @@ pi install npm:pi-cloudflare-agent-tracing
|
|
|
37
36
|
|
|
38
37
|
The `pi-package` keyword makes the published package eligible for discovery in the [Pi package gallery](https://pi.dev/packages).
|
|
39
38
|
|
|
39
|
+
### 2. Install the required Worker runtime dependency
|
|
40
|
+
|
|
41
|
+
From every Pi Durable Worker project that will be investigated:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pnpm add pi-cloudflare-agent-tracing
|
|
45
|
+
# or: npm install pi-cloudflare-agent-tracing
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Enable Workers observability and tracing:
|
|
49
|
+
|
|
50
|
+
```jsonc
|
|
51
|
+
{
|
|
52
|
+
"observability": {
|
|
53
|
+
"enabled": true,
|
|
54
|
+
"traces": { "enabled": true },
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Wire the package into every production `Harness.open()`:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { tracing } from "cloudflare:workers";
|
|
63
|
+
import { createCloudflareTelemetryContext } from "pi-cloudflare-agent-tracing/cloudflare";
|
|
64
|
+
|
|
65
|
+
const harness = await Harness.open(
|
|
66
|
+
storage,
|
|
67
|
+
{
|
|
68
|
+
...harnessOptions,
|
|
69
|
+
telemetry: createCloudflareTelemetryContext(tracing),
|
|
70
|
+
},
|
|
71
|
+
context,
|
|
72
|
+
);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The profiler requires all three conditions—the dependency, Workers tracing, and `Harness.open()` wiring. Investigation commands stop with setup guidance when any condition is missing because platform traces alone do not provide Pi execution telemetry.
|
|
76
|
+
|
|
40
77
|
### Try from a local checkout
|
|
41
78
|
|
|
42
79
|
```bash
|
|
@@ -62,7 +99,7 @@ pi install /absolute/path/to/pi-cloudflare-agent-tracing
|
|
|
62
99
|
The same package can be installed from Git:
|
|
63
100
|
|
|
64
101
|
```bash
|
|
65
|
-
pi install git:github.com/oskarcode/pi-cloudflare-agent-tracing@v0.
|
|
102
|
+
pi install git:github.com/oskarcode/pi-cloudflare-agent-tracing@v0.4.0
|
|
66
103
|
```
|
|
67
104
|
|
|
68
105
|
Use a pinned release tag or commit for reproducible investigations.
|
|
@@ -85,22 +122,15 @@ If Cloudflare authentication is required:
|
|
|
85
122
|
Then run, for example:
|
|
86
123
|
|
|
87
124
|
```text
|
|
88
|
-
/
|
|
89
|
-
/
|
|
90
|
-
/
|
|
91
|
-
/
|
|
92
|
-
/
|
|
125
|
+
/pi-trace
|
|
126
|
+
/pi-trace my-worker --since 30m
|
|
127
|
+
/pi-trace <ray-or-trace-id>
|
|
128
|
+
/pi-trace <submission-or-task-id>
|
|
129
|
+
/pi-trace <conversation-id>
|
|
130
|
+
/pi-trace my-worker --since 6h --html
|
|
93
131
|
```
|
|
94
132
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
```text
|
|
98
|
-
/agent-health my-worker last 6h --html
|
|
99
|
-
/agent-trace ray-id --html
|
|
100
|
-
/agent-cost my-worker last 24h --html
|
|
101
|
-
/agent-eval submission 42 --html
|
|
102
|
-
/agent-incident my-worker last 30m --html
|
|
103
|
-
```
|
|
133
|
+
The target automatically selects the narrowest useful scope: request, Ray, trace, submission, and task identifiers produce a `turn` report; a conversation produces a `thread`; a Worker or deployment plus time range produces a `service` report. Use `--scope` only to override that inference.
|
|
104
134
|
|
|
105
135
|
Each saved report produces matching `.md`, content-free `.json`, and standalone `.html` files under the target project's `.pi/agent-tracing-reports/`. The HTML is printable, responsive, self-contained, uses no remote assets, and renders the same bounded evidence as the Markdown and JSON versions.
|
|
106
136
|
|
|
@@ -109,7 +139,7 @@ Each saved report produces matching `.md`, content-free `.json`, and standalone
|
|
|
109
139
|
```text
|
|
110
140
|
slash command
|
|
111
141
|
→ extension dispatches /skill:cloudflare-agent-tracing
|
|
112
|
-
→ skill
|
|
142
|
+
→ skill verifies required Pi runtime instrumentation
|
|
113
143
|
→ Cloudflare MCP performs bounded GET queries
|
|
114
144
|
→ optional authorized diagnostics provide durable evidence
|
|
115
145
|
→ evaluator produces a content-free verdict
|
|
@@ -120,7 +150,7 @@ The extension and skill have different responsibilities:
|
|
|
120
150
|
- `extensions/cloudflare-agent-tracing.ts` owns executable Pi wiring, command registration, MCP registration, queue behavior, and the read-only gate.
|
|
121
151
|
- `skills/cloudflare-agent-tracing/SKILL.md` owns investigation policy, evidence order, privacy rules, and report structure.
|
|
122
152
|
- `skills/cloudflare-agent-tracing/scripts/` contains dependency-free local evidence utilities.
|
|
123
|
-
- `runtime/` contains
|
|
153
|
+
- `runtime/` contains the required Worker adapter that forwards Pi telemetry to Cloudflare custom spans.
|
|
124
154
|
|
|
125
155
|
## Project discovery
|
|
126
156
|
|
|
@@ -138,9 +168,9 @@ node skills/cloudflare-agent-tracing/scripts/project-profile.mjs \
|
|
|
138
168
|
--config apps/agent/wrangler.jsonc
|
|
139
169
|
```
|
|
140
170
|
|
|
141
|
-
Source discovery is bounded and excludes build output, dependencies, and hidden directories. It searches common TypeScript and JavaScript files for `Harness.open()` and reports whether
|
|
171
|
+
Source discovery is bounded and excludes build output, dependencies, and hidden directories. It searches common TypeScript and JavaScript files for every production `Harness.open()` and reports whether each call passes `telemetry:`. `instrumentation.ready` is true only when the package dependency and Wrangler tracing settings are also present.
|
|
142
172
|
|
|
143
|
-
The profiler is heuristic evidence
|
|
173
|
+
The profiler is heuristic setup evidence. A sampled production span remains the runtime proof.
|
|
144
174
|
|
|
145
175
|
## Fulfillment evaluation
|
|
146
176
|
|
|
@@ -160,7 +190,7 @@ Deterministic evidence checks submission settlement, final answer presence, requ
|
|
|
160
190
|
|
|
161
191
|
## Human-review HTML reports
|
|
162
192
|
|
|
163
|
-
|
|
193
|
+
Every investigation uses one normalized Pi trace schema. See:
|
|
164
194
|
|
|
165
195
|
```text
|
|
166
196
|
skills/cloudflare-agent-tracing/references/report.example.json
|
|
@@ -174,7 +204,7 @@ node skills/cloudflare-agent-tracing/scripts/render-report.mjs \
|
|
|
174
204
|
--out path/to/report.html
|
|
175
205
|
```
|
|
176
206
|
|
|
177
|
-
If `--out` is omitted, the renderer writes beside the JSON input with an `.html` extension. The renderer validates
|
|
207
|
+
If `--out` is omitted, the renderer writes beside the JSON input with an `.html` extension. The renderer validates schema version 2, `kind: "pi-agent-trace"`, and the resolved `turn`, `thread`, or `service` scope. It caps CLI input at 2 MB, escapes every displayed value, and emits a restrictive content security policy. Unknown JSON fields are not rendered, reducing the risk of accidentally exposing unrelated diagnostic data.
|
|
178
208
|
|
|
179
209
|
Saved reports should remain content-free and should normally be ignored by Git:
|
|
180
210
|
|
|
@@ -195,19 +225,15 @@ node skills/cloudflare-agent-tracing/scripts/summarize-session-context.mjs \
|
|
|
195
225
|
|
|
196
226
|
The summary retains IDs, roles, tool names/statuses, stop reasons, token counts, and reported cost. It removes text, thinking, tool arguments, and tool results.
|
|
197
227
|
|
|
198
|
-
##
|
|
228
|
+
## Required Pi telemetry adapter
|
|
199
229
|
|
|
200
|
-
`
|
|
230
|
+
`pi-cloudflare-agent-tracing/cloudflare` structurally implements Pi's telemetry context over Cloudflare Workers custom spans without importing project packages. It must be passed to every production `Harness.open()`.
|
|
201
231
|
|
|
202
|
-
|
|
232
|
+
Pi Durable emits content-free admission, phase, task, tool, recovery, and commit evidence. Pi AI emits model, timing, usage, and cost evidence when the provider supplies it. The adapter redacts error messages by default. Workers custom spans do not expose native event and link APIs, so bounded events and links are encoded as attributes.
|
|
203
233
|
|
|
204
|
-
|
|
205
|
-
2. Cover HTTP, RPC, WebSocket, alarms, retries, and resumed tasks.
|
|
206
|
-
3. Keep error-message recording disabled unless content safety is established.
|
|
207
|
-
4. Pass the adapter as `telemetry` to `Harness.open()`.
|
|
208
|
-
5. Verify expected `pi.durable.*` and `pi.ai.*` spans in a bounded production smoke.
|
|
234
|
+
The package never persists a telemetry context in Durable Object state. Cloudflare's `tracing.enterSpan()` uses the active JavaScript async context for request-time nesting; resumed Durable Object work can begin a new trace when no caller span exists.
|
|
209
235
|
|
|
210
|
-
|
|
236
|
+
After integration, verify the adapter under workerd and then prove expected `pi.durable.*` and `pi.ai.*` spans with one bounded, authorized production smoke before relying on the reports.
|
|
211
237
|
|
|
212
238
|
## Security model
|
|
213
239
|
|
|
@@ -237,7 +263,8 @@ skills/cloudflare-agent-tracing/scripts/
|
|
|
237
263
|
render-report.mjs
|
|
238
264
|
skills/cloudflare-agent-tracing/references/
|
|
239
265
|
report.example.json
|
|
240
|
-
runtime/cloudflare-telemetry-context.
|
|
266
|
+
runtime/cloudflare-telemetry-context.js
|
|
267
|
+
runtime/cloudflare-telemetry-context.d.ts
|
|
241
268
|
test/extension.test.mjs
|
|
242
269
|
```
|
|
243
270
|
|
|
@@ -248,4 +275,4 @@ test/extension.test.mjs
|
|
|
248
275
|
- Cloudflare's official MCP server
|
|
249
276
|
- Cloudflare Workers observability for production evidence
|
|
250
277
|
|
|
251
|
-
Pi Durable provides the
|
|
278
|
+
Pi Durable provides the task, recovery, model, usage, and durable outcome evidence required by the unified trace report.
|
|
@@ -31,38 +31,11 @@ const SKILL = "cloudflare-agent-tracing";
|
|
|
31
31
|
const CLOUDFLARE_EXECUTE_SUFFIX = "cloudflare_execute";
|
|
32
32
|
const METHOD_LITERAL = /\bmethod\s*:\s*["'`](GET|POST|PUT|PATCH|DELETE)["'`]/giu;
|
|
33
33
|
|
|
34
|
-
export const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
fallback: "the current project during the last 30 minutes",
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
name: "agent-health",
|
|
43
|
-
description: "Report Cloudflare agent health, errors, recovery, and latency",
|
|
44
|
-
mode: "health",
|
|
45
|
-
fallback: "the current project during the last 60 minutes",
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
name: "agent-cost",
|
|
49
|
-
description: "Analyze provider- or Pi-reported model usage and cost evidence",
|
|
50
|
-
mode: "cost",
|
|
51
|
-
fallback: "the current project during the last 24 hours",
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
name: "agent-eval",
|
|
55
|
-
description: "Evaluate whether an agent fulfilled a request",
|
|
56
|
-
mode: "evaluate",
|
|
57
|
-
fallback: "the target described in the current conversation",
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
name: "agent-incident",
|
|
61
|
-
description: "Produce an evidence-linked Cloudflare agent incident report",
|
|
62
|
-
mode: "incident",
|
|
63
|
-
fallback: "the current project during the last 30 minutes",
|
|
64
|
-
},
|
|
65
|
-
] as const;
|
|
34
|
+
export const command = {
|
|
35
|
+
name: "pi-trace",
|
|
36
|
+
description: "Trace a Pi agent across execution, health, cost, fulfillment, and recovery",
|
|
37
|
+
fallback: "the current project during the last 30 minutes",
|
|
38
|
+
} as const;
|
|
66
39
|
|
|
67
40
|
export const cloudflareExecuteIsReadOnly = (code: unknown): boolean => {
|
|
68
41
|
if (typeof code !== "string" || !code.includes("cloudflare.request")) return false;
|
|
@@ -91,17 +64,15 @@ export default function cloudflareAgentTracing(pi: PiExtensionApi): void {
|
|
|
91
64
|
};
|
|
92
65
|
});
|
|
93
66
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
});
|
|
106
|
-
}
|
|
67
|
+
pi.registerCommand(command.name, {
|
|
68
|
+
description: command.description,
|
|
69
|
+
handler: (args, context) => {
|
|
70
|
+
const target = args.trim() || command.fallback;
|
|
71
|
+
pi.sendUserMessage(`/skill:${SKILL} ${target}`, {
|
|
72
|
+
...(context.isIdle() ? {} : { deliverAs: "followUp" as const }),
|
|
73
|
+
expandPromptTemplates: true,
|
|
74
|
+
});
|
|
75
|
+
if (!context.isIdle()) context.ui.notify(`${command.name} queued`, "info");
|
|
76
|
+
},
|
|
77
|
+
});
|
|
107
78
|
}
|
package/package.json
CHANGED
|
@@ -1,50 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-cloudflare-agent-tracing",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Required Cloudflare custom-span runtime and read-only tracing reports for Pi Durable applications.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agents",
|
|
7
|
+
"cloudflare",
|
|
8
|
+
"observability",
|
|
9
|
+
"pi-durable",
|
|
10
|
+
"pi-extension",
|
|
11
|
+
"pi-package"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/oskarcode/pi-cloudflare-agent-tracing#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/oskarcode/pi-cloudflare-agent-tracing/issues"
|
|
16
|
+
},
|
|
6
17
|
"license": "MIT",
|
|
7
18
|
"author": "Oskar Ablimit",
|
|
8
19
|
"repository": {
|
|
9
20
|
"type": "git",
|
|
10
21
|
"url": "git+https://github.com/oskarcode/pi-cloudflare-agent-tracing.git"
|
|
11
22
|
},
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
"
|
|
23
|
+
"files": [
|
|
24
|
+
"extensions",
|
|
25
|
+
"skills",
|
|
26
|
+
"runtime",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"type": "module",
|
|
31
|
+
"exports": {
|
|
32
|
+
"./cloudflare": {
|
|
33
|
+
"types": "./runtime/cloudflare-telemetry-context.d.ts",
|
|
34
|
+
"import": "./runtime/cloudflare-telemetry-context.js",
|
|
35
|
+
"default": "./runtime/cloudflare-telemetry-context.js"
|
|
36
|
+
},
|
|
37
|
+
"./runtime/cloudflare-telemetry-context.js": {
|
|
38
|
+
"types": "./runtime/cloudflare-telemetry-context.d.ts",
|
|
39
|
+
"import": "./runtime/cloudflare-telemetry-context.js",
|
|
40
|
+
"default": "./runtime/cloudflare-telemetry-context.js"
|
|
41
|
+
},
|
|
42
|
+
"./runtime/cloudflare-telemetry-context.ts": {
|
|
43
|
+
"types": "./runtime/cloudflare-telemetry-context.d.ts",
|
|
44
|
+
"import": "./runtime/cloudflare-telemetry-context.js",
|
|
45
|
+
"default": "./runtime/cloudflare-telemetry-context.js"
|
|
46
|
+
},
|
|
47
|
+
"./package.json": "./package.json"
|
|
15
48
|
},
|
|
16
49
|
"publishConfig": {
|
|
17
50
|
"access": "public"
|
|
18
51
|
},
|
|
19
|
-
"keywords": [
|
|
20
|
-
"pi-package",
|
|
21
|
-
"pi-extension",
|
|
22
|
-
"cloudflare",
|
|
23
|
-
"observability",
|
|
24
|
-
"pi-durable",
|
|
25
|
-
"agents"
|
|
26
|
-
],
|
|
27
|
-
"pi": {
|
|
28
|
-
"extensions": [
|
|
29
|
-
"./extensions/cloudflare-agent-tracing.ts"
|
|
30
|
-
],
|
|
31
|
-
"skills": [
|
|
32
|
-
"./skills"
|
|
33
|
-
]
|
|
34
|
-
},
|
|
35
52
|
"scripts": {
|
|
36
53
|
"test": "node --test test/*.test.mjs runtime/*.test.mjs skills/cloudflare-agent-tracing/scripts/*.test.mjs",
|
|
37
54
|
"profile": "node skills/cloudflare-agent-tracing/scripts/project-profile.mjs",
|
|
38
55
|
"render-report": "node skills/cloudflare-agent-tracing/scripts/render-report.mjs"
|
|
39
56
|
},
|
|
40
|
-
"files": [
|
|
41
|
-
"extensions",
|
|
42
|
-
"skills",
|
|
43
|
-
"runtime",
|
|
44
|
-
"README.md",
|
|
45
|
-
"LICENSE"
|
|
46
|
-
],
|
|
47
57
|
"engines": {
|
|
48
58
|
"node": ">=22"
|
|
59
|
+
},
|
|
60
|
+
"pi": {
|
|
61
|
+
"extensions": [
|
|
62
|
+
"./extensions/cloudflare-agent-tracing.ts"
|
|
63
|
+
],
|
|
64
|
+
"skills": [
|
|
65
|
+
"./skills"
|
|
66
|
+
]
|
|
49
67
|
}
|
|
50
68
|
}
|
package/runtime/README.md
CHANGED
|
@@ -1,29 +1,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Required Worker telemetry
|
|
2
2
|
|
|
3
|
-
`cloudflare-
|
|
3
|
+
The `pi-cloudflare-agent-tracing/cloudflare` export adapts Pi's vendor-neutral telemetry contract to Cloudflare Workers custom spans. Install this package in the Worker application as well as in Pi:
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
```bash
|
|
6
|
+
pnpm add pi-cloudflare-agent-tracing
|
|
7
|
+
pi install npm:pi-cloudflare-agent-tracing
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Enable `observability.enabled` and `observability.traces.enabled` in Wrangler, then wire every production Harness:
|
|
6
11
|
|
|
7
12
|
```ts
|
|
8
13
|
import { tracing } from "cloudflare:workers";
|
|
9
|
-
import { createCloudflareTelemetryContext } from "
|
|
10
|
-
|
|
11
|
-
const telemetry = createCloudflareTelemetryContext(tracing);
|
|
14
|
+
import { createCloudflareTelemetryContext } from "pi-cloudflare-agent-tracing/cloudflare";
|
|
12
15
|
|
|
13
16
|
const harness = await Harness.open(
|
|
14
17
|
storage,
|
|
15
|
-
{
|
|
18
|
+
{
|
|
19
|
+
...harnessOptions,
|
|
20
|
+
telemetry: createCloudflareTelemetryContext(tracing),
|
|
21
|
+
},
|
|
16
22
|
context,
|
|
17
23
|
);
|
|
18
24
|
```
|
|
19
25
|
|
|
20
|
-
Before deployment, prove under workerd that the selected Cloudflare tracing object remains valid for every invocation type that can use the Harness: HTTP, RPC, WebSockets, alarms, retries, and resumed tasks. A long-lived Durable Object must not accidentally retain an invocation-owned context beyond its valid lifetime.
|
|
21
|
-
|
|
22
26
|
The adapter defaults to content-safe behavior:
|
|
23
27
|
|
|
24
28
|
- error names are recorded;
|
|
25
29
|
- error messages are omitted;
|
|
26
30
|
- events and links are bounded;
|
|
27
|
-
- arrays are encoded as JSON attributes because Workers custom spans do not expose native event/link APIs
|
|
31
|
+
- arrays are encoded as JSON attributes because Workers custom spans do not expose native event/link APIs;
|
|
32
|
+
- telemetry contexts are process objects and are never durable application state.
|
|
33
|
+
|
|
34
|
+
Cloudflare's `tracing.enterSpan()` automatically nests spans using the active JavaScript async context and ends them when the callback's promise settles. A Pi task resumed by an alarm or another invocation may correctly start a new root trace when no caller span is active.
|
|
28
35
|
|
|
29
|
-
|
|
36
|
+
The package profiler treats the runtime dependency and `Harness.open()` wiring as required setup. Still validate the selected compatibility date under workerd and prove sampled `pi.durable.*` and `pi.ai.*` spans in the deployed Worker before making production conclusions.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export type AttributeValue =
|
|
2
|
+
| string
|
|
3
|
+
| number
|
|
4
|
+
| boolean
|
|
5
|
+
| readonly string[]
|
|
6
|
+
| readonly number[]
|
|
7
|
+
| readonly boolean[];
|
|
8
|
+
|
|
9
|
+
export interface SpanAttributes {
|
|
10
|
+
[name: string]: AttributeValue | undefined;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type TelemetryStatus =
|
|
14
|
+
| { status: "ok" }
|
|
15
|
+
| { status: "error"; error?: { name: string; message: string } };
|
|
16
|
+
|
|
17
|
+
export interface TelemetryLink {
|
|
18
|
+
context: TelemetryContext;
|
|
19
|
+
attributes?: SpanAttributes;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface TelemetryContext {
|
|
23
|
+
startSpan<T>(
|
|
24
|
+
options: { name: string; attributes?: SpanAttributes; links?: readonly TelemetryLink[] },
|
|
25
|
+
callback: (span: TelemetrySpan) => T | Promise<T>,
|
|
26
|
+
): Promise<T>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface TelemetrySpan extends TelemetryContext {
|
|
30
|
+
addLink(link: TelemetryLink): void;
|
|
31
|
+
addEvent(name: string, attributes?: SpanAttributes): void;
|
|
32
|
+
setAttributes(attributes: SpanAttributes): void;
|
|
33
|
+
setStatus(status: TelemetryStatus): void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface CloudflareSpan {
|
|
37
|
+
setAttribute(key: string, value: string | number | boolean): this;
|
|
38
|
+
setAttributes(attributes: Record<string, string | number | boolean | undefined>): this;
|
|
39
|
+
setStatus(status: { code: "unset" | "ok" | "error"; message?: string }): this;
|
|
40
|
+
spanContext(): { traceId: string; spanId: string; traceFlags: number };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface CloudflareTracing {
|
|
44
|
+
enterSpan<T>(name: string, callback: (span: CloudflareSpan) => T): T;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface CloudflareTelemetryOptions {
|
|
48
|
+
/** Error messages can contain user/provider content and are redacted by default. */
|
|
49
|
+
includeErrorMessages?: boolean;
|
|
50
|
+
/** Events are encoded as bounded attributes because Workers custom spans have no event API. */
|
|
51
|
+
maxEventsPerSpan?: number;
|
|
52
|
+
maxLinksPerSpan?: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export declare function createCloudflareTelemetryContext(
|
|
56
|
+
tracing: CloudflareTracing,
|
|
57
|
+
options?: CloudflareTelemetryOptions,
|
|
58
|
+
): TelemetryContext;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
const DEFAULT_MAX_EVENTS = 32;
|
|
2
|
+
const DEFAULT_MAX_LINKS = 16;
|
|
3
|
+
const MAX_ERROR_MESSAGE = 500;
|
|
4
|
+
|
|
5
|
+
export function createCloudflareTelemetryContext(tracing, options = {}) {
|
|
6
|
+
return new CloudflareTelemetryContext(tracing, {
|
|
7
|
+
includeErrorMessages: options.includeErrorMessages ?? false,
|
|
8
|
+
maxEventsPerSpan: options.maxEventsPerSpan ?? DEFAULT_MAX_EVENTS,
|
|
9
|
+
maxLinksPerSpan: options.maxLinksPerSpan ?? DEFAULT_MAX_LINKS,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
class CloudflareTelemetryContext {
|
|
14
|
+
constructor(tracing, options) {
|
|
15
|
+
this.tracing = tracing;
|
|
16
|
+
this.options = options;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async startSpan(options, callback) {
|
|
20
|
+
return await this.tracing.enterSpan(options.name, (nativeSpan) => {
|
|
21
|
+
const span = new CloudflareTelemetrySpan(this.tracing, nativeSpan, this.options);
|
|
22
|
+
if (options.attributes) span.setAttributes(options.attributes);
|
|
23
|
+
for (const link of options.links ?? []) span.addLink(link);
|
|
24
|
+
|
|
25
|
+
let result;
|
|
26
|
+
try {
|
|
27
|
+
result = callback(span);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
span.finishError(error);
|
|
30
|
+
return Promise.reject(error);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return Promise.resolve(result).then(
|
|
34
|
+
(value) => {
|
|
35
|
+
span.finishSuccess();
|
|
36
|
+
return value;
|
|
37
|
+
},
|
|
38
|
+
(error) => {
|
|
39
|
+
span.finishError(error);
|
|
40
|
+
throw error;
|
|
41
|
+
},
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class CloudflareTelemetrySpan {
|
|
48
|
+
settled = false;
|
|
49
|
+
explicitStatus = false;
|
|
50
|
+
eventCount = 0;
|
|
51
|
+
linkCount = 0;
|
|
52
|
+
|
|
53
|
+
constructor(tracing, nativeSpan, options) {
|
|
54
|
+
this.tracing = tracing;
|
|
55
|
+
this.nativeSpan = nativeSpan;
|
|
56
|
+
this.options = options;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
startSpan(options, callback) {
|
|
60
|
+
return new CloudflareTelemetryContext(this.tracing, this.options).startSpan(options, callback);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
addLink(link) {
|
|
64
|
+
if (this.settled || this.linkCount >= this.options.maxLinksPerSpan) return;
|
|
65
|
+
if (!(link.context instanceof CloudflareTelemetrySpan)) return;
|
|
66
|
+
|
|
67
|
+
const index = this.linkCount++;
|
|
68
|
+
const context = link.context.nativeSpan.spanContext();
|
|
69
|
+
this.nativeSpan.setAttributes({
|
|
70
|
+
[`link.${index}.trace_id`]: context.traceId,
|
|
71
|
+
[`link.${index}.span_id`]: context.spanId,
|
|
72
|
+
...prefixAttributes(`link.${index}.attribute.`, link.attributes),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
addEvent(name, attributes) {
|
|
77
|
+
if (this.settled || this.eventCount >= this.options.maxEventsPerSpan) return;
|
|
78
|
+
const index = this.eventCount++;
|
|
79
|
+
this.nativeSpan.setAttributes({
|
|
80
|
+
[`event.${index}.name`]: name,
|
|
81
|
+
...prefixAttributes(`event.${index}.attribute.`, attributes),
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
setAttributes(attributes) {
|
|
86
|
+
if (this.settled) return;
|
|
87
|
+
this.nativeSpan.setAttributes(normalizeAttributes(attributes));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
setStatus(status) {
|
|
91
|
+
if (this.settled) return;
|
|
92
|
+
this.explicitStatus = true;
|
|
93
|
+
if (status.status === "ok") {
|
|
94
|
+
this.nativeSpan.setStatus({ code: "ok" });
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const message = this.options.includeErrorMessages
|
|
99
|
+
? status.error?.message.slice(0, MAX_ERROR_MESSAGE)
|
|
100
|
+
: undefined;
|
|
101
|
+
this.nativeSpan.setAttributes({ "error.name": status.error?.name });
|
|
102
|
+
this.nativeSpan.setStatus({ code: "error", ...(message ? { message } : {}) });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
finishSuccess() {
|
|
106
|
+
if (this.settled) return;
|
|
107
|
+
if (!this.explicitStatus) this.nativeSpan.setStatus({ code: "ok" });
|
|
108
|
+
this.settled = true;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
finishError(error) {
|
|
112
|
+
if (this.settled) return;
|
|
113
|
+
const name = error instanceof Error ? error.name : "UnknownError";
|
|
114
|
+
const message =
|
|
115
|
+
this.options.includeErrorMessages && error instanceof Error
|
|
116
|
+
? error.message.slice(0, MAX_ERROR_MESSAGE)
|
|
117
|
+
: undefined;
|
|
118
|
+
this.nativeSpan.setAttributes({ "error.name": name });
|
|
119
|
+
this.nativeSpan.setStatus({ code: "error", ...(message ? { message } : {}) });
|
|
120
|
+
this.settled = true;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function normalizeAttributes(attributes) {
|
|
125
|
+
const normalized = {};
|
|
126
|
+
for (const [name, value] of Object.entries(attributes ?? {})) {
|
|
127
|
+
normalized[name] =
|
|
128
|
+
value !== undefined && typeof value === "object" ? JSON.stringify(value) : value;
|
|
129
|
+
}
|
|
130
|
+
return normalized;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function prefixAttributes(prefix, attributes) {
|
|
134
|
+
const prefixed = {};
|
|
135
|
+
for (const [name, value] of Object.entries(normalizeAttributes(attributes))) {
|
|
136
|
+
prefixed[`${prefix}${name}`] = value;
|
|
137
|
+
}
|
|
138
|
+
return prefixed;
|
|
139
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import test from "node:test";
|
|
3
|
-
import { createCloudflareTelemetryContext } from "
|
|
3
|
+
import { createCloudflareTelemetryContext } from "pi-cloudflare-agent-tracing/cloudflare";
|
|
4
4
|
|
|
5
5
|
class NativeSpan {
|
|
6
6
|
attributes = {};
|
|
@@ -79,7 +79,10 @@ test("encodes links using trace and span identifiers", async () => {
|
|
|
79
79
|
await telemetry.startSpan({ name: "parent" }, async (span) => {
|
|
80
80
|
parent = span;
|
|
81
81
|
});
|
|
82
|
-
await telemetry.startSpan(
|
|
82
|
+
await telemetry.startSpan(
|
|
83
|
+
{ name: "child", links: [{ context: parent, attributes: { reason: "queued" } }] },
|
|
84
|
+
async () => {},
|
|
85
|
+
);
|
|
83
86
|
|
|
84
87
|
assert.deepEqual(tracing.spans[1].attributes, {
|
|
85
88
|
"link.0.trace_id": "trace-1",
|