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

feat: ability to fetch logs from instances #564

Merged
merged 2 commits into from
Sep 23, 2024

Conversation

mojtaba-esk
Copy link
Member

@mojtaba-esk mojtaba-esk commented Sep 19, 2024

Closes #496

Summary by CodeRabbit

  • New Features

    • Introduced logging tests to validate functionality for both standalone and sidecar instances.
    • Added methods to retrieve log streams for specific containers in Kubernetes ReplicaSets.
    • Enhanced monitoring capabilities with new methods for retrieving logs based on instance types.
  • Bug Fixes

    • Improved log retrieval logic to ensure accurate output for different instance configurations.

@mojtaba-esk mojtaba-esk requested a review from a team September 19, 2024 14:53
@mojtaba-esk mojtaba-esk self-assigned this Sep 19, 2024
Copy link
Contributor

coderabbitai bot commented Sep 19, 2024

Walkthrough

The pull request introduces enhanced logging functionality and corresponding tests within the Knuu framework. It adds methods for retrieving log streams from both instances and Kubernetes containers, along with tests to validate the logging behavior of standalone instances and those with sidecars. These changes improve the monitoring capabilities of the system by allowing flexible log retrieval based on instance types.

Changes

File Change Summary
e2e/basic/logs_test.go Added two test functions, TestLogs and TestLogsWithSidecar, to validate logging functionality for instances and sidecars. Introduced sidecarLogsTest type with methods for initializing and managing sidecar instances.
pkg/instance/monitoring.go Introduced a Logs method in the monitoring struct to retrieve log streams based on whether an instance is a sidecar or not.
pkg/k8s/logs.go Added a GetLogStream method in the Client struct to retrieve log streams from a specific container in a Kubernetes ReplicaSet.
pkg/k8s/types.go Enhanced the KubeManager interface by adding the GetLogStream method for retrieving log streams from specified containers within pods.

Poem

In the land of logs, where bunnies hop,
New streams are flowing, they never stop.
With sidecars dancing, and tests in play,
Our Knuu framework shines bright today!
Hooray for the changes, let’s give a cheer,
For every log message we hold so dear! 🐇✨


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1d76518 and 994b731.

Files selected for processing (1)
  • e2e/basic/logs_test.go (1 hunks)
Additional comments not posted (6)
e2e/basic/logs_test.go (6)

22-48: LGTM!

The test function TestLogs is implemented correctly. It creates a new instance, sets the image and start command to generate logs, starts the instance, waits for a short duration, fetches the logs, and verifies that the expected log message is present.


50-78: LGTM!

The test function TestLogsWithSidecar is implemented correctly. It creates a new instance, sets the image and start command, adds a sidecar instance, starts the instance, waits for a short duration, fetches the logs from the sidecar instance, and verifies that the expected log message is present.


80-101: LGTM!

The Initialize method of the sidecarLogsTest struct is implemented correctly. It creates a new instance, sets it as a sidecar, sets the image and start command to generate logs, and commits the changes.


103-108: LGTM!

The PreStart method of the sidecarLogsTest struct is implemented correctly. It checks if the instance is initialized before starting and returns an error if the instance is not initialized.


110-112: LGTM!

The Instance method of the sidecarLogsTest struct is implemented correctly. It returns the instance field.


114-122: LGTM!

The Clone method of the sidecarLogsTest struct is implemented correctly. It clones the instance with a new name prefix using the CloneWithName() method and returns a new sidecarLogsTest struct with the cloned instance.


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.

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

Outside diff range and nitpick comments (4)
e2e/basic/logs_test.go (4)

49-49: Consider using a more reliable waiting mechanism.

The test currently waits for a fixed duration of 5 seconds to allow log generation. This may not be reliable in all scenarios, especially if log generation takes longer than expected.

Consider using a more dynamic waiting mechanism, such as polling for the presence of the expected log message or waiting for a specific log event, to make the test more resilient to timing issues.


