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

Add PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW setting to handle error raised when logs outside of flow run context #8311

Merged
merged 8 commits into from
Jan 31, 2023

Conversation

serinamarie
Copy link
Contributor

@serinamarie serinamarie commented Jan 30, 2023

Overview

Closes #7851. Currently a MissingContextError is raised when logging outside of a flow run context, but users would still like to have a logger for reasons listed in #7851.

This change adds a PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW setting that defaults to warn when a custom logger attempts to send logs outside of a flow run context. Users can toggle this setting to ignore, warn, or error.

Docs preview

Screenshot 2023-01-31 at 1 54 14 PM

Example

This change would handle the reproduction inside #7851 like so:

PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW=ignore produces no warnings or errors when running the reproduction script.

export PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW=ignore
❯ PREFECT_LOGGING_EXTRA_LOGGERS=my-logger python my_flow.py
Log inside a regular function
[DEBUG 2023-01-30 12:10:34,560 my_flow.py:8] debug
12:10:34.560 | DEBUG   | my-logger - debug
[INFO 2023-01-30 12:10:34,561 my_flow.py:9] info
12:10:34.561 | INFO    | my-logger - info
[ERROR 2023-01-30 12:10:34,562 my_flow.py:10] error
12:10:34.562 | ERROR   | my-logger - error
[WARNING 2023-01-30 12:10:34,562 my_flow.py:11] warning
12:10:34.562 | WARNING | my-logger - warning
[CRITICAL 2023-01-30 12:10:34,562 my_flow.py:12] critical
12:10:34.562 | CRITICAL | my-logger - critical
------------------------------`

PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW=error raises an error when running the reproduction script.

export PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW=error
❯ PREFECT_LOGGING_EXTRA_LOGGERS=my-logger python my_flow.py
Log inside a regular function
Traceback (most recent call last):
  File "/Users/bean/code/prefect/my_flow.py", line 25, in <module>
    regular_log()
  File "/Users/bean/code/prefect/my_flow.py", line 8, in regular_log
    logger.debug("debug")
  File "/Users/bean/.pyenv/versions/3.9.13/lib/python3.9/logging/__init__.py", line 1434, in debug
    self._log(DEBUG, msg, args, **kwargs)
  File "/Users/bean/.pyenv/versions/3.9.13/lib/python3.9/logging/__init__.py", line 1589, in _log
    self.handle(record)
  File "/Users/bean/.pyenv/versions/3.9.13/lib/python3.9/logging/__init__.py", line 1599, in handle
    self.callHandlers(record)
  File "/Users/bean/.pyenv/versions/3.9.13/lib/python3.9/logging/__init__.py", line 1661, in callHandlers
    hdlr.handle(record)
  File "/Users/bean/.pyenv/versions/3.9.13/lib/python3.9/logging/__init__.py", line 952, in handle
    self.emit(record)
  File "/Users/bean/code/prefect/src/prefect/logging/handlers.py", line 281, in emit
    self.handleError(record)
  File "/Users/bean/code/prefect/src/prefect/logging/handlers.py", line 295, in handleError
    raise exc
  File "/Users/bean/code/prefect/src/prefect/logging/handlers.py", line 279, in emit
    self.get_worker(profile).enqueue(self.prepare(record, profile.settings))
  File "/Users/bean/code/prefect/src/prefect/logging/handlers.py", line 321, in prepare
    raise MissingContextError(
prefect.exceptions.MissingContextError: Logger 'my-logger' attempted to send logs to Orion without a flow run id. The Orion log handler can only send logs within flow run contexts unless the flow run id is manually provided.

PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW=warn returns a warning for each attempted log when running the reproduction script.

export PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW=warn
❯ PREFECT_LOGGING_EXTRA_LOGGERS=my-logger python my_flow.py
Log inside a regular function
/Users/bean/code/prefect/my_flow.py:8: UserWarning: Logger 'my-logger' attempted to send logs to Orion without a flow run id. The Orion log handler can only send logs within flow run contexts unless the flow run id is manually provided.
  logger.debug("debug")
[DEBUG 2023-01-30 12:11:00,153 my_flow.py:8] debug
12:11:00.153 | DEBUG   | my-logger - debug
/Users/bean/code/prefect/my_flow.py:9: UserWarning: Logger 'my-logger' attempted to send logs to Orion without a flow run id. The Orion log handler can only send logs within flow run contexts unless the flow run id is manually provided.
  logger.info("info")
[INFO 2023-01-30 12:11:00,155 my_flow.py:9] info
12:11:00.155 | INFO    | my-logger - info
/Users/bean/code/prefect/my_flow.py:10: UserWarning: Logger 'my-logger' attempted to send logs to Orion without a flow run id. The Orion log handler can only send logs within flow run contexts unless the flow run id is manually provided.
  logger.error("error")
[ERROR 2023-01-30 12:11:00,155 my_flow.py:10] error
12:11:00.155 | ERROR   | my-logger - error
/Users/bean/code/prefect/my_flow.py:11: UserWarning: Logger 'my-logger' attempted to send logs to Orion without a flow run id. The Orion log handler can only send logs within flow run contexts unless the flow run id is manually provided.
  logger.warning("warning")
[WARNING 2023-01-30 12:11:00,155 my_flow.py:11] warning
12:11:00.155 | WARNING | my-logger - warning
/Users/bean/code/prefect/my_flow.py:12: UserWarning: Logger 'my-logger' attempted to send logs to Orion without a flow run id. The Orion log handler can only send logs within flow run contexts unless the flow run id is manually provided.
  logger.critical("critical")
[CRITICAL 2023-01-30 12:11:00,155 my_flow.py:12] critical
12:11:00.155 | CRITICAL | my-logger - critical
------------------------------

Default PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW setting returns a warning for each attempted log when running the reproduction script (same as above).

unset PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW
❯ PREFECT_LOGGING_EXTRA_LOGGERS=my-logger python my_flow.py
Log inside a regular function
/Users/bean/code/prefect/my_flow.py:8: UserWarning: Logger 'my-logger' attempted to send logs to Orion without a flow run id. The Orion log handler can only send logs within flow run contexts unless the flow run id is manually provided.
  logger.debug("debug")
[DEBUG 2023-01-30 12:12:13,166 my_flow.py:8] debug
12:12:13.166 | DEBUG   | my-logger - debug
/Users/bean/code/prefect/my_flow.py:9: UserWarning: Logger 'my-logger' attempted to send logs to Orion without a flow run id. The Orion log handler can only send logs within flow run contexts unless the flow run id is manually provided.
  logger.info("info")
[INFO 2023-01-30 12:12:13,168 my_flow.py:9] info
12:12:13.168 | INFO    | my-logger - info
/Users/bean/code/prefect/my_flow.py:10: UserWarning: Logger 'my-logger' attempted to send logs to Orion without a flow run id. The Orion log handler can only send logs within flow run contexts unless the flow run id is manually provided.
  logger.error("error")
[ERROR 2023-01-30 12:12:13,168 my_flow.py:10] error
12:12:13.168 | ERROR   | my-logger - error
/Users/bean/code/prefect/my_flow.py:11: UserWarning: Logger 'my-logger' attempted to send logs to Orion without a flow run id. The Orion log handler can only send logs within flow run contexts unless the flow run id is manually provided.
  logger.warning("warning")
[WARNING 2023-01-30 12:12:13,168 my_flow.py:11] warning
12:12:13.168 | WARNING | my-logger - warning
/Users/bean/code/prefect/my_flow.py:12: UserWarning: Logger 'my-logger' attempted to send logs to Orion without a flow run id. The Orion log handler can only send logs within flow run contexts unless the flow run id is manually provided.
  logger.critical("critical")
[CRITICAL 2023-01-30 12:12:13,168 my_flow.py:12] critical
12:12:13.168 | CRITICAL | my-logger - critical
------------------------------

Checklist

  • This pull request references any related issue by including "closes <link to issue>"
    • If no issue exists and your change is not a small fix, please create an issue first.
  • This pull request includes tests or only affects documentation.
  • This pull request includes a label categorizing the change e.g. fix, feature, enhancement

@netlify
Copy link

netlify bot commented Jan 30, 2023

Deploy Preview for prefect-orion ready!

Name Link
🔨 Latest commit daa1c51
🔍 Latest deploy log https://app.netlify.com/sites/prefect-orion/deploys/63d972b2ffaa490008151645
😎 Deploy Preview https://deploy-preview-8311--prefect-orion.netlify.app/concepts/logs
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@serinamarie serinamarie added the enhancement An improvement of an existing feature label Jan 30, 2023
@zanieb
Copy link
Contributor

zanieb commented Jan 30, 2023

Looks like a good start!

@serinamarie serinamarie changed the title Add PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW setting to handle logs outside of flow run context Add PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW setting to handle error raised when logs outside of flow run context Jan 30, 2023
@serinamarie serinamarie marked this pull request as ready for review January 31, 2023 03:09
@serinamarie serinamarie requested review from tpdorsey and a team as code owners January 31, 2023 03:09
@serinamarie serinamarie changed the title Add PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW setting to handle error raised when logs outside of flow run context Add PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW setting to handle error raised when logs outside of flow run context Jan 31, 2023
Copy link
Contributor

@tpdorsey tpdorsey left a comment

Choose a reason for hiding this comment

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

Looks good. Minor suggested edit.

src/prefect/settings.py Outdated Show resolved Hide resolved
docs/concepts/logs.md Outdated Show resolved Hide resolved
Copy link
Contributor

@zanieb zanieb left a comment

Choose a reason for hiding this comment

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

Nice! Minor comment on the docs change.

docs/concepts/logs.md Outdated Show resolved Hide resolved
@serinamarie serinamarie merged commit 5dcfe7d into main Jan 31, 2023
@serinamarie serinamarie deleted the add-missing-flow-logger-setting branch January 31, 2023 19:57
desertaxle pushed a commit that referenced this pull request Feb 1, 2023
… raised when logs outside of flow run context (#8311)

Co-authored-by: Michael Adkins <michael@prefect.io>
ddelange added a commit to ddelange/prefect that referenced this pull request Feb 1, 2023
…erm-testing

* 'main' of https://github.com/prefecthq/prefect: (40 commits)
  Expand `work_queue` table to accommodate work pools (PrefectHQ#8264)
  Update issue label workflow (PrefectHQ#8334)
  Add `PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW` setting to handle error raised when logs outside of flow run context (PrefectHQ#8311)
  Fix tests for scheduled flow runs CLI `--start-at` option (PrefectHQ#8330)
  allow snakecase in maps folder (PrefectHQ#8322)
  Bump @prefecthq/vue-compositions from 1.3.0 to 1.3.3 in /orion-ui (PrefectHQ#8328)
  Bump typescript from 4.9.4 to 4.9.5 in /orion-ui (PrefectHQ#8325)
  Bump @prefecthq/orion-design from 1.2.8 to 1.2.9 in /orion-ui (PrefectHQ#8326)
  Mention reverse proxy for PREFECT_API_URL config (PrefectHQ#8240)
  Update Issue bot (PrefectHQ#8297)
  Fix Prefect Cloud typo in FAQ (PrefectHQ#8317)
  Remove unused Cloud Getting Started page (PrefectHQ#8291)
  Bump @playwright/test from 1.29.2 to 1.30.0 in /orion-ui (PrefectHQ#8313)
  Add role permissions regarding block secrets (PrefectHQ#8309)
  [maintenance] Update isort to fix errors in CI (PrefectHQ#8298)
  Add assertion for single notification and expand notification test suite (PrefectHQ#7504)
  Bump @prefecthq/orion-design from 1.2.7 to 1.2.8 in /orion-ui (PrefectHQ#8303)
  Bump @prefecthq/vue-compositions from 1.2.9 to 1.3.0 in /orion-ui (PrefectHQ#8302)
  Bump @prefecthq/prefect-design from 1.2.2 to 1.2.3 in /orion-ui (PrefectHQ#8301)
  Bump @prefecthq/eslint-config from 1.0.22 to 1.0.23 in /orion-ui (PrefectHQ#8300)
  ...
@zanieb zanieb mentioned this pull request Feb 3, 2023
3 tasks
zanieb added a commit that referenced this pull request Feb 3, 2023
… raised when logs outside of flow run context (#8311)

Co-authored-by: Michael Adkins <michael@prefect.io>
zanieb added a commit that referenced this pull request Feb 3, 2023
… raised when logs outside of flow run context (#8311)

Co-authored-by: Michael Adkins <michael@prefect.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom logger raises exception outside a flow and shows duplicate logs inside a flow
3 participants