-
Notifications
You must be signed in to change notification settings - Fork 16.2k
Add new page for logging from Workers #2107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
16672fc
Add new page for logging from Workers
jkup 9764776
Apply suggestions from code review
jkup 7706629
add screenshot
jkup 96c19e6
nice rephrase from review
jkup 00a3ce5
Apply suggestions from code review
jkup 22afdf0
Apply suggestions from code review
jkup d81b62e
Apply suggestions from code review
jkup bfcf5c1
Update copy from PR review
jkup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| order: 5 | ||
| order: 6 | ||
| pcx-content-type: concept | ||
| --- | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| order: 6 | ||
| order: 7 | ||
| pcx-content-type: concept | ||
| --- | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| order: 4 | ||
| order: 5 | ||
| pcx-content-type: concept | ||
| --- | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| order: 12 | ||
| order: 13 | ||
| pcx-content-type: concept | ||
| --- | ||
|
|
||
|
|
||
175 changes: 175 additions & 0 deletions
175
products/workers/src/content/learning/logging-workers.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| --- | ||
| order: 4 | ||
| pcx-content-type: concept | ||
| --- | ||
|
|
||
| # Logging from Workers | ||
|
|
||
| You can access logs and exceptions for your Workers using the dashboard or [`wrangler tail`](/cli-wrangler/commands#tail). | ||
|
|
||
| The Workers platform captures all `console.log`'s and uncaught exceptions, in addition to information about the event itself. All of this can be viewed with either `wrangler tail` or on the dashboard through your **Account Home** > **Workers** > your **Workers script** > **Logs**. | ||
|
|
||
| ## Adding custom logs | ||
|
|
||
| Any `console.log` statements within your Worker will appear within `wrangler tail` and the dashboard output. The following example demonstrates a custom `console.log` within a Worker request handler. | ||
|
|
||
| ```js | ||
| addEventListener('fetch', event => { | ||
| event.respondWith(handleRequest(event.request)) | ||
| }) | ||
|
|
||
| /** | ||
| * Respond with hello worker text | ||
| * @param {Request} request | ||
| */ | ||
| async function handleRequest(request) { | ||
| const { cf } = request | ||
| const { city, country } = cf | ||
|
|
||
| console.log(`Request came from city: ${city} in country: ${country}`); | ||
|
|
||
| return new Response('Hello worker!', { | ||
| headers: { 'content-type': 'text/plain' }, | ||
| }) | ||
| } | ||
| ``` | ||
|
|
||
| After you deploy the above code, run `wrangler tail` in your terminal, and then access your Worker. Your terminal will display: | ||
|
|
||
| ```sh | ||
| ☁ logging-example [master] ⚡ wrangler tail --format=pretty | ||
| [2021-08-18 17:06:55] [LAX] [Ok] GET https://logging-example.jkup.workers.dev/ | ||
| | [Info] Request came from city: Pacifica in country: US | ||
| [2021-08-18 17:06:56] [LAX] [Ok] GET https://logging-example.jkup.workers.dev/favicon.ico | ||
| | [Info] Request came from city: Pacifica in country: US | ||
| ``` | ||
|
|
||
| ## View logs using `wrangler tail` | ||
|
|
||
| With your Workers application deployed, you may want to inspect incoming traffic. This may be useful in situations where a user is running into production issues that they cannot easily reproduce. In these instances, `wrangler tail` allows developers to “tail” their Workers application’s logs, giving real-time insight into their application's incoming requests. | ||
|
|
||
| To get started, run `wrangler tail` in your Workers project directory. This will log any incoming requests to your application available in your local terminal. | ||
|
|
||
| The output of each `wrangler tail` log is a structured JSON object: | ||
|
|
||
| ```json | ||
| { | ||
| "outcome": "ok", | ||
| "scriptName": null, | ||
| "exceptions": [], | ||
| "logs": [], | ||
| "eventTimestamp": 1590680082349, | ||
| "event": { | ||
| "request": { | ||
| "url": "https://www.bytesized.xyz/", | ||
| "method": "GET", | ||
| "headers": {}, | ||
| "cf": {} | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| By piping the output to tools like [`jq`](https://stedolan.github.io/jq/), you can query and manipulate the requests to look for specific information: | ||
|
|
||
| ```sh | ||
| $ wrangler tail | jq .event.request.url | ||
| "https://www.bytesized.xyz/" | ||
| "https://www.bytesized.xyz/component---src-pages-index-js-a77e385e3bde5b78dbf6.js" | ||
| "https://www.bytesized.xyz/page-data/app-data.json" | ||
| ``` | ||
|
|
||
| You can customize how `wrangler tail` works to fit your needs: refer to [the `wrangler tail` documentation](/cli-wrangler/commands#tail) for available configuration options. | ||
|
|
||
| ## View logs from the dashboard | ||
|
|
||
| You can review the production logs associated with any Worker by [logging into the Cloudflare dashboard](https://dash.cloudflare.com?to=/:account/workers/overview). From your **Account Home** > go to *Workers** > select your **Worker script** > and select **Logs**. Logging is available for all customers, including those on the free plan. | ||
|
|
||
|  | ||
|
|
||
| A few things worth mentioning: | ||
|
|
||
| - Workers logs are not stored. You can start and stop the stream at any time to view them, but they do not persist. | ||
| - Logs will not display if the Worker's requests per second are over 200 for the last 5 minutes. | ||
| - Logs from any [Durable Objects](/learning/using-durable-objects) your Worker is using will show up in the dashboard. | ||
| - A maximum of 10 clients can view a Worker's logs at one time. This can be a combination of either dashboard sessions or `wrangler tail` calls. | ||
|
|
||
| -------------------------------- | ||
|
|
||
| ## Identifying and handling errors and exceptions | ||
|
|
||
| ### Error pages generated by Workers | ||
|
|
||
| When a Worker running in production has an error that prevents it from returning a response, the client will receive an error page with an error code, defined as follows: | ||
|
|
||
| <TableWrap> | ||
|
|
||
| | Error code | Meaning | | ||
| | ---------- | ---------------------------------------------------------------------------------------------------------| | ||
| | 1101 | Worker threw a JavaScript exception. | | ||
| | 1102 | Worker exceeded [CPU time limit](/platform/limits). | | ||
| | 1015 | Your client IP is being rate limited. | | ||
| | 1027 | Worker exceeded free tier [daily request limit](/platform/limits#daily-request). | | ||
| | 1042 | Worker tried to fetch from another Worker on the same zone, which is [unsupported](/runtime-apis/fetch). | | ||
|
|
||
| </TableWrap> | ||
|
|
||
| Other `11xx` errors generally indicate a problem with the Workers runtime itself. Refer to the [status page](https://www.cloudflarestatus.com) if you are experiencing an error. | ||
|
|
||
| ### Identifying errors: Workers Metrics | ||
|
|
||
| You can find out whether your application is experiencing any downtime, or returning any errors by navigating from **Account Home** > to **Workers** > your **Worker script** > **Metrics** in the dashboard. | ||
|
|
||
| <!-- TODO: include screenshots --> | ||
|
jkup marked this conversation as resolved.
|
||
|
|
||
| ### Debugging exceptions | ||
|
|
||
| After you have identified your Workers application is returning exceptions, use `wrangler tail` to inspect and fix the exceptions. | ||
|
|
||
| <!-- TODO: include example --> | ||
|
jkup marked this conversation as resolved.
|
||
|
|
||
| Exceptions will show up under the `exceptions` field in the JSON returned by `wrangler tail`. After you have identified the exception that is causing errors, redeploy your code with a fix, and continue tailing the logs to confirm that it is fixed. | ||
|
|
||
| ### Set up a logging service | ||
|
|
||
| A Worker can make HTTP requests to any HTTP service on the public Internet. You can use a service like [Sentry](https://sentry.io) to collect error logs from your Worker, by making an HTTP request to the service to report the error. Refer to your service’s API documentation for details on what kind of request to make. | ||
|
|
||
| When using an external logging strategy, remember that outstanding asynchronous tasks are canceled as soon as a Worker finishes sending its main response body to the client. To ensure that a logging subrequest completes, pass the request promise to [`event.waitUntil()`](https://developer.mozilla.org/en-US/docs/Web/API/ExtendableEvent/waitUntil). For example: | ||
|
|
||
| ```js | ||
| addEventListener("fetch", event => { | ||
| event.respondWith(handleEvent(event)) | ||
| }) | ||
|
|
||
| async function handleEvent(event) { | ||
| // ... | ||
|
|
||
| // Without event.waitUntil(), the `postLog` function may or may not complete. | ||
| event.waitUntil(postLog(stack)) | ||
| return fetch(event.request) | ||
| } | ||
|
|
||
| function postLog(data) { | ||
| return fetch("https://log-service.example.com/", { | ||
| method: "POST", | ||
| body: data, | ||
| }) | ||
| } | ||
| ``` | ||
|
|
||
| ### Go to origin on error | ||
|
|
||
| By using [`event.passThroughOnException`](/runtime-apis/fetch-event#methods), a Workers application will forward requests to your origin if an exception is thrown during the Worker's execution. This allows you to add logging, tracking, or other features with Workers, without degrading your application's functionality. | ||
|
|
||
| ```js | ||
| addEventListener("fetch", event => { | ||
| event.passThroughOnException() | ||
| event.respondWith(handleRequest(event.request)) | ||
| }) | ||
|
|
||
| async function handleRequest(request) { | ||
| // An error here will return the origin response, as if the Worker wasn’t present. | ||
| // ... | ||
| return fetch(request) | ||
| } | ||
| ``` | ||
Binary file added
BIN
+90.9 KB
products/workers/src/content/learning/media/workers-logging-dashboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| order: 11 | ||
| order: 12 | ||
| pcx-content-type: concept | ||
| --- | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| order: 10 | ||
| order: 11 | ||
| pcx-content-type: concept | ||
| --- | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| order: 8 | ||
| order: 9 | ||
| pcx-content-type: concept | ||
| --- | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| order: 7 | ||
| order: 8 | ||
| pcx-content-type: concept | ||
| --- | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| order: 9 | ||
| order: 10 | ||
| pcx-content-type: concept | ||
| --- | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.