Search code examples
azureazure-functionsazureportalazure-durable-functions

How to measure response time for each and every request in Azure Functions


I want to run Azure Durable Functions and measure the response time taken from the time the trigger is pulled to the time it returns.

The sequence of events I want to measure is as follows

  1. Send HTTP request to Durable Functions (start measurement)
  2. Orchestration functions and activity functions send data to each other and execute the process.
  3. Results are returned and the process ends (measurement ends)

enter image description here

The reason why I want to do this is to investigate how the response time of Durable Functions is affected when the size of data transferred by the orchestration function and the activity function is different.

How can I measure the time between the trigger being pulled and the response being received in Azure Portal or Applications Insights?

The environment I am using is, Azure Free Tier Python programming model v2


Solution

  • I have deployed the below code to Azure Function App.

    import  azure.functions  as  func
    import  azure.durable_functions  as  df
    import  logging
    import  numpy  as  np
    import  pickle  
    
    app  =  df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)  
    
    ### client function ###
    @app.route(route="orchestrators/client_function")
    @app.durable_client_input(client_name="client")
    async  def  client_function(req: func.HttpRequest, client: df.DurableOrchestrationClient) -> func.HttpResponse:
    instance_id  =  await  client.start_new("orchestrator", None, {})
    logging.info(f"Started orchestration with ID = '{instance_id}'.")
    await  client.wait_for_completion_or_create_check_status_response(req, instance_id)
    status  =  await  client.get_status(instance_id)
    runtime  =  status.runtime_status
    output  =  status.output
    return  f"runtime: {runtime}\n\noutput:{output}" 
    
    @app.orchestration_trigger(context_name="context")
    def  orchestrator(context: df.DurableOrchestrationContext):
    result  =  yield  context.call_activity("activity1", "")
    return  result
    
    @app.activity_trigger(input_name="blank")
    def  activity1(blank: str) -> str:
    data  =  np.random.rand(1024  *  1024  *  5)
    serialized_data  =  pickle.dumps(data)
    serialized_size  =  len(serialized_data)
    return  str(serialized_size)
    

    Post deployment, I am able to see the functions in function app.

    enter image description here

    Then, I invoked the Http Trigger function.

    enter image description here

    There are multiple ways you can check the total time taken by Http function to complete the execution.

    1. Check in Monitor blade as shown below.

    enter image description here

    1. You can check the total response time in Application Insight as well.

    enter image description here

    enter image description here

    You can also graphically visualize the execution in vs code using Durable Function Monitor extension.

    Install the Durable Function Monitor extension in vs code and then connect to your function app. You can refer to this link to know more about the mentioned extension.

    enter image description here