Skip to content

Commit 0508944

Browse files
committed
[workers-observability] add execution log message summary, overview page, best practices
1 parent 0cd8470 commit 0508944

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
lines changed
14.8 KB
Loading

src/content/docs/workers/observability/logs/index.mdx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,22 @@ sidebar:
77
hideIndex: false
88
---
99

10-
import { DirectoryListing } from "~/components";
10+
import { Badge } from "~/components";
1111

12-
TODO
12+
Logs are an important component of a developer's toolkit to troubleshoot and diagnose application issues and maintaining system health. The Cloudflare Developer Platform offers many tools to help developers manage their application's logs.
1313

14-
<DirectoryListing />
14+
## [Workers Logs](/workers/observability/logs/workers-logs) <Badge text="New" variant="tip" size="large" />
15+
16+
Automatically ingest, filter, and analyze logs emitted from Cloudflare Workers in the Cloudflare dashboard.
17+
18+
## [Real-time logs](/workers/observability/logs/real-time-logs)
19+
20+
Access log events in near real-time. Real-time logs provide immediate feedback and visiblity into the health of your Cloudflare Worker.
21+
22+
## [Tail Workers](/workers/observability/logs/tail-workers) <Badge text="Beta" size="large"/>
23+
24+
Tail Workers allow developers to apply custom filtering, sampling, and transformation logic to telemetry data.
25+
26+
## [Workers Logpush](/workers/observability/logs/workers-logpush)
27+
28+
Send Workers Trace Event Logs to a supported destination. Workers Logpush includes metadata about requests and responses, unstructured `console.log()` messages and any uncaught exceptions.

src/content/docs/workers/observability/logs/real-time-logs.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ To view real-time logs associated with any deployed Worker using the Cloudflare
2727
<Steps>
2828
1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com) and select your account.
2929
2. In Account Home, go to **Workers & Pages**.
30-
3. In **Overview**, select your **Worker** > and select **Logs**.
31-
4. In the right-hand navigation bar, select **Live**.
30+
3. In **Overview**, select your **Worker**.
31+
4. Select **Logs**.
32+
5. In the right-hand navigation bar, select **Live**.
3233
</Steps>
3334

3435
## View logs using `wrangler tail`

