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
Dynamic context via prefect.runtime
#8790
Conversation
✅ Deploy Preview for prefect-docs ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks cool! Just a few small suggestions/questions from me
re-use get flow run ID method Co-authored-by: Alexander Streed <desertaxle@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm so psyched that you knocked this out so quickly. Thanks for updating the API reference. We'll want other docs on how to use this. I'll start thinking about that today.
This is a much more elegant solution than get_run_context. Can I suggest adding more available attributes? Specifically flow_name, flow_run_name, and deployment_name. Obviously we can get these using the API once we have the already available IDs but it would be much more straight forward if they were available as attributes. For people transitioning from v1 (like us) it would make things easier if the v1 context items (https://docs-v1.prefect.io/api/latest/utilities/context.html#context-2) were all available as attributes in prefect.runtime in V2. Or at least all the ones that are still applicable in v2. |
@jbnitorum yep we will continue to add attributes following this first pass; if you want to open an issue it might help facilitate other contributions. |
+1 on flow_name |
#8864 adds some more attributes |
Many users rely on various pieces of run / deployment configuration for dynamically specifying pieces of code external to a flow function; for example, one might want to configure a task runner based on either tags or parameters passed to a deployment run.
Today, users must query the API themselves for information outside of a flow function; within a flow / task function, they can use
get_run_context
but that is intended for internal purposes more than as a user facing entity (there are many attributes here that would be confusing for users to access).This PR introduces a new submodule,
prefect.runtime
, for accessing such information in an intuitive and graceful way - when accessed outside of a known context, all attributes return empty values. This submodule can easily be expanded upon following the patterns developed here in ways that are memory efficient and independent of implementation details of the engine. Note that the attribute access implemented here was inspired by PEP 562.Example
Imagine using a
RayTaskRunner
and wanting to tag the Ray cluster based on customer name, which is also a parameter to your flow:Previously this was not achievable in a first class way.
Checklist
<link to issue>
"fix
,feature
,enhancement