Skip to content

Conversation

@petebacondarwin
Copy link
Contributor

@petebacondarwin petebacondarwin commented Jan 23, 2026

This refactoring moves the telemetry events (wrangler command started/completed/errored) from the yargs middleware in index.ts to the command handler in register-yargs-command.ts.

Benefits:

Telemetry is now sent after config is loaded, allowing access to send_metrics and hasAssets

Key changes:

  • Add CommandHandledError wrapper class to signal when telemetry has been sent
  • Add getErrorType() function to classify errors for telemetry
  • Send telemetry events in register-yargs-command.ts after config is loaded
  • Preserve fallback telemetry in index.ts for yargs validation errors
  • Handle nested wrangler.parse() calls by not double-wrapping CommandHandledError
  • Properly unwrap CommandHandledError when writing command-failed output

  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: refactor

A picture of a cute animal (not mandatory, but encouraged)


Open with Devin

…mmand handlers

This refactoring moves the telemetry events (wrangler command started/completed/errored)
from the yargs middleware in index.ts to the command handler in register-yargs-command.ts.

Benefits:
- Telemetry is now sent after config is loaded, allowing access to send_metrics and hasAssets
- Telemetry is only sent for commands that use defineCommand

Key changes:
- Add CommandHandledError wrapper class to signal when telemetry has been sent
- Add getErrorType() function to classify errors for telemetry
- Send telemetry events in register-yargs-command.ts after config is loaded
- Preserve fallback telemetry in index.ts for yargs validation errors
- Handle nested wrangler.parse() calls by not double-wrapping CommandHandledError
- Properly unwrap CommandHandledError when writing command-failed output
@changeset-bot
Copy link

changeset-bot bot commented Jan 23, 2026

⚠️ No Changeset found

Latest commit: ec8e9f8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

…hers

Move the requests tracking from per-dispatcher instance to module-level
scope so that all dispatchers share the same Set of pending requests.
This ensures all metrics requests are awaited before Wrangler exits,
regardless of which dispatcher created them.

- Add module-level pendingRequests Set with auto-cleanup via .finally()
- Export waitForAllMetricsDispatches() to await all pending requests
- Remove the per-dispatcher requests getter
* Wait for all pending metrics requests to complete.
* This should be called before the process exits to ensure all metrics are sent.
*/
export function waitForAllMetricsDispatches(): Promise<void> {
Copy link
Contributor

@vicb vicb Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you want to keep it sync getMetricsDispatchesCompleted could be a good name but looking at the call sites, making it async (+ keep the name) sounds like the best thing to do. There is only one call that is not awaited which is not a problem as it needs a promise.

Context: A lot (including me) have been confused by ctx.waitUntil being sync. My understanding is that the runtime team would change the name if it wasn't too late.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "make it async"? It returns a Promise, which makes it async from the outside. How it is implemented internally makes no difference.

Copy link
Contributor

@vicb vicb Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export async function waitForAllMetricsDispatches(): Promise<void>

or drop "waitFor" because sync function can't wait

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clearer:

export async function waitForAllMetricsDispatches(): Promise<void> {
  await Promise.allSettled(pendingRequests);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't understand how this makes the function any clearer. It has the same signature: () => Promise<void>.
The caller doesn't see the async modifier in intellisense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could not explain any better than:

Image

The comments for the PR review were taking from the previous PR (#12055)
@petebacondarwin petebacondarwin force-pushed the pbd/wrangler/telemetry-event-refactor branch from be3fd85 to e0829f5 Compare January 23, 2026 17:53
@petebacondarwin petebacondarwin marked this pull request as ready for review January 23, 2026 17:56
@petebacondarwin petebacondarwin requested a review from a team as a code owner January 23, 2026 17:56
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 23, 2026

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@12069

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@12069

miniflare

npm i https://pkg.pr.new/miniflare@12069

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@12069

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@12069

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@12069

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@12069

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@12069

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@12069

wrangler

npm i https://pkg.pr.new/wrangler@12069

commit: ec8e9f8

Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 6 additional flags.

Open in Devin Review

@petebacondarwin petebacondarwin requested a review from vicb January 24, 2026 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Untriaged

Development

Successfully merging this pull request may close these issues.

2 participants