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][feat]: add scripts to capture telemetry #2289

Closed
wants to merge 1 commit into from

Conversation

pratapalakshmi
Copy link
Collaborator

@pratapalakshmi pratapalakshmi commented Apr 7, 2024

Expected-output:
we shall write the output of info.json and we can push it to any api based on the necessity

root@c552b4b0e106:/app# %
❯ docker exec -it -u root c552b4b0e106 cat info.json
{"cloudProvider":"local","Tool":"docker","Hostname":""}

Type

enhancement


Description

  • Introduced a new Bash script entrypoint.sh to capture telemetry data including cloud provider, deployment tool, and hostname.
  • Updated the Dockerfile to include the new entrypoint.sh script and set it as the ENTRYPOINT, altering the container startup process.

Changes walkthrough

Relevant files
Enhancement
entrypoint.sh
Add Telemetry Capture Script                                                         

services/workflows-service/entrypoint.sh

  • Added a new script entrypoint.sh to capture telemetry data.
  • The script determines the cloud provider, deployment tool (Docker or
    Kubernetes), and hostname.
  • Generates a JSON with cloud provider, deployment tool, and hostname
    information and saves it to /app/info.json.
  • +46/-0   
    Configuration changes
    Dockerfile
    Dockerfile Update to Use New Entrypoint                                   

    services/workflows-service/Dockerfile

  • Added entrypoint.sh to the Docker image.
  • Changed the Dockerfile to use entrypoint.sh as the ENTRYPOINT.
  • +3/-1     

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

    Summary by CodeRabbit

    • Refactor
      • Improved the startup process of the workflows service to enhance compatibility and reliability across different deployment environments.

    Copy link

    changeset-bot bot commented Apr 7, 2024

    ⚠️ No Changeset found

    Latest commit: 338052b

    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

    @pratapalakshmi pratapalakshmi changed the title [feat]: add scripts to capture telemetry [wip][feat]: add scripts to capture telemetry Apr 7, 2024
    Copy link
    Contributor

    coderabbitai bot commented Apr 7, 2024

    Walkthrough

    The recent update introduces an entrypoint.sh script for the workflows-service, enhancing its initialization process. This script now intelligently determines the cloud provider, deployment mode, and gathers essential network information before starting the service with npm run prod. These modifications aim to increase flexibility and operational awareness in diverse deployment environments.

    Changes

    File Path Change Summary
    services/.../Dockerfile Updated to utilize /app/entrypoint.sh as the entrypoint script before executing npm run prod.
    services/.../entrypoint.sh New script added to identify cloud provider, deployment mode, and fetch hostname/IP for execution.

    🐰✨
    In the world of bits, where systems twirl,
    A script emerges, a clever swirl.
    It talks to the clouds, knows their tale,
    Collects the whispers, without fail.
    With a jump and a bound, the scene is set,
    For the service to shine, without a fret.
    🌟🐇


    Recent Review Details

    Configuration used: CodeRabbit UI

    Commits Files that changed from the base of the PR and between 24ff7a8 and 338052b.
    Files selected for processing (2)
    • services/workflows-service/Dockerfile (1 hunks)
    • services/workflows-service/entrypoint.sh (1 hunks)
    Files skipped from review as they are similar to previous changes (1)
    • services/workflows-service/Dockerfile
    Additional comments not posted (8)
    services/workflows-service/entrypoint.sh (8)

    1-1: Ensure the shebang is appropriate for the environment where this script will be executed.


    3-3: Using set -e is good practice as it causes the script to exit immediately if a command exits with a non-zero status.


    5-5: Consider logging more context about the environment, not just the user ID, for better traceability in logs.


    7-19: The function get_cloud_provider effectively determines the cloud provider based on the kernel release details. Ensure that the conditions cover all expected cloud environments.


    21-29: The function get_tool correctly identifies whether the service is running in Docker or Kubernetes. This is crucial for the telemetry setup.


    41-41: Use an array to handle quotes and backslashes correctly when constructing the JSON string. This change was suggested in a previous review and is still valid.


    43-43: Ensure to use double quotes around variables to prevent globbing and word splitting. This change was suggested in a previous review and is still valid.


    45-46: The exec "$@" command is used to handle the CMD command from the Dockerfile. This is a standard practice in entrypoint scripts.


    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>.
      • 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 generate interesting stats about this repository and render them as a table.
      • @coderabbitai show all the console.log statements in this repository.
      • @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.

    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 as PR comments)

    • @coderabbitai pause to pause the reviews on a PR.
    • @coderabbitai resume to resume the paused reviews.
    • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
    • @coderabbitai resolve resolve all the CodeRabbit review comments.
    • @coderabbitai help to get help.

    Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

    CodeRabbit Configration 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 Apr 7, 2024
    Copy link
    Contributor

    github-actions bot commented Apr 7, 2024

    PR Description updated to latest commit (28e36f5)

    Copy link
    Contributor

    github-actions bot commented Apr 7, 2024

    PR Review

    (Review updated until commit 6e1735a)

    ⏱️ Estimated effort to review [1-5]

    3, because the PR involves both a new script addition and changes to the Dockerfile, which requires understanding of bash scripting, Docker container lifecycle, and the application's deployment process.

    🧪 Relevant tests

    No

    🔍 Possible issues

    Possible Bug: The get_username_ip function only captures the hostname but is named as if it should also capture an IP address, which might be misleading.

    Security Concern: Directly echoing variables into a JSON file without any form of validation or escaping could potentially lead to injection vulnerabilities if the variables are somehow tampered with or improperly sanitized.

    🔒 Security concerns

    - Sensitive information exposure: The script captures and stores hostname information, which could be considered sensitive in some contexts. Ensure that storing this information complies with privacy policies and security standards.

    Code feedback:
    relevant fileservices/workflows-service/entrypoint.sh
    suggestion      

    Consider validating or sanitizing the cloud_provider, dep_tool, and hostname variables before echoing them into the JSON file to prevent potential injection vulnerabilities. [important]

    relevant lineecho $info_Json > /app/info.json

    relevant fileservices/workflows-service/entrypoint.sh
    suggestion      

    Rename the get_username_ip function to get_hostname to accurately reflect its functionality, or extend its functionality to also capture the IP address if that was the original intent. [medium]

    relevant linefunction get_username_ip() {

    relevant fileservices/workflows-service/entrypoint.sh
    suggestion      

    Add error handling for the echo $info_Json > /app/info.json command to ensure the script gracefully handles file write errors. [medium]

    relevant lineecho $info_Json > /app/info.json

    relevant fileservices/workflows-service/Dockerfile
    suggestion      

    Ensure that the entrypoint.sh script has executable permissions set either in the Dockerfile using RUN chmod +x /app/entrypoint.sh or before copying it into the Docker image. This is crucial for the script to be executed as an entrypoint. [important]

    relevant lineCOPY --from=dev /app/entrypoint.sh ./entrypoint.sh


    ✨ Review tool usage guide:

    Overview:
    The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

    The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

    • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
    /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
    
    [pr_reviewer]
    some_config1=...
    some_config2=...
    

    See the review usage page for a comprehensive guide on using this tool.

    Copy link
    Contributor

    github-actions bot commented Apr 7, 2024

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Best practice
    Quote variables to prevent word splitting and handle special characters.

    Consider quoting variables to prevent word splitting, especially in conditional statements
    and echo commands. This is a best practice to handle values that might contain spaces or
    special characters.

    services/workflows-service/entrypoint.sh [5-10]

     echo "Running as: $(id)"
    -if [[ $release_details == *"amzn"* ]];then
    +if [[ "$release_details" == *"amzn"* ]];then
     
    Ensure entrypoint script has execute permissions.

    Ensure that the entrypoint script (entrypoint.sh) has execute permissions set before
    building the Docker image. This can be achieved by adding a RUN chmod +x
    /app/entrypoint.sh command after copying the script into the image.

    services/workflows-service/Dockerfile [38]

    +RUN chmod +x /app/entrypoint.sh
     ENTRYPOINT [ "/app/entrypoint.sh" ]
     
    Enhancement
    Add 'set -u' for better error handling of unset variables.

    To ensure that the script fails early and for better debugging, consider adding set -u to
    treat unset variables and parameters other than the special parameters "@" or "*" as an
    error when performing parameter expansion.

    services/workflows-service/entrypoint.sh [3]

    -set -e
    +set -eu
     
    Add a default case for unsupported cloud providers in get_cloud_provider.

    Consider adding a default case in the get_cloud_provider function to handle unexpected or
    unsupported cloud providers. This will improve the script's robustness and provide clearer
    feedback for unsupported environments.

    services/workflows-service/entrypoint.sh [16-17]

     else
    -   echo "Local Deployment";
    +   echo "Unsupported cloud provider or Local Deployment";
     
    Possible issue
    Utilize the hostname variable set in get_username_ip function.

    The function get_username_ip only sets a variable but does not use or echo it. Consider
    echoing the hostname or using it in a meaningful way within the function.

    services/workflows-service/entrypoint.sh [32-34]

     function get_username_ip() {
         hostname=$(cat /etc/hostname)
    +    echo "Hostname: $hostname"
     }
     

    ✨ Improve tool usage guide:

    Overview:
    The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

    • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
    /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
    
    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    

    See the improve usage page for a comprehensive guide on using this tool.

    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 Status

    Configuration used: CodeRabbit UI

    Commits Files that changed from the base of the PR and between be2a0c1 and 28e36f5.
    Files selected for processing (2)
    • services/workflows-service/Dockerfile (1 hunks)
    • services/workflows-service/entrypoint.sh (1 hunks)
    Additional comments not posted (4)
    services/workflows-service/Dockerfile (2)

    38-38: Ensure /app/entrypoint.sh is executable and properly configured within the Docker container.


    40-40: Correctly updated CMD to use dumb-init for better signal handling and process management.

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

    1-40: Ensure the script is robust against potential failures in external commands and consider adding error handling where appropriate.


    40-40: Correctly handles the execution of the CMD command, ensuring the service starts as intended.


    ## Check the hostname and IP
    function get_username_ip() {
    hostname=$(cat /etc/hostname)
    Copy link
    Contributor

    Choose a reason for hiding this comment

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

    The hostname variable appears unused. Consider completing the implementation or removing it if unnecessary.

    Copy link
    Contributor

    PR Description updated to latest commit (6e1735a)

    Copy link
    Contributor

    Persistent review updated to latest commit 6e1735a

    Copy link
    Contributor

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Best practice
    Quote variables to prevent globbing and word splitting.

    It's recommended to quote variables to prevent globbing and word splitting. This is
    particularly important when dealing with variables that may contain spaces or special
    characters. In the echo command, you should quote $info_Json to ensure it's treated as a
    single argument, even if it contains spaces.

    services/workflows-service/entrypoint.sh [43]

    -echo $info_Json > /app/info.json
    +echo "$info_Json" > /app/info.json
     
    Use declare for function-local variables for script safety.

    For better script safety and to avoid unexpected behavior, it's a good practice to use
    declare when initializing your function-local variables. This ensures the variable is
    treated as local to the function, reducing the risk of name conflicts and unintended side
    effects.

    services/workflows-service/entrypoint.sh [9-11]

    -release_details=$(uname -r)
    +local release_details=$(uname -r)
     if [[ $release_details == *"amzn"* ]];then 
    -   cloud_provider="amazon";
    +   local cloud_provider="amazon";
     
    Add error handling for critical operations.

    To ensure that your script can handle errors more gracefully, consider adding error
    handling for critical operations. For example, when capturing the hostname, you can check
    if the /etc/hostname file exists and is readable before attempting to read it. This
    prevents the script from failing silently in environments where the file might not be
    available.

    services/workflows-service/entrypoint.sh [33]

    -hostname=$(cat /etc/hostname)
    +if [ -r /etc/hostname ]; then
    +    hostname=$(cat /etc/hostname)
    +else
    +    echo "Error: /etc/hostname not found or not readable" >&2
    +    exit 1
    +fi
     
    Maintainability
    Use a case statement for clarity and maintainability.

    To improve the maintainability and readability of your script, consider using a case
    statement instead of multiple if-elif statements when determining the cloud_provider. This
    approach makes it easier to add or modify conditions in the future.

    services/workflows-service/entrypoint.sh [10-17]

    -if [[ $release_details == *"amzn"* ]];then 
    -   cloud_provider="amazon";
    -elif [[ $release_details == *"azure"* ]];then
    -   cloud_provider="azure";
    -elif [[ $release_details == *"gcp"* ]];then
    -   cloud_provider="gcp";
    -else
    -   cloud_provider="local";
    -fi
    +case $release_details in
    +*"amzn"*) cloud_provider="amazon" ;;
    +*"azure"*) cloud_provider="azure" ;;
    +*"gcp"*) cloud_provider="gcp" ;;
    +*) cloud_provider="local" ;;
    +esac
     

    ✨ Improve tool usage guide:

    Overview:
    The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

    • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
    /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
    
    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    

    See the improve usage page for a comprehensive guide on using this tool.

    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

    get_username_ip

    # one line
    info_Json='{"cloudProvider":"'"$cloud_provider"'","Tool":"'"$dep_tool"'","Hostname":"'"$hostname"'"}'
    Copy link
    Contributor

    Choose a reason for hiding this comment

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

    Use an array to handle quotes and backslashes correctly when constructing the JSON string.

    - info_Json='{"cloudProvider":"'"$cloud_provider"'","Tool":"'"$dep_tool"'","Hostname":"'"$hostname"'"}'
    + declare -a info_Json=("cloudProvider" "$cloud_provider" "Tool" "$dep_tool" "Hostname" "$hostname")
    + printf -v info_Json '{"%s":"%s","%s":"%s","%s":"%s"}' "${info_Json[@]}"

    Committable suggestion

    ‼️ IMPORTANT
    Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

    Suggested change
    info_Json='{"cloudProvider":"'"$cloud_provider"'","Tool":"'"$dep_tool"'","Hostname":"'"$hostname"'"}'
    declare -a info_Json=("cloudProvider" "$cloud_provider" "Tool" "$dep_tool" "Hostname" "$hostname")
    printf -v info_Json '{"%s":"%s","%s":"%s","%s":"%s"}' "${info_Json[@]}"

    # one line
    info_Json='{"cloudProvider":"'"$cloud_provider"'","Tool":"'"$dep_tool"'","Hostname":"'"$hostname"'"}'

    echo $info_Json > /app/info.json
    Copy link
    Contributor

    Choose a reason for hiding this comment

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

    Ensure to use double quotes around variables to prevent globbing and word splitting.

    - echo $info_Json > /app/info.json
    + echo "$info_Json" > /app/info.json

    Committable suggestion

    ‼️ IMPORTANT
    Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

    Suggested change
    echo $info_Json > /app/info.json
    echo "$info_Json" > /app/info.json

    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

    Comment on lines +31 to +34
    ## Check the hostname and IP
    function get_username_ip() {
    hostname=$(cat /etc/hostname)
    }
    Copy link
    Contributor

    Choose a reason for hiding this comment

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

    The function get_username_ip retrieves the hostname but does not handle IP retrieval as mentioned in the PR objectives. Consider implementing IP address retrieval or updating the function name and documentation to reflect its actual functionality.

    - function get_username_ip() {
    + function get_hostname() {

    Committable suggestion

    ‼️ IMPORTANT
    Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

    Suggested change
    ## Check the hostname and IP
    function get_username_ip() {
    hostname=$(cat /etc/hostname)
    }
    ## Check the hostname and IP
    function get_hostname() {
    hostname=$(cat /etc/hostname)
    }

    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