src/content/docs/workers/observability/logs/workers-logs.mdx

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ You must add the observability setting for your Worker to write logs to Workers
3535
head_sampling_rate = 1 # optional. default = 1.
3636
```
3737

38-
The default value for `head_sampling_rate` is 1, meaning 100% of your logs are sampled. `head_sampling_rate` can be set to a value between 0 and 1. For example, 0.01 indicates that 1% of your logs are sampled.
38+
[Head-based sampling](/workers/observability/logs/workers-logs/#head-based-sampling) allows you set the percentage of Workers requests that are logged.
3939

4040
### Enabling with environments
4141

@@ -56,20 +56,42 @@ To view logs for your Worker:
5656
<Steps>
5757
1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com) and select your account.
5858
2. In Account Home, go to **Workers & Pages**.
59-
3. In **Overview**, select your **Worker** > and select **Logs**.
59+
3. In **Overview**, select your **Worker**.
60+
4. Select **Logs**.
6061
</Steps>
6162

6263
## Best Practices
6364

64-
### Logging JSON objects
65+
### Logging structured JSON objects
6566

66-
To get the most out of Workers Logs, it is recommended to log in JSON format. Workers Logs automatically extracts the fields and indexes TODO
67+
To get the most out of Workers Logs, it is recommended you log in JSON format. Workers Logs automatically extracts the fields and indexes them intelligently in the database. The benefit of this structured logging technique is in how it allows you to easily segment data across any dimension for fields with unlimited cardinality. Consider the following scenarios:
68+
69+
| Scenario | Logging Code | Event Log (Partial) |
70+
| -------- | ---------------------------------------------------------- | --------------------------------------------- |
71+
| 1 | `console.log("user_id: " + 123)` | `{message: "user_id: 123"}` |
72+
| 2 | `console.log({user_id: 123})` | `{user_id: 123}` |
73+
| 3 | `console.log({user_id: 123, user_email: "[email protected]"})` | `{user_id: 123, user_email: "[email protected]"}` |
74+
75+
The difference between these examples is in how you index your logs to enable faster queries. In scenario 1, the `user_id` is embedded within a message. To find all logs relating to a particular user_id, you would have to run a text match. In scenarios 2 and 3, your logs can be filtered against the keys `user_id` and `user_email`.
6776

6877
## Features
6978

7079
### Execution Logs
7180

72-
Each Workers invocation returns an execution log that contain details about the Request, Response, and related metadata. TODO
81+
Each Workers invocation returns a single execution log that contains details such as the Request, Response, and related metadata. These execution logs can be identified by the field `$cloudflare.$metadata.type = "cf-worker-event"`. Each execution log is enriched with information available to Cloudlare in the context of the invocation.
82+
83+
In the Workers Logs UI, logs are presented with a localized timestamp and a Message. The Message is dependent on the invocation handler. For example, Fetch requests will have a message describing the request method and the request URL, while cron events will be listed as cron. Below is a list of invocation handlers along with their execution message.
84+
85+
| Invocation Handler | Execution Message |
86+
| --------------------------------------------------------------- | -------------------------- |
87+
| [Alarm](/durable-objects/api/alarms/) | \<Scheduled Time\> |
88+
| [Email](/email-routing/email-workers/runtime-api/) | \<Email Recipient\> |
89+
| [Fetch](/workers/runtime-apis/handlers/fetch/) | \<Method\> \<URL\> |
90+
| [Queue](/queues/configuration/javascript-apis/#consumer) | \<Queue Name\> |
91+
| [Cron](/workers/runtime-apis/handlers/scheduled/) | \<unix-cron schedule\> |
92+
| [Tail](/workers/runtime-apis/handlers/tail/) | tail |
93+
| [RPC](/workers/runtime-apis/rpc/) | \<RPC method\> |
94+
| [Websocket](/workers/examples/websockets/) | \<Websocket Event Type\> |
7395

7496
### Add custom logs
7597

@@ -121,11 +143,17 @@ async function handleRequest(request) {
121143

122144
After you deploy the code above, view your Worker's logs in [the dashboard](/workers/observability/logs/workers-logs/#view-logs-from-the-dashboard) or with [real-time logs](/workers/observability/logs/real-time-logs/).
123145

124-
### Head-based Sampling
146+
### Head-based sampling
147+
148+
Head-based sampling allows you to log a percentage of incoming requests to your Cloudflare Worker. Especially for high-traffic applications, this helps reduce log volume and manage costs, while still providing meaningful insights into your application's performance. When you configure a head-based sampling rate, you can control the percentage of requests that get logged. All logs within the context of the request are collected.
125149

126-
Head-based sampling allows you to log only a subset of incoming requests to your Cloudflare Workers. Especially for high-traffic applications, this helps reduce log volume and manage costs, while still providing meaningful insights into your application's performance. When you configure a head-based sampling rate, you can control the percentage of requests that get logged. All logs within the context of the request are collected.
150+
To enable head-based sampling, set `head_sampling_rate` within the observability configuration. The valid range is from 0 to 1, where 0 indicates zero out of one hundred requests are logged, and 1 indicates every request is logged. If `head_sampling_rate` is unspecified, it is configured to a default value of 1 (100%). In the example below, `head_sampling_rate` is set to 0.01, which means one out of every one hundred requests is logged.
127151

128-
To enable head-based sampling, simply set `head_sampling_rate` within the observability configuration. For example, setting a sampling rate of 0.01 (1%) will log one out of every one hundred requests. If the `head_sampling_rate` is unspecified, it is configured to a default value of 1 (100%).
152+
```toml
153+
[observability]
154+
enabled = true
155+
head_sampling_rate = 0.01 # 1% sampling rate
156+
```
129157

130158
## Limits
131159

0 commit comments

Comments
 (0)