Vincent Rubingh
09/25/2024, 6:04 PMNate
09/25/2024, 8:04 PMget_run_logger
?
it should just work and has all the standard methods (info()
debug()
etc)
log_prints
is just sending what you send to print
through get_run_logger().info()
Vincent Rubingh
10/02/2024, 6:43 PMlog_prints=false
, none of the output from inside the container (docker_container_block
) ends up in prefect:
@task(name="Parser", retries=1, log_prints=True)
def run__parser():
#TODO: failures from within the docker container are not propagating to the (failed) task
docker_container_block = DockerContainer.load("docker-container-name")
docker_container_block.env.update(
{
"PREFECT_TASK_ID": runtime.task_run.id,
"PREFECT_TASK_NAME": runtime.task_run.name,
"PREFECT_FLOW_ID": runtime.flow_run.id,
"PREFECT_FLOW_NAME": runtime.flow_run.name,
"MAX_NUM_ROUNDS": Variable.get('parser_num_rounds').value,
}
)
container_result = docker_container_block.run()
log_result = log_parser_results(
artifact_name="parser-report",
service_name="Parser",
service_path="parser/dev/logs/parser.log")
return {}
Vincent Rubingh
10/02/2024, 6:45 PMdocker_container_block
? That's what I was trying, so I can get the logging with the right levels to propagate correctly (especially errors).Vincent Rubingh
10/02/2024, 6:48 PMget_run_logger
in the actual docker container. That's why I was trying to forward some task_id and flow_id to the docker container, as I figured with those I can create a prefect logger in my container(s)Nate
10/02/2024, 6:53 PMget_run_logger
, its just within the context of any task or flow run (pedantic detail: there's a ContextVar
we set when we enter the flow or task run engine, so if you're in that context, you can use get_run_logger
) i.e. inside a flow or task function
So if I setlog_prints will patch the builtin, none of the output from inside the container (log_prints=false
) ends up in prefect:docker_container_block
print
, which I don't see used above, so its not clear to me why anything should be sent to the API as logs, not knowing how log_parser_results
works at leastVincent Rubingh
10/02/2024, 7:02 PMDockerContainer
object (that's part of prefect.infrastructure.container
).
So far so good, I can get this to work, I use different tasks to run different docker containers.
However, I want to somehow redirect the output from inside the docker containers (which can contain many lines of info/success/debug level output as well as errors) correctly to the Prefect UI. (Either redirect by changing the code inside the task above, or change my logging inside my docker image to log to prefect directly)
So far I've only managed to redirect the output from the running docker containers by using log_prints=false
. I haven't found another way to get for example an error to correctly propagate from inside my running docker container to Prefect. Is this a use case that's supported?Vincent Rubingh
10/02/2024, 7:02 PMNate
10/02/2024, 7:10 PMDockerContainer(...).run()
were originally added to prefect 2.x as a way of defining deployment config. they happen to be convenient wrappers for invoking work on some infra like a container, but these things are removed in 3.x and generally speaking I would recommend creating actual deployments for these pieces of work that run on their own containers, and then chain them in a parent flow, using run_deployment
to kick them off. using these infra blocks directly is not a 1st class pattern and is likely why you're having a hard time finding info on it
• its not clear to me whats happening in each container of yours rn, but generally if you're running prefect code, and you're in a task or flow context, using the get_run_logger
should just work as long as that container has the PREFECT_API_KEY and PREFECT_API_URL set as env vars so that the API it talks to is the one you expect (again, setting these env vars just happens for you if you were to define deployments for these intermediate containers, but you'd have to inject it yourself if you use DockerContainer
yourself directly)Vincent Rubingh
10/02/2024, 7:12 PMVincent Rubingh
10/02/2024, 7:12 PMNate
10/02/2024, 7:13 PMNate
10/02/2024, 7:15 PMtriggers
that can be defined on deployments, that way (in many cases) you can get away with not having a parent "chaining" flow at all, because each downstream deployment just has a trigger defined to expect
the prefect.flow-run.Completed
event from the upstream