Skip to content
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

feat: support outputing ND-JSON files via an environment variable #6383

Merged
merged 7 commits into from
Aug 7, 2024

Conversation

petebacondarwin
Copy link
Contributor

@petebacondarwin petebacondarwin commented Jul 31, 2024

What this PR solves / how to test

Adds a feature needed for the Workers CI/CD team.

Author has addressed the following

  • Tests
    • TODO (before merge)
    • Included
    • Not necessary because:
  • E2E Tests CI Job required? (Use "e2e" label or ask maintainer to run separately)
    • I don't know
    • Required / Maybe required
    • Not required because: no e2e changes
  • Changeset (Changeset guidelines)
    • TODO (before merge)
    • Included
    • Not necessary because:
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Not necessary because: internal usage for now

@petebacondarwin petebacondarwin requested a review from a team as a code owner July 31, 2024 04:16
Copy link

changeset-bot bot commented Jul 31, 2024

🦋 Changeset detected

Latest commit: 916ccb4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
wrangler Minor
@cloudflare/vitest-pool-workers Patch

Not sure what this means? Click here to learn what changesets are.

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

Copy link
Contributor

github-actions bot commented Jul 31, 2024

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10282849166/npm-package-wrangler-6383

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/6383/npm-package-wrangler-6383

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10282849166/npm-package-wrangler-6383 dev path/to/script.js
Additional artifacts:
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10282849166/npm-package-create-cloudflare-6383 --no-auto-update
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10282849166/npm-package-cloudflare-kv-asset-handler-6383
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10282849166/npm-package-miniflare-6383
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10282849166/npm-package-cloudflare-pages-shared-6383
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10282849166/npm-package-cloudflare-vitest-pool-workers-6383

Note that these links will no longer work once the GitHub Actions artifact expires.


[email protected] includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20240725.0
workerd 1.20240725.0 1.20240725.0
workerd --version 1.20240725.0 2024-07-25

Please ensure constraints are pinned, and miniflare/workerd minor versions match.

@petebacondarwin petebacondarwin changed the title refactor: remove duplicated getWranglerSendMetricsFromEnv() function feat: support outputing ND-JSON files via an environment variable Jul 31, 2024
@petebacondarwin petebacondarwin added the e2e Run e2e tests on a PR label Jul 31, 2024
@petebacondarwin petebacondarwin force-pushed the add-output-file branch 2 times, most recently from 0163cb9 to 4ccaa37 Compare July 31, 2024 09:51
@petebacondarwin petebacondarwin force-pushed the add-output-file branch 3 times, most recently from 4241d6b to b985b36 Compare August 5, 2024 20:52
writeOutput({
type: "deployment",
version: 1,
worker_id: name,
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Comment on lines +21 to +22
| "WRANGLER_OUTPUT_FILE_DIRECTORY"
| "WRANGLER_OUTPUT_FILE_PATH";
Copy link
Contributor

Choose a reason for hiding this comment

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

To reduce API surface, can we make this one env var WRANGLER_OUTPUT_PATH? Like WRANGLER_LOG_PATH (.log) if it ends with .json we assume it's a filepath otherwise we assume it's a directory path

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That feels somewhat magical? The surface is basically the same it is just that the user needs to know that specifying .json on the end makes a difference to the behaviour. What if they decide they want to use .ndjson or something instead?

Comment on lines 366 to 373
if (deploymentId) {
writeOutput({
type: "deployment",
version: 1,
worker_id: name,
deployment_id: deploymentId,
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you intended to have something like this for versions upload in versions/index.ts (which matches this deploy/deploy.ts + deploy/index.ts file structure)

command_line_args: ["--help"],
});
const outputFile = readFileSync(WRANGLER_OUTPUT_FILE_PATH, "utf8");
expect(outputFile).toContainEntries([
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this custom matcher necessary? To understand what it's doing, you'd have to go looking for the definition whereas if it was a plain function call (expect(parseEntries(outputFile)).toEqual([...) you could just cmd+click (parseEntries) to get to the implementation

Copy link
Contributor

Choose a reason for hiding this comment

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

I think custom matchers are great for common use-cases to a whole codebase and should be introduced globally so they can be defined clearly, learnt and relied upon. Implementing them as one-offs per suite I think is obfuscating

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It just a helper function to avoid duplicating the same logic throughout and keeping the tests themselves nice and readable.

It is only used in this file (where it is defined) and it makes no sense outside of this file. It is nicer than using a vanilla function because Vitest treats it specially, for example it will format the errors nicely and do diffing of the actual and expected results.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you Cmd-Click on the function it takes you to the type definition below, which is exactly next to the implementation. So I don't think it is too hard to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Somewhat ironically, when you create global custom matchers like this it is common to put the type in a .d.ts file - e.g. https://github.com/cloudflare/workers-sdk/blob/f6300a71adb83e5f92cd9c9ddd56a85132e03f98/packages/create-cloudflare/vitest.d.ts

In this case it is actually much harder to find the original implementation of the matcher. For example the toExist() matcher implementation is over here: https://github.com/cloudflare/workers-sdk/blob/cc335ee1c1dbd58009714c4b49e23e9dcf121d1e/packages/create-cloudflare/e2e-tests/setup.ts

@@ -275,6 +277,15 @@ export async function versionsUploadHandler(
tag: args.tag,
message: args.message,
});

if (versionId && name) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does this need && name but the same code in deploy/deploy.ts doesn't? Not sure which is correct but I think they should be the same, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is because the check for whether the name is defined or not (causing an error to be thrown) is deep inside the versionsUpload() function. So it should always be defined by the time it gets here. But I felt it was too large a refactor to pull that out.

Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't that the same for deploy? Both call getScriptName. Both should be consistent.

writeOutput({
type: "deployment",
version: 1,
worker_id: name,
Copy link
Member

Choose a reason for hiding this comment

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

Something I was just chatting with the team about - It would be a lot better for us to get a stable ID for the worker. Ideally we'd use the script_tag - Would that be possible?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The script_tag is internal to EWC and not made available to Wrangler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All the APIs that Wrangler hits use the Worker name and not the ID. AFAIK.

Copy link
Member

Choose a reason for hiding this comment

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

Wrangler does use script_tag for fetching deployments

).default_environment.script.tag;
The problem with using name for us is that users can update the name in dash and then all of our internal references would be out-of-sync

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So basically someone has to make this call:

fetchResult<ServiceMetadataRes>(`/accounts/${accountId}/workers/services/${scriptName}`)).default_environment.script.tag;

I guess that Wrangler could do it since it will also have the authentication tokens etc.

I'll update to get that.

Copy link
Contributor

Choose a reason for hiding this comment

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

deploy() already makes that call https://github.com/cloudflare/workers-sdk/blob/main/packages/wrangler/src/deploy/deploy.ts so you can add it to the returned object

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @RamIdeas

@@ -361,6 +363,15 @@ export async function deployHandler(
experimentalVersions: args.experimentalVersions,
});

if (deploymentId) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahah. So in that case I made the entry allow worker_id to be undefined!

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 can do that too for version uploads I guess.

@petebacondarwin petebacondarwin requested a review from jrf0110 August 7, 2024 11:22
@petebacondarwin petebacondarwin merged commit 05082ad into main Aug 7, 2024
20 checks passed
@petebacondarwin petebacondarwin deleted the add-output-file branch August 7, 2024 14:12
@workers-devprod workers-devprod mentioned this pull request Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e2e Run e2e tests on a PR
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants