hello, anyone knows if it’s possible to include agent logs in flow logs? I’d like to include some lo...
m
hello, anyone knows if it’s possible to include agent logs in flow logs? I’d like to include some logs from my custom Infrastructure block
r
Quick note up front: if you're able to convert your infrastructure block into a worker, then you'll have access to a flow run logger and won't need to do any extra work. It's been a while since I made an infrastructure block, but IIRC by the time
run
is called on your block,
""PREFECT__FLOW_RUN_ID"
should be available in the block's
env
dict. You could probably use that plus the API client to manually create log entries from within your infra block. So, maybe something like:
Copy code
from prefect.context import FlowRunContext
from prefect import get_client, get_run_logger
from prefect.logging import 

# then, in your `run` method
client = get_client()
flow_run_id = self.env["PREFECT__FLOW_RUN_ID"]
flow_run = client.read_flow_run(flow_run_id)
flow = client.read_flow(flow_run.flow_id)
# make a 'fake' flow run context to pass to get_run_logger
context = FlowRunContext(flow=flow, flow_run=flow_run)
logger = get_run_logger(context)
That's just from memory - I haven't tried it. I think it'll give you a logger attached to the flow run. In general, messing around with contexts manually isn't a good idea, but if you've made your own infra block it's probably okay. But YMMV, and use at your own risk, of course. 🙂 You could also just get the flow run ID and then use the client's create_logs call to add log entries to the flow run, but then you won't get any of the log formatting the logger normally applies. This doesn't quite get you the agent logs you mentioned, but it sounds like the logs from inside the infra block.
m
thanks Ryan, that’s super helpful! yeah I do have access to flow run id inside
run
so I’ll try to hack around. Thanks for the tip with workers, it might take me some time to migrate but I’ll keep that in mind