Skip to content

Commit bb012fd

Browse files
committed
Add observability docs for Sandbox SDK
1 parent 44f31cc commit bb012fd

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

src/content/docs/sandbox/guides/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ These guides show you how to solve specific problems and implement features with
1616
- [Use code interpreter](/sandbox/guides/code-execution/) - Execute Python and JavaScript code with rich outputs
1717
- [Work with Git](/sandbox/guides/git-workflows/) - Clone repositories, manage branches, and automate Git operations
1818
- [Stream output](/sandbox/guides/streaming-output/) - Handle real-time output from commands and processes
19+
- [Observability](/sandbox/guides/observability/) - View logs and debug with trace IDs
1920

2021
## Related resources
2122

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
title: Observability
3+
pcx_content_type: how-to
4+
sidebar:
5+
order: 8
6+
---
7+
8+
import { TypeScriptExample } from "~/components";
9+
10+
This guide shows you how to enable and view logs from your Sandbox applications.
11+
12+
## Enable logging
13+
14+
To view logs, enable Workers Logs in your `wrangler.jsonc`:
15+
16+
```jsonc
17+
{
18+
"name": "my-worker",
19+
"observability": {
20+
"enabled": true
21+
}
22+
}
23+
```
24+
25+
Deploy your Worker for logs to start appearing.
26+
27+
## View logs
28+
29+
After deploying, logs appear in two places:
30+
31+
**Durable Objects logs** - High-level operations
32+
1. Go to [Workers & Pages](https://dash.cloudflare.com/?to=/:account/workers-and-pages) on the Cloudflare dashboard
33+
2. Select your Worker → **Bindings** → Select the **Sandbox** Durable Object binding → **Logs**
34+
35+
**Container logs** - Detailed execution
36+
1. Go to [Containers](https://dash.cloudflare.com/?to=/:account/containers) on the Cloudflare dashboard
37+
2. Select your container → **Logs**
38+
39+
## Debug with trace IDs
40+
41+
The Sandbox SDK automatically adds trace IDs to all logs. Use them to follow a request across Durable Objects and Containers.
42+
43+
### Find trace IDs
44+
45+
Every log entry includes a `traceId` field:
46+
47+
```json
48+
{
49+
"level": "info",
50+
"msg": "Command execution started",
51+
"traceId": "tr_7f3a9b2c4e5d6f1a",
52+
"sandboxId": "user-123",
53+
"command": "python analyze.py"
54+
}
55+
```
56+
57+
### Follow a request
58+
59+
1. Find any log related to your request
60+
2. Copy the `traceId` value
61+
3. Filter both Durable Objects and Container logs with: `traceId:"tr_7f3a9b2c4e5d6f1a"`
62+
4. View the complete flow
63+
64+
Example - debugging a failed command:
65+
66+
```json
67+
// Durable Objects log
68+
{
69+
"msg": "Command execution started",
70+
"traceId": "tr_7f3a9b2c4e5d6f1a",
71+
"sandboxId": "user-123",
72+
"command": "python analyze.py"
73+
}
74+
75+
// Container log
76+
{
77+
"level": "error",
78+
"msg": "Command execution failed",
79+
"traceId": "tr_7f3a9b2c4e5d6f1a",
80+
"exitCode": 1,
81+
"stderr": "ModuleNotFoundError: No module named 'pandas'"
82+
}
83+
```
84+
85+
The trace ID connects both logs, showing the command failed due to a missing dependency.
86+
87+
## Reduce log volume
88+
89+
For high-traffic applications, sample logs to reduce costs:
90+
91+
```jsonc
92+
{
93+
"observability": {
94+
"enabled": true,
95+
"head_sampling_rate": 0.1 // Log 10% of requests
96+
}
97+
}
98+
```
99+
100+
## Troubleshooting
101+
102+
### Logs not appearing
103+
104+
If you don't see logs:
105+
- Verify `observability.enabled: true` in `wrangler.jsonc`
106+
- Deploy your Worker (logs don't appear locally)
107+
- Wait a few seconds after deployment
108+
- Check you're viewing the correct Worker/Container
109+
110+
### Can't find specific request
111+
112+
1. Check both Durable Objects and Container logs
113+
2. Search by `sandboxId` if you know it
114+
3. Filter by time range around when the request occurred
115+
4. Use trace IDs to see the full request flow
116+
117+
## Related resources
118+
119+
- [Workers Logs](/workers/observability/logs/workers-logs/) - Log streaming and analysis
120+
- [Containers logs FAQ](/containers/faq/#how-do-container-logs-work) - Container-specific logging
121+
- [Durable Objects logs](/durable-objects/observability/metrics-and-analytics/#view-logs) - DO logging details

0 commit comments

Comments
 (0)