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

Allow custom log formats #3288

Merged
merged 9 commits into from
Jan 28, 2025
Merged

Allow custom log formats #3288

merged 9 commits into from
Jan 28, 2025

Conversation

schustmi
Copy link
Contributor

@schustmi schustmi commented Jan 15, 2025

Describe changes

This PR adds an environment variable to allow specifying custom log formats. Additionally, we now propagate logging-related environment variables for remote pipeline runs.

Pre-requisites

Please ensure you have done the following:

  • I have read the CONTRIBUTING.md document.
  • I have added tests to cover my changes.
  • 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.
  • IMPORTANT: I made sure that my changes are reflected properly in the following resources:
    • ZenML Docs
    • Dashboard: Needs to be communicated to the frontend team.
    • Templates: Might need adjustments (that are not reflected in the template tests) in case of non-breaking changes and deprecations.
    • Projects: Depending on the version dependencies, different projects might get affected.

Types of changes

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

Copy link
Contributor

coderabbitai bot commented Jan 15, 2025

Important

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.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

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 internal To filter out internal PRs and issues enhancement New feature or request labels Jan 15, 2025
Copy link
Contributor

@hyperlint-ai hyperlint-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.

The style guide flagged several spelling errors that seemed like false positives. We skipped posting inline suggestions for the following words:

  • [Ee]ndhint

@schustmi schustmi requested a review from stefannica January 15, 2025 14:57
@@ -40,6 +45,14 @@
if TYPE_CHECKING:
from zenml.artifact_stores.base_artifact_store import BaseArtifactStore

ENV_VARS_TO_PROPAGATE = [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stefannica Do you think this is a reasonable change, to automatically forward these logging related environment variables to the Docker containers that run steps? Or should I revert this, or add another environment variable/setting somewhere that allows specifying which environment variables get forwarded?

Copy link
Contributor

Choose a reason for hiding this comment

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

The concern I have is that some of these client-side env vars may override whatever is set in the container images or docker settings that the users define for containerized pipelines. How would a user change the logging verbosity for a containerized pipeline before this PR ? And would that still work ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep that is the case with the current implementation. Previously, the only way for users to configure the logging behaviour for steps in container images was by setting these environment variables in the Docker settings, which means they will be included in the Dockerfile.

My changes in this PR essentially pass the environment as runtime options using the orchestrator, which will override the values present in the Docker image. So this would lead to different behaviour than previous versions of ZenML in cases where users have local settings that differ from the environment variables they set in the Docker settings. Which I definitely agree is not the best solution.
I see a few ways to change this:

  • Remove the automatic forwarding of any environment variable. This means that the logging format will only be applied client-side, and if wanted users would also have to set it on the Docker settings to apply it to step logs.
  • Only forward my new environment variable for the log format. This wouldn't change any existing behaviour, but would be very inconsistent in the way we treat these (logging) environment variables.
  • Allow users to configure which environment variables from the client environment get forwarded (probably via a different environment variable or a pipeline configuration?). We could then still decide whether we want to default this to nothing, only the log format, or all logging environment variables

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stefannica @bcdurak After a discussion with @htahir1 we decided to keep the new environment variable consistent with all the existing ones, and revisit this environment forwarding when we take a closer look at environments.

@schustmi schustmi requested a review from htahir1 January 15, 2025 14:59
The formatter.
"""
if log_format := os.environ.get(ENV_ZENML_LOGGING_FORMAT, None):
return logging.Formatter(fmt=log_format)
Copy link
Contributor

Choose a reason for hiding this comment

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

@schustmi Do you think we can also add a standard Python entry-point logic, in order to be able to inject custom implementation of the Logger?
It is a clean way to provide extensibility in a plugin fashion, just by pip-installing dependencies in the runtime.
Here is an example of how MLFlow does its plugin system.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think there are many ways to extend this (this was supposed to just be a quick way to have some basic configurability). There's also the logging file config which we could give access to I assume (https://docs.python.org/3/library/logging.config.html#logging-config-fileformat), but I'm curious to hear what kind of plugin you'd like, both specifically for this logging feature as well as in general in ZenML :)

Copy link
Contributor

Choose a reason for hiding this comment

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

My company uses a custom JSON log format to ingest in the central logging infrastructure. Some of the fields can come from environment variables, and some can be dynamically added based on the context (e.g., if there is an HTTP context, we add the correlation_id). We can also reformat the exception stack trace.
We use the json-logging lib to do this, thus we need to provide our implementation of the CustomLogger class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting, I just had a quick look at the library and it seems like it needs to be initialized which supplies a different log formatter for all loggers. Could you provide me with a quick pseudo-code of your internal logging setup, so that I can make sure it's possible when designing more logging configurability?

Copy link
Contributor

Choose a reason for hiding this comment

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

@schustmi here's the pseudo-code / sequence we use when setting up the log facility:

  • Define a set of loggers to skip
  • Define CustomLogger class (json_logging lib)
  • Define function to get root logger names
  • Define function to filter logger names based on the set of loggers to skip
  • Define function to attach logger with handlers
  • Define function to set up logger with parameters
  • Clear existing handlers and set up new ones
  • Attach loggers for main package and additional loggers if autodiscovery is enabled
  • Set log level and propagation for the main logger

@schustmi schustmi requested a review from bcdurak January 21, 2025 10:39
Copy link
Contributor

github-actions bot commented Jan 27, 2025

✅ No broken markdown links found!

docs/book/reference/environment-variables.md Outdated Show resolved Hide resolved
src/zenml/constants.py Show resolved Hide resolved
@@ -157,8 +158,17 @@ def show_dashboard(url: str) -> None:

elif environment in (EnvironmentType.NATIVE, EnvironmentType.WSL):
if constants.handle_bool_env_var(
constants.ENV_AUTO_OPEN_DASHBOARD, default=True
constants.ENV_AUTO_OPEN_DASHBOARD, default=False
Copy link
Contributor

Choose a reason for hiding this comment

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

With the current state, if they use the old variable and set it to False, this check will still return True. Here, I would still expect the old one to work, while throwing a warning.

Alternatively, if they configured the old one to be True and the new one to be False, this will still open the dashboard.

Not sure, if this is the intended behavior, that's why I just wanted to note it down here. (IMO, if the new one is set, I would expect it to take precedence.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously, the dashboard automatically openend unless you set the legacy environment variable to False. IMO, setting any of these environment variable to True should be a no-op. It's the default behaviour anyway.

With the current logic, there is no precedence: If any of the two are set to False, the dashboard will not open automatically. If the old one is used, it will warn that it's deprecated.

WDYT?

@schustmi schustmi requested a review from bcdurak January 28, 2025 13:42
@schustmi schustmi merged commit a126a87 into develop Jan 28, 2025
33 of 35 checks passed
@schustmi schustmi deleted the feature/PRD-769-log-format branch January 28, 2025 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request internal To filter out internal PRs and issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants