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

pwa-kit-dev command for tailing logs #789

Merged
merged 59 commits into from
Nov 7, 2022
Merged

pwa-kit-dev command for tailing logs #789

merged 59 commits into from
Nov 7, 2022

Conversation

raiyaj
Copy link
Contributor

@raiyaj raiyaj commented Oct 20, 2022

Adds the ability to tail logs for deployed Managed Runtime environments!

Description

Types of Changes

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Documentation update
  • Breaking change (could cause existing functionality to not work as expected)
  • Other changes (non-breaking changes that does not fit any of the above)

Breaking changes include:

  • Removing a public function or component or prop
  • Adding a required argument to a function
  • Changing the data type of a function parameter or return value
  • Adding a new peer dependency to package.json

Changes

  • Incorporate Oli's managedRuntimeCommand helper into the pwa-kit-dev CLI to make sharing common options like cloudOrigin and credentialsFile easier, and use it for push and save-credentials
  • Add a tail-logs command
  • Add a tail-logs npm script to each template's package.json
  • Add/update tests
  • Upgrade commander's major version

How to Test-Drive This PR

  • Ensure your production API key is in ~/.mobify

  • Checkout this branch

  • If you haven't done so already, install lerna, then run npm ci at the root of the repo

  • Tail logs! Run node packages/pwa-kit-dev/bin/pwa-kit-dev.js tail-logs -p <slug> -e <slug> for an environment in production. Once you generate some traffic on the environment, you should see something like this:

    Screen Shot 2022-11-01 at 11 17 43 AM
  • Other things to try:

    • Make sure the command's help text includes the project, environment, cloud origin, and credentials file options
    • Start tailing, but don't open the environment URL until > 10 minutes later (the idle timeout). The connection should remain open and stream the logs once you open it.

Checklists

General

  • Changes are covered by test cases
  • CHANGELOG.md updated with a short description of changes (not required for documentation updates)

Accessibility Compliance

You must check off all items in one of the follow two lists:

  • There are no changes to UI

or...

Localization

  • Changes include a UI text update in the Retail React App (which requires translation)

@salesforce-cla
Copy link

Thanks for the contribution! Unfortunately we can't verify the commit author(s): Mahdi Soleimani <m***@s***.com>. One possible solution is to add that email to your GitHub account. Alternatively you can change your commits to another email and force push the change. After getting your commits associated with your GitHub account, refresh the status of this Pull Request.

}
})
.action(async (_, opts) => {
const {project, environment, cloudApiBase, credentialsFile} = opts.optsWithGlobals()
Copy link
Contributor

Choose a reason for hiding this comment

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

Really nice – I like that optsWithGlobals makes the order of the args insignificant. Couldn't work out how to do this myself the other day.

There are a set of commands in here for which these "global" flags can never be of interest – basically anything that is not an "MRT Command" (build, lint, etc).

I think that's harmless, but I came up with this mrtCommand helper here that might be worth thinking about.

The only advantage I can think of with the mrtCommand approach is that the --cloudURLBase still appear in the command-specific help. Here's what we see with your approach now:

(venv) ➜  pwa-kit-dev git:(tail-logs) ./bin/pwa-kit-dev.js logs --help
Usage: pwa-kit-dev logs [options]

tail environment logs

Options:
  -p, --project <project_slug>          the project slug
  -e, --environment <environment_slug>  the environment slug
  -h, --help                            display help for command

It's a nice-to-have.

Copy link
Contributor Author

@raiyaj raiyaj Oct 20, 2022

Choose a reason for hiding this comment

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

Interesting, I like the mrtCommand idea, I'll take a shot at refactoring the commands to use it (though you've done most of that in your PR :)). I think your point about the global options not appearing for command-specific help is important, cause I also made credentialsFile global, which means I've taken it out of the push and save-credentials help. And the wrapper seems similar to what other people are doing to get around that problem.

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 also like the fancy color-coded logs you have there! I wanna give that a try too :)

}

const token = await scriptUtils.createToken(project, environment, cloudApiBase, credentials.api_key)
const url = new URL(cloudApiBase.replace('cloud', 'logs'))
Copy link
Contributor

Choose a reason for hiding this comment

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

Probs want to validate that URL actually looks "cloud-ish" if you're going to replace like that!

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'll add validation to the option definition itself 😀

@olibrook
Copy link
Contributor

@raiyaj I think what you have here looks excellent. I think there's an open question about --tail.

If we do --tail now, my question is do we have a plan to do anything other than --tail in the CLI in future? What would an implementation of pwa-kit-dev logs be now, without the --tail option?

I think y'all were discussing showing the --help text. I mean, fine, if we have other plans for a --tail-free version of the command. If we don't have plans, I can't help thinking that it's just 6 extra characters to type every single time, forever!

What do other tools do? I feel like heroku logs without --tail used to show you one "page" of logs, no?

@raiyaj
Copy link
Contributor Author

raiyaj commented Oct 21, 2022

my question is do we have a plan to do anything other than --tail in the CLI in future?

@olibrook Way back we talked about adding the ability to query past logs using the CLI, but since we have no plans to implement it anytime soon, I agree that the --tail option is kinda redundant since that's the only functionality we're offering.

What do other tools do? I feel like heroku logs without --tail used to show you one "page" of logs, no?

I checked heroku logs and aws logs tail, and you're right - both display recent logs and only poll for new logs with an extra --tail or --follow arg.

@johnboxall 's point about --tail generally being a long-running program that exits on a signal seems valid too. For now I'm going to add the --tail arg and show help without it, but will get team feedback before merging.

@salesforce-cla
Copy link

Thanks for the contribution! It looks like @kenjush is an internal user so signing the CLA is not required. However, we need to confirm this.

@@ -61,7 +61,7 @@
"babel-plugin-dynamic-import-node-babel-7": "^2.0.7",
"babel-plugin-formatjs": "10.2.20",
"chalk": "^4.1.2",
"commander": "^8.3.0",
"commander": "^9.3.0",
Copy link
Contributor Author

@raiyaj raiyaj Nov 1, 2022

Choose a reason for hiding this comment

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

I kept this library upgrade from the initial spike. I noticed PWA Kit team's 3PP approval for this lib is only for v2.20.3 (?!), so I'll look into requesting approval for v9.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -6,7 +6,7 @@
"packages": {
"": {
"name": "pwa-kit-dev",
"version": "2.3.0-dev",
"version": "2.4.0-dev",
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'm guessing this is leftover from the recent 2.3.0-dev release.

@raiyaj raiyaj marked this pull request as ready for review November 1, 2022 18:56
@raiyaj raiyaj requested a review from a team as a code owner November 1, 2022 18:56
@@ -184,7 +224,7 @@ const main = () => {
'a message to include along with the uploaded bundle in Managed Runtime'
)
// The default message is loaded dynamically as part of `uploadBundle(...)`
.default(undefined, '<git branch>:<git commit hash>')
.default(null, '<git branch>:<git commit hash>')
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 changed this to null because I noticed that when it's undefined, the default message is not shown in the help output because of this.

Comment on lines +15 to +20
beforeEach(() => {
realFail = Utils.fail
Utils.fail.mockImplementation((errorMessage) => {
throw new Error(errorMessage)
})
})
Copy link
Contributor Author

@raiyaj raiyaj Nov 1, 2022

Choose a reason for hiding this comment

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

At one point a test in this file was failing because we added a new required option to uploadBundle, and it was tripping me up because the option validation has no effect in the tests unless Utils.fail throws an error.

Comment on lines 316 to 323
managedRuntimeCommand('logs')
.description(`display environment logs (without --tail, displays this help text and exits)`)
.addOption(
new program.Option('-p, --project <projectSlug>', 'the project slug').default(
null,
"the 'name' key from package.json"
)
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I find this help text kind of horrendous! Do we have a plan to support pwa-kit-dev logs without --tail soon? You know, in the next few months?

I suspect we don't and we'll be looking at this help text in a year thinking "weird, who thought that was a good idea?"!

Can we do one of:

  1. Rename the command to tail-logs.
  2. Remove the --tail flag and just tail when you run pwa-kit-dev logs, the only thing we have planned.
  3. Keep --tail, but provide some behaviour for pwa-kit-dev logs right away? You could show the first page of logs and then exit, for instance.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you call it tail-logs I think there's an easy way to build a logs command in future, while maintaining backwards compatibility with an alias...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'm convinced. I went with option 1.

Copy link
Contributor

@kenjush kenjush left a comment

Choose a reason for hiding this comment

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

🚢 🚢

@vmarta vmarta merged commit 6f5e79b into develop Nov 7, 2022
@vmarta vmarta deleted the tail-logs branch November 7, 2022 19:28
This was referenced Dec 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants