Skip to content

feat: optionally track telemetry for the main commands #614

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

Merged
merged 5 commits into from
Jan 14, 2025
Merged

Conversation

roderik
Copy link
Member

@roderik roderik commented Jan 14, 2025

Summary by Sourcery

Add telemetry to the create, connect, codegen, login, and logout commands.

New Features:

  • Track telemetry for the main commands, which allows SettleMint to collect data on how the CLI is used, while allowing users to opt out by setting the SETTLEMINT_DISABLE_TELEMETRY environment variable.

Tests:

  • Add integration tests for telemetry.

Copy link
Contributor

sourcery-ai bot commented Jan 14, 2025

Reviewer's Guide by Sourcery

This pull request implements optional telemetry tracking for the main commands. It introduces a new telemetry utility function that sends telemetry data to the specified instance. The function is called at the end of each command execution, with the command name and status. The telemetry data includes the command name, status, workspace, and application information. If any error occurs during the command execution, the error message is also included in the telemetry data.

Sequence diagram for telemetry tracking flow

Loading
sequenceDiagram
    participant User
    participant CLI
    participant SettleMint

    User->>CLI: Execute command (connect/login/create/etc)
    activate CLI
    Note over CLI: Execute command logic
    alt Success case
        CLI->>SettleMint: POST /cm/telemetry
        Note right of CLI: {command, status: 'success', workspace, application}
    else Error case
        CLI->>SettleMint: POST /cm/telemetry
        Note right of CLI: {command, status: 'error', message, workspace, application}
    end
    CLI-->>User: Command result
    deactivate CLI

File-Level Changes

Change Details Files
Added telemetry tracking to the connect command.
  • Wrapped the command's action in a try-catch block to capture errors.
  • Added a telemetry call at the end of the command execution to track success.
  • Added a telemetry call in the catch block to track errors including error messages
sdk/cli/src/commands/connect.ts
Added telemetry tracking to the login command.
  • Wrapped the command's action in a try-catch block to capture errors.
  • Added a telemetry call at the end of the command execution to track success and the instance URL.
  • Added a telemetry call in the catch block to track errors including error messages.
sdk/cli/src/commands/login.ts
Added telemetry tracking to the codegen command.
  • Wrapped the command's action in a try-catch block to capture errors.
  • Added a telemetry call at the end of the command execution to track success.
  • Added a telemetry call in the catch block to track errors including error messages.
sdk/cli/src/commands/codegen.ts
Added telemetry tracking to the create command.
  • Wrapped the command's action in a try-catch block to capture errors.
  • Added a telemetry call at the end of the command execution to track success and the selected template.
  • Added a telemetry call in the catch block to track errors including error messages.
sdk/cli/src/commands/create.ts
Added telemetry tracking to the logout command.
  • Wrapped the command's action in a try-catch block to capture errors.
  • Added a telemetry call at the end of the command execution to track success and the instance URL.
  • Added a telemetry call in the catch block to track errors including error messages.
sdk/cli/src/commands/logout.ts
Created a new telemetry utility function.
  • Created a function that sends telemetry data to the specified instance.
  • The function accepts command name, status, message, prod flag, and instance URL as parameters.
  • The function sends a POST request to the /cm/telemetry endpoint with the telemetry data.
  • The function includes workspace and application information in the telemetry data if available.
  • The function handles errors and timeouts gracefully.
  • Added a check for the SETTLEMINT_DISABLE_TELEMETRY environment variable to disable telemetry if set.
sdk/cli/src/utils/telemetry.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions github-actions bot added the feat label Jan 14, 2025
Copy link

github-actions bot commented Jan 14, 2025

📦 Packages

Package NPM Docker
SDK Cli @settlemint/sdk-cli@1.0.0-pra3873949
SDK The Graph @settlemint/sdk-thegraph@1.0.0-pra3873949
SDK Portal @settlemint/sdk-portal@1.0.0-pra3873949
SDK Hasura @settlemint/sdk-hasura@1.0.0-pra3873949
SDK JS @settlemint/sdk-js@1.0.0-pra3873949
SDK Utils @settlemint/sdk-utils@1.0.0-pra3873949
SDK Next @settlemint/sdk-next@1.0.0-pra3873949
SDK Minio @settlemint/sdk-minio@1.0.0-pra3873949
SDK IPFS @settlemint/sdk-ipfs@1.0.0-pra3873949
SDK Blockscout @settlemint/sdk-blockscout@1.0.0-pra3873949

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @roderik - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟡 Review instructions: 1 issue found
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 59 to 68
personalAccessToken = await Promise.race([
(async () => {
const chunks: Buffer[] = [];
for await (const chunk of process.stdin) {
chunks.push(Buffer.from(chunk));
}
return Buffer.concat(chunks).toString().trim();
})(),
new Promise<string>((resolve) => setTimeout(() => resolve(""), 1_000)),
]);
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: 1-second timeout for stdin might be too short in some environments.

Consider making the timeout configurable or providing a more user-friendly error message if the timeout is reached.

Suggested change
personalAccessToken = await Promise.race([
(async () => {
const chunks: Buffer[] = [];
for await (const chunk of process.stdin) {
chunks.push(Buffer.from(chunk));
}
return Buffer.concat(chunks).toString().trim();
})(),
new Promise<string>((resolve) => setTimeout(() => resolve(""), 1_000)),
]);
const STDIN_TIMEOUT_MS = process.env.STDIN_TIMEOUT_MS ? parseInt(process.env.STDIN_TIMEOUT_MS, 10) : 5_000;
personalAccessToken = await Promise.race([
(async () => {
const chunks: Buffer[] = [];
for await (const chunk of process.stdin) {
chunks.push(Buffer.from(chunk));
}
return Buffer.concat(chunks).toString().trim();
})(),
new Promise<string>((resolve, reject) =>
setTimeout(() =>
reject(new Error(`Timeout waiting for token input via STDIN after ${STDIN_TIMEOUT_MS}ms. Set STDIN_TIMEOUT_MS environment variable to adjust this timeout.`)),
STDIN_TIMEOUT_MS
)
),
]);

promises.push(codegenIpfs(env));
}
const results = await Promise.allSettled(promises);
if (results.some((r) => r.status === "rejected")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Error handling in Promise.allSettled could be more detailed.

Consider providing more detailed information about which specific task failed to aid in debugging.

Suggested implementation:

          const results = await Promise.allSettled(promises);
          const failures = results
            .map((result, index) => ({ result, index }))
            .filter(({ result }) => result.status === "rejected")
            .map(({ result, index }) => `Task ${index + 1}: ${(result as PromiseRejectedResult).reason}`);

          if (failures.length > 0) {
            cancel(`Errors occurred while generating resources:\n${failures.join('\n')}`);
          }

For even better error handling, you might want to:

  1. Add a mapping of promise index to task name/description when creating the promises array
  2. Use that mapping in the error message instead of just showing "Task 1", "Task 2", etc.

application: env.SETTLEMINT_APPLICATION,
}),
}).catch(() => {}), // Swallow fetch errors
new Promise((resolve) => setTimeout(resolve, 500)), // Timeout after 500ms
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (review_instructions): Potential performance issue with 500ms timeout in telemetry function.

The 500ms timeout in the telemetry function might introduce unnecessary delays. Consider reducing the timeout duration to improve performance.

Review instructions:

Path patterns: **/*.ts

Instructions:
Always write correct, up to date, bug free, fully functional and working, secure, performant and efficient code.

fix
fix
fix
fix
@roderik roderik merged commit d8f085b into main Jan 14, 2025
2 checks passed
@roderik roderik deleted the feat/telemetry branch January 14, 2025 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant