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

WIP: Open Source Telemetry #2363

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from
Open

WIP: Open Source Telemetry #2363

wants to merge 15 commits into from

Conversation

MatanYadaev
Copy link
Collaborator

@MatanYadaev MatanYadaev commented May 9, 2024

PR Type

enhancement


Description

  • Integrated Supabase client for telemetry to track user logins in local-auth.guard.ts.
  • Added new environment variables to manage telemetry features.
  • Updated pnpm-lock.yaml and package.json to include necessary Supabase packages.

Changes walkthrough 📝

Relevant files
Enhancement
local-auth.guard.ts
Integrate Supabase for Telemetry Tracking on Logins           

services/workflows-service/src/auth/local/local-auth.guard.ts

  • Added telemetry tracking for user logins using Supabase.
  • Conditionally logs login URLs if telemetry is enabled.
  • +15/-0   
    Configuration changes
    env.ts
    Add Telemetry Configuration Environment Variables               

    services/workflows-service/src/env.ts

    • Added environment variables for telemetry configuration.
    +7/-0     
    Dependencies
    pnpm-lock.yaml
    Update Dependencies for Supabase Integration                         

    pnpm-lock.yaml

    • Added dependencies related to Supabase for telemetry features.
    +72/-2   
    package.json
    Add Supabase JS SDK to Package Dependencies                           

    services/workflows-service/package.json

    • Added @supabase/supabase-js to package dependencies.
    +1/-0     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Summary by CodeRabbit

    • New Features
      • Enhanced telemetry capabilities: Users can now enable or disable telemetry which logs infrastructure details for improved service monitoring and analytics.
      • Integration with Supabase for storing telemetry data, allowing for better tracking and analysis of application performance.
      • New entry point script (entrypoint.sh) that gathers and outputs infrastructure details in JSON format to improve visibility into the application's environment.
      • New environment variables for telemetry configuration added to the application.
    • Bug Fixes
      • Improved error handling during telemetry data insertion to ensure reliable logging and tracking.
    • Documentation
      • Updated configuration settings related to telemetry, ensuring users can customize their telemetry preferences.

    Copy link

    changeset-bot bot commented May 9, 2024

    ⚠️ No Changeset found

    Latest commit: c5b4c22

    Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

    This PR includes no changesets

    When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

    Click here to learn what changesets are, and how to add one.

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

    Copy link
    Contributor

    coderabbitai bot commented May 9, 2024

    Walkthrough

    The recent updates involve modifications to the Dockerfile and the introduction of a new entrypoint.sh script for the workflows service. The Dockerfile now specifies the entrypoint.sh as the entry point for the production stage, replacing the previous command. The entrypoint.sh script gathers infrastructure details and outputs them in JSON format. Additionally, changes to main.ts include the integration of the Supabase client for telemetry data logging, enhancing the application's functionality while maintaining existing configurations. New environment variables related to telemetry have also been added across multiple configuration files.

    Changes

    File Path Change Summary
    services/workflows-service/Dockerfile Added entrypoint.sh as the entry point; removed CMD.
    services/workflows-service/entrypoint.sh New script to gather infrastructure details and output JSON.
    services/workflows-service/src/main.ts Added Supabase client import and telemetry data logging.
    deploy/.env Added new environment variables for telemetry.
    deploy/docker-compose-build.yml Included telemetry environment variables for the service.
    services/workflows-service/.env.example Added new environment variables for telemetry.
    services/workflows-service/src/auth/local/local-auth.guard.ts Modified LocalAuthGuard to include telemetry logging.

    Possibly related PRs

    • bal 2027 fix build image ee include sentry #2385: The changes in the Dockerfile related to the build image and the introduction of the SHORT_SHA argument may connect with the modifications in the main PR, which also involves updates to the Dockerfile.
    • fix: base64 for hashing key #2429: The addition of environment variables related to telemetry in the .env files and Docker configurations aligns with the telemetry enhancements in the main PR, which introduces new environment variables for telemetry functionality.
    • Ability to store customer secrets and consume them in workflows #2569: The introduction of secret management capabilities in the workflows service may relate to the changes in the main PR, particularly if the entrypoint.sh script interacts with these secrets.
    • feat(analytics): add posthog #2610: The integration of PostHog for analytics may relate to the telemetry aspects introduced in the main PR, enhancing the overall monitoring capabilities of the workflows service.
    • Posthog leftovers #2612: The modifications to the environment schema for PostHog integration could connect with the telemetry enhancements in the main PR, as both involve improving analytics and monitoring.

    Suggested reviewers

    • alonp99
    • MatanYadaev

    🐇✨
    In the code's woven nest,
    A new feature finds its quest.
    With a hop and a skip, logs are kept,
    In Supabase's embrace, securely slept.
    Cheers to changes, big and small,
    May our data journey never stall!
    🌟🐾


    Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

    ❤️ Share
    🪧 Tips

    Chat

    There are 3 ways to chat with CodeRabbit:

    • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
      • I pushed a fix in commit <commit_id>, please review it.
      • Generate unit testing code for this file.
      • Open a follow-up GitHub issue for this discussion.
    • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
      • @coderabbitai generate unit testing code for this file.
      • @coderabbitai modularize this function.
    • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
      • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
      • @coderabbitai read src/utils.ts and generate unit testing code.
      • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
      • @coderabbitai help me debug CodeRabbit configuration file.

    Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

    CodeRabbit Commands (Invoked using PR comments)

    • @coderabbitai pause to pause the reviews on a PR.
    • @coderabbitai resume to resume the paused reviews.
    • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
    • @coderabbitai full review to do a full review from scratch and review all the files again.
    • @coderabbitai summary to regenerate the summary of the PR.
    • @coderabbitai resolve resolve all the CodeRabbit review comments.
    • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
    • @coderabbitai help to get help.

    Other keywords and placeholders

    • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
    • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
    • Add @coderabbitai anywhere in the PR title to generate the title automatically.

    CodeRabbit Configuration File (.coderabbit.yaml)

    • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
    • Please see the configuration documentation for more information.
    • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

    Documentation and Community

    • Visit our Documentation for detailed information on how to use CodeRabbit.
    • Join our Discord Community to get help, request features, and share feedback.
    • Follow us on X/Twitter for updates and announcements.

    @github-actions github-actions bot added the enhancement New feature or request label May 9, 2024
    Copy link
    Contributor

    github-actions bot commented May 9, 2024

    PR Description updated to latest commit (e6d8242)

    Copy link
    Contributor

    github-actions bot commented May 9, 2024

    PR Review 🔍

    ⏱️ Estimated effort to review [1-5]

    3, because the PR involves integrating a new external service (Supabase) for telemetry, which requires careful review of security, configuration, and proper usage of the API. The changes are moderate in size but high in impact, especially concerning data handling and environment configuration.

    🧪 Relevant tests

    No

    ⚡ Possible issues

    Possible Bug: The code does not handle potential exceptions from the Supabase client operations, which could lead to unhandled promise rejections if the network request fails or if there's a configuration error.

    Security Concern: Storing and transmitting API keys and URLs in environment variables without clear documentation on securing these can lead to security vulnerabilities.

    🔒 Security concerns

    No explicit vulnerabilities are introduced in the code changes themselves, but the use of environment variables for sensitive information like API keys necessitates ensuring that these are securely managed.

    Code feedback:
    relevant fileservices/workflows-service/src/auth/local/local-auth.guard.ts
    suggestion      

    Consider wrapping the Supabase client operations in a try-catch block to handle potential errors gracefully. This will prevent the application from crashing due to unhandled exceptions. [important]

    relevant lineawait SupabaseClient.schema('public')

    relevant fileservices/workflows-service/src/auth/local/local-auth.guard.ts
    suggestion      

    It's a good practice to abstract the telemetry logic into a separate service or module. This would improve modularity and make the code easier to manage and test. [important]

    relevant lineconst SupabaseClient = createClient(

    relevant fileservices/workflows-service/src/env.ts
    suggestion      

    Ensure that the environment variables related to telemetry (API keys, URLs) are documented on how to secure them, possibly using secrets management tools. [important]

    relevant lineTELEMETRY_SUPABASE_API_KEY: z.string().optional().describe('Supabase API key for telemetry'),

    relevant fileservices/workflows-service/src/auth/local/local-auth.guard.ts
    suggestion      

    To enhance security, validate the fullUrl constructed from the request to ensure it does not expose sensitive information or is manipulated in a harmful way. [medium]

    relevant lineconst fullUrl = `${request.protocol}://${request.get('Host')}${request.originalUrl}`;

    Copy link
    Contributor

    github-actions bot commented May 9, 2024

    PR Code Suggestions ✨

    CategorySuggestions                                                                                                                                                       
    Performance
    Move the Supabase client creation to a separate service to improve performance.

    Consider moving the creation of the Supabase client to a separate service or module to
    avoid creating a new instance on every request. This will improve performance by reusing
    the client and reduce the overhead of establishing new connections frequently.

    services/workflows-service/src/auth/local/local-auth.guard.ts [15-17]

    -const SupabaseClient = createClient(
    -  env.TELEMETRY_SUPABASE_URL,
    -  env.TELEMETRY_SUPABASE_API_KEY,
    -);
    +// SupabaseClient is now initialized in a dedicated service module
    +const SupabaseClient = supabaseService.getClient();
     
    Best practice
    Add error handling for the Supabase insert operation.

    Add error handling for the Supabase insert operation to manage potential failures
    gracefully, such as logging the error or retrying the operation.

    services/workflows-service/src/auth/local/local-auth.guard.ts [22-24]

    -await SupabaseClient.schema('public')
    -  .from('logins')
    -  .insert([{ url: fullUrl }]);
    +try {
    +  await SupabaseClient.schema('public')
    +    .from('logins')
    +    .insert([{ url: fullUrl }]);
    +} catch (error) {
    +  console.error('Failed to log telemetry data:', error);
    +}
     
    Destructure environment variables for clarity and reduced redundancy.

    Use environment variable destructuring at the beginning of the function to improve code
    clarity and reduce repeated access to the env object.

    services/workflows-service/src/auth/local/local-auth.guard.ts [14-17]

    -if (env.TELEMETRY_ENABLED && env.TELEMETRY_SUPABASE_URL && env.TELEMETRY_SUPABASE_API_KEY) {
    +const { TELEMETRY_ENABLED, TELEMETRY_SUPABASE_URL, TELEMETRY_SUPABASE_API_KEY } = env;
    +if (TELEMETRY_ENABLED && TELEMETRY_SUPABASE_URL && TELEMETRY_SUPABASE_API_KEY) {
       const SupabaseClient = createClient(
    -    env.TELEMETRY_SUPABASE_URL,
    -    env.TELEMETRY_SUPABASE_API_KEY,
    +    TELEMETRY_SUPABASE_URL,
    +    TELEMETRY_SUPABASE_API_KEY,
       );
     
    Maintainability
    Extract URL construction into a utility function.

    Extract the URL construction logic into a separate utility function to improve code
    readability and reusability.

    services/workflows-service/src/auth/local/local-auth.guard.ts [20]

    -const fullUrl = `${request.protocol}://${request.get('Host')}${request.originalUrl}`;
    +const fullUrl = getRequestFullUrl(request);
     
    Enhancement
    Simplify the TELEMETRY_ENABLED environment variable handling by using a boolean type.

    Consider using a boolean type directly for the TELEMETRY_ENABLED environment variable
    instead of using an enum with a transform, to simplify the configuration.

    services/workflows-service/src/env.ts [81-84]

    -TELEMETRY_ENABLED: z
    -  .enum(['true', 'false'])
    -  .default('true')
    -  .transform(value => value === 'true')
    +TELEMETRY_ENABLED: z.boolean().default(true)
     

    Copy link
    Contributor

    @coderabbitai coderabbitai bot left a comment

    Choose a reason for hiding this comment

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

    Actionable comments posted: 1

    Review Details

    Configuration used: CodeRabbit UI
    Review profile: CHILL

    Commits Files that changed from the base of the PR and between 64ac06c and e6d8242.
    Files ignored due to path filters (1)
    • pnpm-lock.yaml is excluded by !pnpm-lock.yaml
    Files selected for processing (3)
    • services/workflows-service/package.json (1 hunks)
    • services/workflows-service/src/auth/local/local-auth.guard.ts (2 hunks)
    • services/workflows-service/src/env.ts (1 hunks)
    Files skipped from review due to trivial changes (1)
    • services/workflows-service/package.json
    Additional comments not posted (3)
    services/workflows-service/src/auth/local/local-auth.guard.ts (2)

    4-5: Ensure that the imports from @supabase/supabase-js and '@/env' are used appropriately in the file.


    26-26: The return statement correctly passes the result from the parent canActivate method. Good use of explicit type casting to boolean.

    services/workflows-service/src/env.ts (1)

    81-87: The new telemetry-related environment variables are well-defined with appropriate validations and descriptions. Ensure that these variables are documented in the project's environment configuration guide.

    Copy link
    Contributor

    @coderabbitai coderabbitai bot left a comment

    Choose a reason for hiding this comment

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

    Actionable comments posted: 0

    Review Details

    Configuration used: CodeRabbit UI
    Review profile: CHILL

    Commits Files that changed from the base of the PR and between e6d8242 and 4d21e90.
    Files selected for processing (1)
    • services/workflows-service/src/auth/local/local-auth.guard.ts (2 hunks)
    Files skipped from review as they are similar to previous changes (1)
    • services/workflows-service/src/auth/local/local-auth.guard.ts

    Copy link
    Contributor

    @coderabbitai coderabbitai bot left a comment

    Choose a reason for hiding this comment

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

    Actionable comments posted: 7

    🧹 Outside diff range and nitpick comments (1)
    services/workflows-service/src/main.ts (1)

    41-41: Use consistent variable naming conventions

    The variable SupabaseClient is instantiated as an object but is named with an initial uppercase letter, which is typically reserved for classes or constructors in JavaScript/TypeScript. Consider renaming it to supabaseClient for consistency.

    -const SupabaseClient = createClient(
    +const supabaseClient = createClient(
    📜 Review details

    Configuration used: CodeRabbit UI
    Review profile: CHILL

    📥 Commits

    Files that changed from the base of the PR and between 4d21e90 and 603fdc2.

    📒 Files selected for processing (3)
    • services/workflows-service/Dockerfile (1 hunks)
    • services/workflows-service/entrypoint.sh (1 hunks)
    • services/workflows-service/src/main.ts (2 hunks)
    🧰 Additional context used
    🪛 Shellcheck
    services/workflows-service/entrypoint.sh

    [warning] 65-65: Quotes/backslashes will be treated literally. Use an array.

    (SC2089)


    [warning] 68-68: Quotes/backslashes in this variable will not be respected.

    (SC2090)

    🔇 Additional comments (3)
    services/workflows-service/Dockerfile (2)

    35-39: Summary of Dockerfile changes

    The modifications to this Dockerfile align well with the PR objectives for integrating telemetry:

    1. The addition of entrypoint.sh provides a mechanism for gathering infrastructure details and initializing telemetry.
    2. The switch from CMD to ENTRYPOINT allows for more flexible container startup and potential runtime argument passing.

    These changes enhance the application's capabilities while maintaining a clean Docker build process. Ensure that the entrypoint.sh script is properly implemented to handle both telemetry initialization and application startup.


    39-39: Changed container startup method

    The ENTRYPOINT instruction has replaced the previous CMD instruction. This change provides more flexibility in how the container starts and allows for runtime argument passing.

    Let's verify the contents of the entrypoint.sh script to ensure it's properly set up:

    ✅ Verification successful

    ENTRYPOINT Configuration Verified

    🏁 Scripts executed

    The following scripts were executed for the analysis:

    Script:

    #!/bin/bash
    # Display the contents of entrypoint.sh
    if [ -f services/workflows-service/entrypoint.sh ]; then
      cat services/workflows-service/entrypoint.sh
    else
      echo "entrypoint.sh does not exist"
    fi
    

    Length of output: 1744

    services/workflows-service/src/main.ts (1)

    38-38: Avoid logging sensitive data

    Logging infradata may expose sensitive or confidential information. Ensure that logging this data complies with security policies, or consider removing the log statement.

    [security]

    -console.log(infradata);

    services/workflows-service/Dockerfile Show resolved Hide resolved
    services/workflows-service/entrypoint.sh Show resolved Hide resolved
    services/workflows-service/src/main.ts Outdated Show resolved Hide resolved
    services/workflows-service/src/main.ts Outdated Show resolved Hide resolved
    Copy link
    Contributor

    @coderabbitai coderabbitai bot left a comment

    Choose a reason for hiding this comment

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

    Actionable comments posted: 2

    🧹 Outside diff range and nitpick comments (1)
    services/workflows-service/src/main.ts (1)

    Line range hint 20-48: Summary of telemetry implementation review

    The introduction of telemetry using Supabase is a valuable addition to the application. However, there are several areas for improvement:

    1. Infradata initialization: Consider using a more robust and configurable approach for reading the JSON file.
    2. Asynchronous execution: Move the telemetry code into an async function to avoid top-level await issues.
    3. Error handling: Implement proper error handling for both file reading and Supabase operations.
    4. Code organization: Consider moving the telemetry initialization to a separate module or service for better maintainability.

    Addressing these points will significantly improve the robustness and maintainability of the telemetry implementation.

    📜 Review details

    Configuration used: CodeRabbit UI
    Review profile: CHILL

    📥 Commits

    Files that changed from the base of the PR and between 603fdc2 and d599a3f.

    📒 Files selected for processing (1)
    • services/workflows-service/src/main.ts (2 hunks)
    🧰 Additional context used
    🔇 Additional comments (1)
    services/workflows-service/src/main.ts (1)

    20-20: LGTM: Supabase client import.

    The import statement for the Supabase client is correctly placed and uses named import, which is good for tree-shaking.

    services/workflows-service/src/main.ts Outdated Show resolved Hide resolved
    services/workflows-service/src/main.ts Outdated Show resolved Hide resolved
    Copy link
    Contributor

    @coderabbitai coderabbitai bot left a comment

    Choose a reason for hiding this comment

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

    Actionable comments posted: 1

    📜 Review details

    Configuration used: CodeRabbit UI
    Review profile: CHILL

    📥 Commits

    Files that changed from the base of the PR and between d599a3f and ec9e80f.

    📒 Files selected for processing (1)
    • services/workflows-service/src/main.ts (2 hunks)
    🧰 Additional context used
    🔇 Additional comments (3)
    services/workflows-service/src/main.ts (3)

    20-20: LGTM: Supabase client import.

    The import statement for the Supabase client is correctly placed and uses named import, which is good for tree-shaking.


    39-39: ⚠️ Potential issue

    Improve infradata initialization for better error handling and conditional loading.

    The current approach of using require with an absolute path may lead to portability issues and lacks error handling. Consider the following improvements:

    1. Use a configurable path or environment variable instead of a hardcoded absolute path.
    2. Implement error handling for file reading operations.
    3. Load infradata only when telemetry is enabled to optimize performance.

    Example implementation:

    import * as fs from 'fs';
    import * as path from 'path';
    
    let infradata;
    if (env.TELEMETRY_ENABLED) {
      try {
        const infraPath = process.env.INFRA_DATA_PATH || '/tmp/infra.json';
        const rawData = fs.readFileSync(path.resolve(infraPath), 'utf8');
        infradata = JSON.parse(rawData);
      } catch (error) {
        console.error('Error reading infra.json:', error);
        // Handle the error appropriately
      }
    }

    This approach addresses the concerns raised in the previous comments and improves the overall robustness of the code.


    50-55: ⚠️ Potential issue

    Refactor telemetry data insertion for proper error handling and asynchronous execution.

    The current implementation has several issues:

    1. It uses an undefined supabase variable, which will cause a runtime error.
    2. The insertion is performed outside the telemetry enabled condition.
    3. It uses await at the top level, which can cause syntax errors in CommonJS modules.
    4. Error logging to console may not be suitable for production.

    Consider refactoring the telemetry implementation as follows:

    const initializeTelemetry = async () => {
      if (env.TELEMETRY_ENABLED && env.TELEMETRY_SUPABASE_URL && env.TELEMETRY_SUPABASE_API_KEY) {
        try {
          const supabaseClient = createClient(
            env.TELEMETRY_SUPABASE_URL,
            env.TELEMETRY_SUPABASE_API_KEY,
            {
              db: { schema: 'public' },
            },
          );
          const { error } = await supabaseClient.from('infra').insert([infradata]);
          if (error) {
            logger.error('Error inserting infradata into Supabase:', error);
          } else {
            logger.info('Telemetry data inserted successfully');
          }
        } catch (error) {
          logger.error('Unexpected error during Supabase operation:', error);
        }
      }
    };
    
    // Call this function at the beginning of the main function
    const main = async () => {
      await initializeTelemetry();
      // ... rest of the main function
    };

    This refactored version addresses the issues by:

    1. Moving the telemetry code inside an async function.
    2. Adding proper error handling for Supabase operations.
    3. Using the application logger instead of console.
    4. Calling the telemetry initialization at the beginning of the main function.

    services/workflows-service/src/main.ts Outdated Show resolved Hide resolved
    Copy link
    Contributor

    @coderabbitai coderabbitai bot left a comment

    Choose a reason for hiding this comment

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

    Actionable comments posted: 5

    📜 Review details

    Configuration used: CodeRabbit UI
    Review profile: CHILL

    📥 Commits

    Files that changed from the base of the PR and between ec9e80f and df51d4c.

    📒 Files selected for processing (3)
    • services/workflows-service/Dockerfile (1 hunks)
    • services/workflows-service/entrypoint.sh (1 hunks)
    • services/workflows-service/src/main.ts (2 hunks)
    🚧 Files skipped from review as they are similar to previous changes (1)
    • services/workflows-service/src/main.ts
    🧰 Additional context used
    🪛 Shellcheck
    services/workflows-service/entrypoint.sh

    [warning] 65-65: Quotes/backslashes will be treated literally. Use an array.

    (SC2089)


    [warning] 68-68: Quotes/backslashes in this variable will not be respected.

    (SC2090)

    🔇 Additional comments (4)
    services/workflows-service/Dockerfile (2)

    35-35: LGTM: Entrypoint script integration looks good

    The changes to copy the entrypoint.sh script and set it as the ENTRYPOINT for the production stage are correct and align with the PR objectives for integrating telemetry. This setup allows for gathering infrastructure details and initializing the application with telemetry capabilities.

    Also applies to: 39-39


    Line range hint 21-21: Clarification on CMD instruction in dev stage

    The CMD instruction [ "dumb-init", "npm", "run", "dev", "--host" ] is still present in the development stage. This is correct and doesn't conflict with the new ENTRYPOINT in the production stage. The CMD instruction is used for development purposes, while the ENTRYPOINT is used for the production environment.

    services/workflows-service/entrypoint.sh (2)

    47-49: LGTM: Current time retrieval is correct

    The get_current_time function correctly retrieves the current UTC time in ISO 8601 format. This is a standard and widely compatible approach.


    59-70: ⚠️ Potential issue

    Fix JSON construction and file writing

    The current implementation of JSON construction and file writing has several issues:

    1. The JSON construction using single quotes doesn't properly handle variable expansion.
    2. The echo command for writing to the file doesn't use quotes, which could lead to word splitting and globbing.

    Apply the following changes to fix these issues:

    -infra_json='{"cloudProvider":"'"$cloud_provider"'","tool":"'"$dep_tool"'","hostname":"'"$hostname"'", "currentTime": "'"$currentTime"'"}'
    -echo "$infra_json"
    -echo $infra_json > $infra_file
    +infra_json=$(jq -n \
    +  --arg cp "$cloud_provider" \
    +  --arg dt "$dep_tool" \
    +  --arg hn "$hostname" \
    +  --arg ct "$currentTime" \
    +  '{"cloudProvider":$cp,"tool":$dt,"hostname":$hn,"currentTime":$ct}')
    +echo "$infra_json"
    +echo "$infra_json" > "$infra_file"

    This change uses jq to construct the JSON, which properly handles escaping and quoting. It also ensures that the echo command uses quotes when writing to the file. If jq is not available in your environment, you can use the printf method suggested in the previous review comment.

    To ensure jq is available in your environment, you can run:

    ✅ Verification successful

    JSON construction and file writing are correctly implemented

    The jq command is available in the environment, ensuring proper JSON construction and safe file writing as suggested.

    🏁 Scripts executed

    The following scripts were executed for the analysis:

    Script:

    #!/bin/bash
    if ! command -v jq &> /dev/null; then
        echo "jq is not installed. Please install it or use the printf method for JSON construction."
        exit 1
    fi
    

    Length of output: 15

    🧰 Tools
    🪛 Shellcheck

    [warning] 65-65: Quotes/backslashes will be treated literally. Use an array.

    (SC2089)


    [warning] 68-68: Quotes/backslashes in this variable will not be respected.

    (SC2090)

    services/workflows-service/entrypoint.sh Show resolved Hide resolved
    services/workflows-service/entrypoint.sh Show resolved Hide resolved
    services/workflows-service/entrypoint.sh Show resolved Hide resolved
    services/workflows-service/entrypoint.sh Show resolved Hide resolved
    services/workflows-service/entrypoint.sh Outdated Show resolved Hide resolved
    @ballerine-io ballerine-io deleted a comment from pratapalakshmi Oct 27, 2024
    Copy link
    Contributor

    @coderabbitai coderabbitai bot left a comment

    Choose a reason for hiding this comment

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

    Actionable comments posted: 6

    🧹 Outside diff range and nitpick comments (4)
    services/workflows-service/.env.example (1)

    37-39: Consider improving telemetry configuration defaults and documentation.

    1. Consider changing TELEMETRY_ENABLED default to false to make it opt-in rather than opt-out, which is a better practice for privacy-conscious users.
    2. Add comments explaining:
      • The purpose of these variables
      • Required format for TELEMETRY_SUPABASE_URL
      • Instructions on where to obtain TELEMETRY_SUPABASE_API_KEY

    Apply this diff to improve the configuration:

    -TELEMETRY_ENABLED=true
    -TELEMETRY_SUPABASE_URL=""
    -TELEMETRY_SUPABASE_API_KEY=""
    +# Configuration for anonymous usage tracking (optional)
    +# Set to true to enable telemetry collection
    +TELEMETRY_ENABLED=false
    +# Your Supabase project URL (e.g., https://your-project.supabase.co)
    +TELEMETRY_SUPABASE_URL=""
    +# Your Supabase API key (get it from your project's API settings)
    +TELEMETRY_SUPABASE_API_KEY=""
    services/workflows-service/src/auth/local/local-auth.guard.ts (1)

    17-20: Optimization: Remove redundant schema configuration

    The schema configuration in the client options is redundant since it's already specified in the query using from().

    Apply this diff:

    -          {
    -            db: { schema: 'public' },
    -          },
    deploy/docker-compose-build.yml (1)

    62-64: LGTM with security recommendations.

    The telemetry environment variables are correctly configured. However, consider these security improvements:

    1. Mark the API key as a secret using Docker Compose secrets management
    2. Add validation for these variables in the application startup
    3. Consider adding comments to document the expected format of these variables

    Here's how you could implement secrets management:

    services:
      ballerine-workflow-service:
        environment:
          TELEMETRY_ENABLED: ${TELEMETRY_ENABLED}
          TELEMETRY_SUPABASE_URL: ${TELEMETRY_SUPABASE_URL}
        secrets:
          - telemetry_api_key
    
    secrets:
      telemetry_api_key:
        file: ./secrets/telemetry_api_key.txt
    services/workflows-service/src/main.ts (1)

    38-58: Consider moving telemetry logic to a dedicated service.

    The telemetry initialization and data insertion logic would be better placed in a dedicated NestJS service. This would improve:

    1. Separation of concerns
    2. Testability
    3. Reusability
    4. Configuration management

    Consider creating a TelemetryService class:

    @Injectable()
    export class TelemetryService {
      private readonly supabaseClient: SupabaseClient | undefined;
      
      constructor(
        private readonly configService: ConfigService,
        private readonly logger: AppLoggerService,
      ) {
        this.initializeClient();
      }
    
      private initializeClient() {
        if (
          this.configService.get('TELEMETRY_ENABLED') &&
          this.configService.get('TELEMETRY_SUPABASE_URL') &&
          this.configService.get('TELEMETRY_SUPABASE_API_KEY')
        ) {
          this.supabaseClient = createClient(
            this.configService.get('TELEMETRY_SUPABASE_URL')!,
            this.configService.get('TELEMETRY_SUPABASE_API_KEY')!,
            {
              db: { schema: 'public' },
            },
          );
        }
      }
    
      async trackInfrastructureData() {
        if (!this.supabaseClient) return;
        
        // Implementation here
      }
    }
    📜 Review details

    Configuration used: CodeRabbit UI
    Review profile: CHILL

    📥 Commits

    Files that changed from the base of the PR and between df51d4c and f65a598.

    📒 Files selected for processing (5)
    • deploy/.env (1 hunks)
    • deploy/docker-compose-build.yml (1 hunks)
    • services/workflows-service/.env.example (1 hunks)
    • services/workflows-service/src/auth/local/local-auth.guard.ts (1 hunks)
    • services/workflows-service/src/main.ts (2 hunks)
    🔇 Additional comments (4)
    deploy/.env (1)

    20-22: ⚠️ Potential issue

    Consider disabling telemetry by default and add documentation.

    1. For privacy considerations, it's recommended to set TELEMETRY_ENABLED=false by default, allowing users to explicitly opt-in.
    2. Please add comments explaining:
      • The purpose of each telemetry variable
      • Required format for TELEMETRY_SUPABASE_URL
      • How to obtain the TELEMETRY_SUPABASE_API_KEY

    Let's verify the integration of these variables:

    services/workflows-service/.env.example (1)

    37-39: Verify error handling for empty telemetry credentials.

    Please ensure proper error handling exists when telemetry is enabled but the URL or API key is empty to prevent runtime errors.

    Let's verify the error handling in the code:

    ✅ Verification successful

    Based on the search results, I can now generate the final response since I have enough information about how empty telemetry credentials are handled.

    Error handling for empty telemetry credentials is properly implemented

    The code has appropriate safeguards in place:

    • Environment variables are validated using Zod schema with .optional() for URL and API key
    • Telemetry client initialization is wrapped in a condition that checks all three required values: if (env.TELEMETRY_ENABLED && env.TELEMETRY_SUPABASE_URL && env.TELEMETRY_SUPABASE_API_KEY)
    • The initialization is also wrapped in try-catch blocks to handle runtime errors
    🏁 Scripts executed

    The following scripts were executed for the analysis:

    Script:

    #!/bin/bash
    # Description: Check how empty telemetry credentials are handled
    
    # Test: Search for telemetry initialization code
    rg -A 10 "TELEMETRY_ENABLED" 
    
    # Test: Search for error handling patterns around Supabase client initialization
    ast-grep --pattern 'new createClient($URL, $KEY)' 
    

    Length of output: 3917

    services/workflows-service/src/auth/local/local-auth.guard.ts (1)

    4-5: LGTM: Import statements are clean and necessary.

    services/workflows-service/src/main.ts (1)

    20-20: LGTM: Import statement is correctly placed.

    The Supabase client import is properly added and the package is included in the project dependencies.

    deploy/docker-compose-build.yml Outdated Show resolved Hide resolved
    services/workflows-service/src/main.ts Outdated Show resolved Hide resolved
    services/workflows-service/src/main.ts Outdated Show resolved Hide resolved
    services/workflows-service/src/main.ts Outdated Show resolved Hide resolved
    Comment on lines +53 to +55
    if (error) {
    this.logger.error(`Failed to log infra data: ${error.message}`);
    } else {
    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    for error handling please use SentryModule and notify error

    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    do same for other previous function as well

    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    Can you give a reference in workflow-service i can refer and close it

    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    sentryService.captureException(error);

    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.

    4 participants