Skip to content

Send analytics in background - #7615

Open
gonzaloriestra wants to merge 1 commit into
mainfrom
background-analytics
Open

Send analytics in background#7615
gonzaloriestra wants to merge 1 commit into
mainfrom
background-analytics

Conversation

@gonzaloriestra

@gonzaloriestra gonzaloriestra commented May 22, 2026

Copy link
Copy Markdown
Contributor

WHY are these changes introduced?

HackDays project: https://vault.shopify.io/hackdays/154/projects/24279-Shopify-CLI-UX-improvements

Analytics delivery currently blocks command completion, adding noticeable latency to every CLI invocation.

WHAT is this pull request doing?

Writes the completed analytics payload to a temporary file and sends it from a detached internal command, allowing the original command to exit immediately.

This works for Mac and Linux, but on Windows we still have to wait for the process because of a known issue. We also wait on CI to avoid issues with the job teardown.

Benchmark for shopify version:

Version Average time (10 runs)
4.5.0 1.352s
snapshot 0.248s
demo.mp4

How to test your changes?

  • pnpm i -g @shopify/cli@0.0.0-snapshot-20260727091503
  • shopify version --verbose

Checklist

  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've considered possible documentation changes
  • I've considered analytics changes to measure impact
  • The change is user-facing — I've identified the correct bump type (patch for bug fixes · minor for new features · major for breaking changes) and added a changeset with pnpm changeset add

@gonzaloriestra

Copy link
Copy Markdown
Contributor Author

/snapit

@github-actions github-actions Bot added the no-changelog This PR doesn't include a changeset entry. Is an internal only change not relevant to end users. label May 22, 2026
@gonzaloriestra
gonzaloriestra force-pushed the background-analytics branch 3 times, most recently from 972e250 to f72a8d8 Compare May 29, 2026 11:40
@github-actions

Copy link
Copy Markdown
Contributor

This PR seems inactive. If it's still relevant, please add a comment saying so. Otherwise, take no action.
→ If there's no activity within a week, then a bot will automatically close this.
Thanks for helping to improve Shopify's dev tooling and experience.

@gonzaloriestra
gonzaloriestra force-pushed the background-analytics branch 3 times, most recently from 375d96d to a23f004 Compare July 21, 2026 09:45

gonzaloriestra commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

This was referenced Jul 21, 2026
@gonzaloriestra
gonzaloriestra force-pushed the background-analytics branch 2 times, most recently from 0427849 to 603e207 Compare July 27, 2026 08:32
@gonzaloriestra

Copy link
Copy Markdown
Contributor Author

/snapit

@gonzaloriestra

Copy link
Copy Markdown
Contributor Author

/snapit

@github-actions

Copy link
Copy Markdown
Contributor

🫰✨ Thanks @gonzaloriestra! Your snapshot has been published to npm.

Test the snapshot by installing your package globally:

pnpm i -g --@shopify:registry=https://registry.npmjs.org @shopify/cli@0.0.0-snapshot-20260727091503

Caution

After installing, validate the version by running shopify version in your terminal.
If the versions don't match, you might have multiple global instances installed.
Use which shopify to find out which one you are running and uninstall it.

@Shopify Shopify deleted a comment from github-actions Bot Jul 27, 2026
@Shopify Shopify deleted a comment from github-actions Bot Jul 27, 2026
@gonzaloriestra
gonzaloriestra marked this pull request as ready for review July 27, 2026 09:24
@gonzaloriestra
gonzaloriestra requested review from a team as code owners July 27, 2026 09:24
Comment thread packages/cli-kit/src/public/node/analytics.ts Outdated
Comment thread packages/cli-kit/src/public/node/analytics.ts Outdated
@gonzaloriestra
gonzaloriestra force-pushed the background-analytics branch 3 times, most recently from 65e2599 to 4990f80 Compare August 7, 2026 09:29
Comment thread packages/cli/src/index.ts
Comment thread packages/cli-kit/src/public/node/analytics.ts Outdated
Comment thread packages/cli-kit/src/public/node/analytics.ts Outdated
prompt,
},
)
await writeFile(payloadPath, JSON.stringify(fullPayload), {encoding: 'utf8', mode: 0o600, flag: 'wx'})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is writing sensitive data to a file... there is a small posibility that the clenaup fails, or the send-analytics invocation fails... something fails and this file is never deleted.
What can we do to prevent that? 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In theory we delete even on failure paths.

But if that fails, we are reducing the risk by:
- Creating the file in the tmp folder, that should eventually be removed
- Setting the file permissions so that only the user running the CLI can access it (0600)
- Using a random filename

Do you think it's enough? Alternatively, we could try to encrypt or pass the data in a different way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Another option is to clean all the CLI payloads after every run (or old ones at least)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

um, cleaning all files wouldn't be compatible with multiple CLIs running in different terminals (one might delete another's file).

maybe ask Claude/Codex if there is reliable way to encrypt? if not I guess there isn't much we can do

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that's why I suggested to only remove the old ones.

Encrypting looks complex because we need to keep a key somehow. But let me try by passing the data through stdin instead.

@gonzaloriestra
gonzaloriestra force-pushed the background-analytics branch 2 times, most recently from f8326f2 to 853bb21 Compare August 7, 2026 12:52
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Differences in type declarations

We detected differences in the type declarations generated by Typescript for this branch compared to the baseline ('main' branch). Please, review them to ensure they are backward-compatible. Here are some important things to keep in mind:

  • Some seemingly private modules might be re-exported through public modules.
  • If the branch is behind main you might see odd diffs, rebase main into this branch.

New type declarations

We found no new type declarations in this PR

Existing type declarations

packages/cli-kit/dist/private/node/otel-metrics.d.ts
@@ -2,7 +2,7 @@ import { OtelService } from '../../public/node/vendor/otel-js/service/types.js';
 import { DefaultOtelServiceOptions } from '../../public/node/vendor/otel-js/service/DefaultOtelService/DefaultOtelService.js';
 type MetricRecorder = 'console' | {
     type: 'otel';
-    otel: Pick<OtelService, 'record'>;
+    otel: Pick<OtelService, 'getMeterProvider' | 'record'>;
 };
 interface Timing {
     active: number;
packages/cli-kit/dist/public/node/analytics.d.ts
@@ -6,6 +6,7 @@ interface ReportAnalyticsEventOptions {
     errorMessage?: string;
     exitMode: CommandExitMode;
 }
+export declare function sendAnalyticsEventFromStdin(): Promise<void>;
 /**
  * Report an analytics event, sending it off to Monorail -- Shopify's internal analytics service.
  *
packages/cli-kit/dist/public/node/fs.d.ts
@@ -86,6 +86,8 @@ export declare function appendFile(path: string, content: string): Promise<void>
 export declare function appendFileSync(path: string, data: string): void;
 export interface WriteOptions {
     encoding: BufferEncoding;
+    mode?: number;
+    flag?: string;
 }
 /**
  * Writes content to file at path.
packages/cli-kit/dist/public/node/vendor/otel-js/service/types.d.ts
@@ -1,5 +1,5 @@
-import type { Counter, Histogram, MeterProvider, MetricAttributes, MetricOptions, UpDownCounter } from '@opentelemetry/api';
-import type { ViewOptions } from '@opentelemetry/sdk-metrics';
+import type { Counter, Histogram, MetricAttributes, MetricOptions, UpDownCounter } from '@opentelemetry/api';
+import type { MeterProvider, ViewOptions } from '@opentelemetry/sdk-metrics';
 export type CustomMetricLabels<TLabels extends Record<TKeys, MetricAttributes>, TKeys extends string = keyof TLabels & string> = {
     [P in TKeys]: TLabels[P] extends MetricAttributes ? TLabels[P] : never;
 };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog This PR doesn't include a changeset entry. Is an internal only change not relevant to end users.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants