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

[LogStreaming] App logs command #4041

Open
wants to merge 30 commits into
base: main
Choose a base branch
from

Conversation

mssalemi
Copy link
Contributor

@mssalemi mssalemi commented Jun 12, 2024

Part of the LogStreaming Prototype project.

#gsd:39776

Current:

 NODE_TLS_REJECT_UNAUTHORIZED=0  SHOPIFY_CLI_ENABLE_APP_LOG_POLLING=1 pnpm shopify app logs --path ~/code/log-streaming/mehdi-local-cli --json --status=failure --source=product-discount

WHY are these changes introduced?

We are adding the app logs command. This command is similar to dev, but will stream logging events with full detail. It will not write logs to file. See the Nick's DX doc for more details.

Log Output

Screenshot 2024-06-13 at 10 02 19 AM

Error State
Screenshot 2024-06-12 at 5 41 30 PM

WHAT is this pull request doing?

The <Logs /> component will handle both the polling, and displaying the output.

The log and subscribe process are now only responsible for making the requests. This processes are used in the Logs component and are results are handling by either adding the log to the output, or displaying the errors.

TLDR on the error handling:

If errors are 401 -> we do not update the polling interval, we will re-subscribe. Manage this in the react component by setting the JWT state to null, so the next time it polls, it will re-subscribe first.

If errors are 429 -> update the polling interval to 60 seconds. We also show the error state and a retry in 60 seconds message.

If errors are we update the retry interval to 5 second, and display the error message.

If errors are not handled, they will get thrown. This will cause the terminal to abort.

If there are app logs, they are displayed to the CLI through the Static component. This component is also used by dev's ConcurrentOutput.

How to test your changes?

Checkout this branch

Run:

NODE_TLS_REJECT_UNAUTHORIZED=0  SHOPIFY_CLI_ENABLE_APP_LOG_POLLING=1 pnpm shopify app logs --path ~/code/log-streaming/mehdi-local-cli

Optionally:
(https://github.com/Shopify/partners/pull/55154 needed)

--status=failure --source=extension-handle

Post-release steps

Measuring impact

How do we know this change was effective? Please choose one:

  • n/a - this doesn't need measurement, e.g. a linting rule or a bug-fix
  • Existing analytics will cater for this addition
  • PR includes analytics changes to measure impact

Checklist

  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've considered possible documentation changes
  • I've made sure that any changes to dev or deploy have been reflected in the internal flowchart.

Copy link
Contributor

Thanks for your contribution!

Depending on what you are working on, you may want to request a review from a Shopify team:

  • Themes: @shopify/advanced-edits
  • UI extensions: @shopify/ui-extensions-cli
    • Checkout UI extensions: @shopify/checkout-ui-extensions-api-stewardship
  • Hydrogen: @shopify/hydrogen
  • Other: @shopify/app-management

Copy link
Contributor

github-actions bot commented Jun 12, 2024

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements
72.45% (-0.17% 🔻)
7485/10331
🟡 Branches
69.11% (-0.11% 🔻)
3676/5319
🟡 Functions
71.59% (-0.01% 🔻)
1983/2770
🟡 Lines
72.77% (-0.2% 🔻)
7066/9710
Show new covered files 🐣
St.
File Statements Branches Functions Lines
🔴
... / logs.ts
0% 0% 0% 0%
🔴
... / logs.ts
0% 0% 0% 0%
🟢
... / poll-app-logs.ts
100% 82.35% 100% 100%
🔴
... / ui.tsx
0% 100% 0% 0%
🟢
... / Logs.tsx
100% 87.5% 100% 100%
🟢
... / usePollAppLogs.ts
94.29% 92.86% 80% 93.55%
Show files with reduced coverage 🔻
St.
File Statements Branches Functions Lines
🔴
... / flow_template.ts
33.33% (-5.13% 🔻)
0%
33.33% (+13.33% 🔼)
33.33% (-5.13% 🔻)
🟢
... / utils.ts
86.36% (-8.37% 🔻)
75% (-5% 🔻)
100%
86.36% (-8.37% 🔻)
🟢
... / poll-app-logs.ts
88.37% (-3.01% 🔻)
81.25%
44.44% (-10.1% 🔻)
88.37% (-3.01% 🔻)
🟢
... / breakdown-extensions.ts
96.55% (-0.14% 🔻)
83.33% 100% 100%
🟢
... / upload.ts
84.09% (-0.87% 🔻)
78.57% (-1.98% 🔻)
85.37%
84.87% (-0.96% 🔻)
🟢
... / replay.ts
97.06% (+0.57% 🔼)
80% (-6.96% 🔻)
100%
97.06% (+0.57% 🔼)

Test suite run success

1723 tests passing in 800 suites.

Report generated by 🧪jest coverage report action from 73001ce

@mssalemi mssalemi force-pushed the ms.app-logs-command-version2-no-refactor branch 2 times, most recently from d976073 to 1586bed Compare June 12, 2024 21:29
Copy link
Contributor

@DuncanUszkay1 DuncanUszkay1 left a comment

Choose a reason for hiding this comment

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

Mainly just some nits on my end, though I'd want CLI folks to take a look before I put the ✅ since there are some elements in here that look like they should probably be generic 🤔

packages/app/src/cli/services/context.ts Outdated Show resolved Hide resolved
packages/app/src/cli/commands/app/logs.ts Outdated Show resolved Hide resolved
packages/app/src/cli/commands/app/logs.ts Outdated Show resolved Hide resolved
packages/app/src/cli/commands/app/logs.ts Outdated Show resolved Hide resolved
packages/app/src/cli/commands/app/logs.ts Outdated Show resolved Hide resolved
packages/app/src/cli/commands/app/logs.ts Outdated Show resolved Hide resolved
packages/app/src/cli/services/context.ts Outdated Show resolved Hide resolved
@mssalemi mssalemi force-pushed the ms.app-logs-command-version2-no-refactor branch 5 times, most recently from 796cc88 to 68adb58 Compare June 14, 2024 12:24
@mssalemi mssalemi force-pushed the ms.app-logs-command-version2-no-refactor branch from ddff241 to a982bee Compare June 18, 2024 01:32
@mssalemi mssalemi changed the base branch from main to lopert.polling-refactor June 18, 2024 01:32
@mssalemi mssalemi force-pushed the ms.app-logs-command-version2-no-refactor branch 3 times, most recently from a697f66 to 2b6fef6 Compare June 18, 2024 14:04
Base automatically changed from lopert.polling-refactor to main June 18, 2024 14:45
@mssalemi mssalemi force-pushed the ms.app-logs-command-version2-no-refactor branch from f4a6cf0 to d2614e5 Compare June 20, 2024 18:25
@mssalemi mssalemi force-pushed the ms.app-logs-command-version2-no-refactor branch from f056e4d to 0eff461 Compare June 20, 2024 19:00
logTimestamp: log.log_timestamp,
}

setAppLogOutputs((prev) => [...prev, {appLog, prefix}])
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious if we need to keep track of every event we've ever seen 🤔 Not urgent but something to explore as a fast-follow

Copy link
Contributor

Choose a reason for hiding this comment

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

Since we're using React, which is state-based rendering, I suspect that we need to keep an array of all lines that we want to render on the screen.

Copy link
Contributor

@DuncanUszkay1 DuncanUszkay1 Jun 20, 2024

Choose a reason for hiding this comment

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

That's true in the browser, but is it true with ink? Does dev keep track of every line it's ever printed? If not, wouldn't we be able to do something similar?

Copy link
Contributor

Choose a reason for hiding this comment

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

Might just be a gap in my understanding of ink

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW The output component dev (and this) uses does keep the entire output in state -- it doesn't do virtualisation or anything like that

@mssalemi mssalemi marked this pull request as ready for review June 20, 2024 19:22
Copy link
Contributor

We detected some changes at either packages/*/src or packages/cli-kit/assets/cli-ruby/** and there are no updates in the .changeset.
If the changes are user-facing, run "pnpm changeset add" to track your changes and include them in the next release CHANGELOG.

@mssalemi mssalemi enabled auto-merge June 20, 2024 19:23
packages/app/src/cli/commands/app/logs.ts Outdated Show resolved Hide resolved
packages/app/src/cli/commands/app/logs.ts Outdated Show resolved Hide resolved
packages/app/src/cli/services/app-logs/types.ts Outdated Show resolved Hide resolved
logTimestamp: log.log_timestamp,
}

setAppLogOutputs((prev) => [...prev, {appLog, prefix}])
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we're using React, which is state-based rendering, I suspect that we need to keep an array of all lines that we want to render on the screen.

<Text color="green">{prefix.logTimestamp}</Text>
<Text color="blueBright">{`${prefix.source}`}</Text>
<Text color={prefix.status === 'Success' ? 'green' : 'red'}>{prefix.status}</Text>
<Text>in {prefix.fuelConsumed} M instructions</Text>
Copy link
Contributor

Choose a reason for hiding this comment

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

❔ Should we keep the prefix generic for other app log types? In this case, should the prefix actually have something like description (set to the instruction count for Functions) or something that we just blindly output here?

@mssalemi mssalemi force-pushed the ms.app-logs-command-version2-no-refactor branch from 0a7b159 to d5f09aa Compare June 20, 2024 21:09
Copy link
Contributor

@shauns shauns left a comment

Choose a reason for hiding this comment

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

This is looking better now, but still a few things to pick up. I think Andrew's round of review in particular does need to be actioned before merging.

packages/app/src/cli/commands/app/logs.ts Outdated Show resolved Hide resolved
env: 'SHOPIFY_FLAG_SOURCE',
}),
status: Flags.string({
description: 'Filters output to the specified status (success or failure).',
Copy link
Contributor

Choose a reason for hiding this comment

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

If these are fixed strings, lets use options as per the above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yea, the status will be fixed to success or failure, whereas the source is the extension handle. Maybe we could do @nickwesselman?

  static descriptionWithMarkdown = `
  Opens a real-time stream of detailed log events from the selected app and store.
  Use the \`--source\` argument to limit output to a particular function, such as a specific Shopify Function handle.
  Use the \`--status\` argument to specify the type of status to retrieve, either \`success\` or \`failure\`.
  \`\`\`
  shopify app logs --status=success --source=extension-handle
  \`\`\`
  `

We will update the flags to use the options for status.

packages/app/src/cli/commands/app/logs.ts Outdated Show resolved Hide resolved
Comment on lines 10 to 15
cursor?: string
errors?: {
status: number
message: string
}[]
appLogs?: AppLogData[]
Copy link
Contributor

Choose a reason for hiding this comment

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

+1


// eslint-disable-next-line @typescript-eslint/no-floating-promises
poll()
}, [])
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to handle unmount here -- cancelling the timeout?

@mssalemi mssalemi added this pull request to the merge queue Jun 21, 2024
@shauns shauns removed this pull request from the merge queue due to a manual request Jun 21, 2024
@mssalemi mssalemi changed the title [LogStreaming] App logs command (without refactor) [LogStreaming] App logs command Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants