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
10 changes: 8 additions & 2 deletions src/prefect/logging/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
PREFECT_LOGGING_ORION_BATCH_SIZE,
PREFECT_LOGGING_ORION_ENABLED,
PREFECT_LOGGING_ORION_MAX_LOG_SIZE,
PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW,
)


Expand Down Expand Up @@ -285,8 +286,13 @@ def handleError(self, record: logging.LogRecord) -> None:
# Warn when a logger is used outside of a run context, the stack level here
# gets us to the user logging call
if isinstance(exc, MissingContextError):
warnings.warn(exc, stacklevel=8)
return
if PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW.value() == "warn":
serinamarie marked this conversation as resolved.
Show resolved Hide resolved
warnings.warn(str(exc), stacklevel=8)
return
elif PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW.value() == "ignore":
return
else:
raise exc

# Display a longer traceback for other errors
return super().handleError(record)
Expand Down
15 changes: 15 additions & 0 deletions src/prefect/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
Generic,
Iterable,
List,
Literal,
Mapping,
Optional,
Set,
Expand Down Expand Up @@ -583,6 +584,20 @@ def default_cloud_ui_url(settings, value):
)
"""The maximum size in bytes for a single log."""

PREFECT_LOGGING_ORION_WHEN_MISSING_FLOW = Setting(
Literal["warn", "error", "ignore"],
default="warn",
)
"""
Controls the behavior when logs are attempted to be sent to Orion without a flow run id.
serinamarie marked this conversation as resolved.
Show resolved Hide resolved
The Orion log handler can only send logs within flow run contexts unless the flow run id is
manually provided.

"warn": Log a warning message.
"error": Raise an error.
"ignore": Do not log a warning message or raise an error.
"""

PREFECT_LOGGING_COLORS = Setting(
bool,
default=True,
Expand Down