Hi! i'm trying to log the duration of each flow/ta...
# ask-community
l
Hi! i'm trying to log the duration of each flow/task without adding too much code. I see the duration shows up in the UI so it's calculated somewhere, any easy/hacky way to access the start/end times of each task and flow?
1
o
Maybe a get request to this endpoint would be sufficient? The start and end times are in the response: https://api.prefect.cloud/api/accounts/{account_id}/workspaces/{workspace_id}/flow_runs/{id}
l
sadly no i'm trying to integrate my Prefect project into my company's logging system and by far the easiest way is for the flows themselves to write run duration to log
o
could you not combine it with completion/failure hooks?
l
maybe, but it seems to me that there's an easier solution here i'm not seeing
@Oscar thanks for giving me the idea to look around in completion/failure hooks! ended up implementing something a bit weird but it works so I guess i'm fine with it:
Copy code
from prefect.client.schemas.objects import State, FlowRun
from prefect.client.schemas.objects import Flow as FlowSchema
def log_runtime(flow: FlowSchema, flow_run: FlowRun, state: State):
    flow_run_time_seconds = (state.timestamp - flow_run.created).total_seconds()
    print(f"flow_name: {flow.name}, runtime_seconds: {flow_run_time_seconds}")
o
You’re welcome! Glad it works