55-56: Consider using a more efficient way to read the log stream.

The test currently uses io.ReadAll to read the entire log stream into memory. This may cause performance issues for large log outputs.

Consider using a more efficient approach, such as reading the log stream in chunks or using a scanner to read the logs line by line, to handle large log outputs more effectively.


86-86: Consider using a more reliable waiting mechanism.

The test currently waits for a fixed duration of 5 seconds to allow log generation. This may not be reliable in all scenarios, especially if log generation takes longer than expected.

Consider using a more dynamic waiting mechanism, such as polling for the presence of the expected log message or waiting for a specific log event, to make the test more resilient to timing issues.


92-93: Consider using a more efficient way to read the log stream.

The test currently uses io.ReadAll to read the entire log stream into memory. This may cause performance issues for large log outputs.

Consider using a more efficient approach, such as reading the log stream in chunks or using a scanner to read the logs line by line, to handle large log outputs more effectively.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4ede783 and 1d76518.

Files selected for processing (4)
  • e2e/basic/logs_test.go (1 hunks)
  • pkg/instance/monitoring.go (2 hunks)
  • pkg/k8s/logs.go (1 hunks)
  • pkg/k8s/types.go (2 hunks)
Additional comments not posted (4)
pkg/k8s/logs.go (1)

10-23: LGTM!

The GetLogStream function is well-implemented and follows best practices. It enhances the client's capabilities by enabling log retrieval for specific containers within ReplicaSets. The function signature, parameter handling, error handling, and resource management are all properly done.

Some key points:

  • The function takes appropriate parameters (context, ReplicaSet name, and container name) to identify the specific container for log retrieval.
  • It handles the case when the container name is not provided by setting an empty container name in the PodLogOptions.
  • It retrieves the first pod associated with the specified ReplicaSet using GetFirstPodFromReplicaSet and performs proper error handling.
  • It uses the Kubernetes client to create a log request and stream the logs efficiently.
  • The function returns the log stream as an io.ReadCloser, allowing the caller to read the logs and close the stream when done, promoting proper resource management.

Overall, this function is a valuable addition to the client and provides a convenient way to access logs for debugging and monitoring purposes.

pkg/instance/monitoring.go (1)

22-27: LGTM!

The Logs function is a great addition to the monitoring system. It enhances the flexibility and usability of the logging feature by handling log retrieval differently based on the instance type. The implementation follows good practices by:

  • Correctly determining the parameters for GetLogStream based on whether the instance is a sidecar or not.
  • Returning the log stream as an io.ReadCloser which is suitable for streaming data.
  • Propagating errors from GetLogStream to the caller for appropriate error handling.

Overall, this is a well-designed and implemented function.

pkg/k8s/types.go (2)

5-5: LGTM!

The import statement for the io package is correctly added. This is a standard library package in Go that provides basic interfaces to I/O primitives.


54-54: Approve the new method addition to the interface.

The GetLogStream method is a valuable addition to the KubeManager interface. It is well-defined and follows the Go conventions. The method parameters and return types are appropriately specified.

This method will enable retrieving log streams from specified containers within pods, which enhances the monitoring and debugging capabilities in Kubernetes environments.

Note that this is a breaking change for any existing implementations of the KubeManager interface. They will need to be updated to implement this new method.

Copy link
Member

@smuu smuu left a comment

Choose a reason for hiding this comment

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

Neat solution 🚀
Only two small things in the tests.

e2e/basic/logs_test.go Outdated Show resolved Hide resolved
e2e/basic/logs_test.go Show resolved Hide resolved
@mojtaba-esk mojtaba-esk merged commit 81328f6 into main Sep 23, 2024
12 checks passed
@mojtaba-esk mojtaba-esk deleted the mojtaba/feat-ability-to-fetch-logs-from-instances branch September 23, 2024 06:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Ability to fetch logs from instances
3 participants