zenml icon indicating copy to clipboard operation
zenml copied to clipboard

Fix GCP step logging

Open adtygan opened this issue 11 months ago β€’ 21 comments

Describe changes

The issue arises because GCS artifacts are immutable Stack Overflow thread. To fix the issue, I suggest a fix based on @strickvl 's idea mentioned below

Alex Strick van Linschoten's Idea

Consider using the logging.StreamHandler facility to temporarily write logs to the remote file (GCS, S3, etc.). Here's an example:

import logging
import fsspec

f = fsspec.open("gs://<<my_gcs_bucket>>/test_log.log", "w")
with f as of:
    log_handler = logging.StreamHandler(of)
    logger = logging.getLogger()  # Root logger
    logger.addHandler(log_handler)
    for i in range(0, 5000):
        logger.warning(f"I'm log line #{i}")
    logger.removeHandler(log_handler)

This approach could fit nicely in the StepLogsStorageContext class.

My Solution

Set buffer size to sys.maxsize and copy the existing contents of the log file to the buffer. This prevents the triggering of the part of code that appends buffer contents to log file once buffer is full and flushes buffer. (reference)

Code to test the change:

Please use GCS stack

import gcsfs
from zenml.client import Client
from zenml.logging.step_logging import StepLogsStorage, StepLogsStorageContext

client = Client()
_ = client.active_stack

TEST_FILE="gs://<<my_gcs_bucket>>/test.txt"

with StepLogsStorageContext(logs_uri=TEST_FILE) as log_storage:
    for i in range(0,10):
        print(f"I'm log line #{i}")

fs = gcsfs.GCSFileSystem()

with fs.open(TEST_FILE, 'r') as f:
    all_of_it = f.read()

print('Final Contents:')
print(all_of_it)

Pre-requisites

Please ensure you have done the following:

  • [x] I have read the CONTRIBUTING.md document.
  • [ ] If my change requires a change to docs, I have updated the documentation accordingly.
  • [ ] I have added tests to cover my changes.
  • [x] I have based my new branch on develop and the open PR is targeting develop. If your branch wasn't based on develop read Contribution guide on rebasing branch to develop.
  • [ ] If my changes require changes to the dashboard, these changes are communicated/requested.

Types of changes

  • [ ] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [x] Breaking change (fix or feature that would cause existing functionality to change)
  • [ ] Other (add details above)

Summary by CodeRabbit

  • Refactor
    • Enhanced the initialization process of StepLogs to support a maximum message limit and improved handling of log file reading errors.

adtygan avatar Mar 16 '24 06:03 adtygan

[!IMPORTANT]

Auto Review Skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository.

To trigger a single review, invoke the @coderabbitai review command.

Walkthrough

The update introduces an enhancement to the logging mechanism within the StepLogs class by incorporating a max_messages parameter for better log message management and adding functionality to recover logs from a specified URI, improving robustness and usability in diverse environments, including cloud storage solutions.

Changes

File Path Change Summary
src/zenml/.../step_logging.py Added max_messages to StepLogsStorage initialization; implemented log recovery from logs_uri.

Related issues

  • zenml-io/zenml#2211: This PR could address the issue by improving log handling and ensuring completeness of logs in cloud storage scenarios, such as GCS.

πŸ‡βœ¨

In the realm of code, where logs do sprawl,
A rabbit hopped, aiming to enthrall.
With messages tamed, and errors so few,
Through cloud and through file, our insights grew.
πŸŒŸπŸ“œ

"Fear not," it whispered, "for I've found a way,
To keep our logs safe, come what may."
πŸŒˆπŸš€

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/coderabbit-overrides.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.

coderabbitai[bot] avatar Mar 16 '24 06:03 coderabbitai[bot]

@coderabbitai review

strickvl avatar Mar 16 '24 10:03 strickvl

@adtygan there's a linting error that needs fixing

strickvl avatar Mar 16 '24 11:03 strickvl

@strickvl , I ran the below commands before I made the PR

zenml integration install gcp 
pip install click~=8.0.3
mypy --install-types

After that I ran

bash scripts/format.sh
bash scripts/run-ci-checks.sh >> .local/run-ci-checks.log

For format.sh, I got the following output

Screenshot 2024-03-17 at 12 21 15β€―AM

For run-ci-checks.sh, I got the following output in the terminal

Screenshot 2024-03-17 at 12 23 46β€―AM

I am attaching the log file for run-ci-checks.sh. I see that the lint script ran without errors. I am not sure what I am doing wrong. Kindly request your assistance.

run-ci-checks.log

adtygan avatar Mar 16 '24 16:03 adtygan

https://github.com/zenml-io/zenml/actions/runs/8307099335/job/22735937951?pr=2533#step:5:27 this is the specific thing you need to fix first of all.

strickvl avatar Mar 16 '24 16:03 strickvl

In the commit I had made I do see the import at Line 25 SHA is: 34d179ca47be2927e1e0af77f5dd25d4971e186a Screenshot 2024-03-17 at 12 44 43β€―AM

But after this, there was another commit made by you, and in this version I cannot see the import anymore SHA is: 64a1e745e4b38efc5ff4ce9c6738e95199f4dd50

Screenshot 2024-03-17 at 12 47 42β€―AM

I am not sure why this is happening.

adtygan avatar Mar 16 '24 16:03 adtygan

Check out this pull request onΒ  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

It seems like maybe you're using an old version of the ruff formatter @adtygan? Can you pip install "zenml[server,dev]" again in your environment to make sure you're using the latest version, then rerun the script and push the changes? We def shouldn't be seeing changes to all these files in the PR.

strickvl avatar Mar 22 '24 09:03 strickvl

I figured out that I was working on a fork of the repo so my develop branch was out of date. I pulled the updates from upstream and then pushed my changes. Please review.

adtygan avatar Mar 24 '24 10:03 adtygan

There are quite a few updated files in here that shouldn't need to be updated. Not sure what happened on your side or with the fork, but since the changes are small, it might be worth resetting this branch somehow so that only the changes you made are reflected in the changed files?

strickvl avatar Mar 25 '24 09:03 strickvl

I only made a change to the logging/step_logging.py file. But when I run the format.sh script it modifies a lot of other files. Can I run the script on only the login file and push a commit? I think that should fix the issue.

adtygan avatar Mar 25 '24 09:03 adtygan

You could try that, but the point is that the other previously-modified files will still have been previously committed. have you made sure to installed / upgraded the latest version of the dev tools? Probably if you do pip install -U ruff that would be enough, since that's the package that's modifying / formatting files...

On Mon, 25 Mar 2024 at 10:30, Aditya Ganesh Kumar @.***> wrote:

I only made a change to the logging/step_logging.py file. But when I run the format.sh script it modifies a lot of other files. Can I run the script on only the login file and push a commit? I think that should fix the issue.

β€” Reply to this email directly, view it on GitHub https://github.com/zenml-io/zenml/pull/2533#issuecomment-2017569718, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZRNJQYLQ2ZEIVVOF6DSWTYZ7VDFAVCNFSM6AAAAABEZCOQNWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMJXGU3DSNZRHA . You are receiving this because you were mentioned.Message ID: @.***>

strickvl avatar Mar 25 '24 09:03 strickvl

I upgraded zenml but not ruff. I will do this also and then try once more like you mentioned.

adtygan avatar Mar 25 '24 10:03 adtygan

Yeah the updates to the important formatting packages will only come when you update zenml[dev] packages, not just core zenml @adtygan

strickvl avatar Mar 25 '24 10:03 strickvl

I had already run pip install "zenml[server,dev]" the last time. But this was using ruff 0.1.7. I ran pip install -U ruff after this like you suggested and it upgraded to version 0.3.4. When I ran format.sh now, it doesn't raise any errors. So I have now pushed my changes.

adtygan avatar Mar 25 '24 15:03 adtygan

May I please know if there is any action required from my end to solve this issue?

adtygan avatar Apr 06 '24 07:04 adtygan

@bcdurak is looking into it / at it

strickvl avatar Apr 06 '24 11:04 strickvl

Thank you for your review @bcdurak . I agree with the points you have mentioned above. If I am allowed to propose a GCP specific solution, I have 2 approaches in mind.

  1. Patch the time limit as well so that my current solution can work.
  2. GCP, if I'm not wrong, allows some other way to create mutable files. But as far as I know, it won't be a GCS object. I will look into using these tools to solve the problem.

Which of the 2 routes do you suggest?

Thanks

adtygan avatar May 02 '24 10:05 adtygan

linked to #2366 (closed in favor of this one)

strickvl avatar May 03 '24 05:05 strickvl

Hey @adtygan,

I think the second approach would be better here if possible. The first one is still viable but I would only apply it if the active artifact store is a GCS artifact store.

As I mentioned before, logging has been an integral part of ZenML and we will be working on improving the user experience when it comes to handling these log messages. @safoinme is currently taking a look at our implementation. He can also share any insights and recommendations he comes across.

bcdurak avatar May 07 '24 14:05 bcdurak

Thank you for your inputs @bcdurak . I will see how I can implement option 2. @safoinme could you please share your insights?

adtygan avatar May 08 '24 08:05 adtygan

GCP issue shall be closed with https://github.com/zenml-io/zenml/pull/2755

avishniakov avatar Jun 06 '24 16:06 avishniakov

Closed with https://github.com/zenml-io/zenml/pull/2755

htahir1 avatar Jun 10 '24 19:06 htahir1