Python Azure function timer trigger log info not showing when deployed

197 views Asked by At

I have a timer trigger function that downloads ml models. I want to log the name of these models an see this output in azure monitor . Below is an example of how I am using the logging library. This is just a snippet of code. For clarification the keys variable gets all the model names from a particular json file which I loaded earlier.

import datetime
import logging
import azure.functions as func
from azureml.core import Model

def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
    
        keys = model_file.keys()
        logging.info("downloading the following  models:")
        for key in keys:
            logging.info(key)
            model_obj = Model(ws, key)
            model_path = model_obj.download(target_dir=downloaded_models_path)
            print(model_path)

    logging.info(
        "download_ml_models timer trigger function ran at %s", utc_timestamp)

I want the logs to display the actual info of the models I have logged. However, from below you can see azure monitor just displays the function was successfully executed

enter image description here

1

There are 1 answers

2
SiddheshDesai On

I tried below method to log the Downloaded model name in my TimerTrigger.

My init.py:-

import datetime
import logging
import os
import azure.functions as func
from azureml.core import Model, Workspace
from applicationinsights import TelemetryClient
import json

# Initialize Application Insights
instrumentation_key = ("InstrumentationKey=xxxxx8bd;IngestionEndpoint=https://uksouth-1.in.applicationinsights.azure.com/;LiveEndpoint=https://uksouth.livediagnostics.monitor.azure.com/")
tc = TelemetryClient(instrumentation_key)

# Function to download models
def download_models():
    try:
        ws = Workspace.from_config('./config.json')
        # Load your Azure ML Workspace. Replace with your own values.
        ws = Workspace(subscription_id="xxxxxxx74c23f",
                       resource_group="rg-name",
                       workspace_name="mlws-name")

        # Replace with the actual path where you want to download the models
        downloaded_models_path = "temp"

        utc_timestamp = datetime.datetime.utcnow().replace(
            tzinfo=datetime.timezone.utc).isoformat()

        # Load model info from the JSON file
        with open("model_info.json", "r") as json_file:
            model_info = json.load(json_file)

        logging.info("Downloading the following models:")
        for model in model_info["models"]:
            model_name = model["name"]
            model_path = model["path"]
            logging.info("Model Name: %s", model_name)

            model_obj = Model(ws, model_name)
            model_path = model_obj.download(target_dir=downloaded_models_path)
            logging.info("Downloaded model %s to %s", model_name, model_path)
            tc.track_trace("Downloaded model {} to {}".format(model_name, model_path))

        tc.flush()

    except Exception as e:
        logging.error("An error occurred: %s", str(e))
        tc.track_exception()
        tc.flush()

# Azure Function entry point
def main(mytimer: func.TimerRequest) -> None:
    if mytimer.past_due:
        logging.info("Downloading models triggered by the timer...")
        tc.track_event("Downloading models triggered by the timer")
        download_models()

    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()
    logging.info("download_ml_models timer trigger function ran at %s", utc_timestamp)
    tc.track_event("download_ml_models timer trigger function ran at {}".format(utc_timestamp))
    tc.flush()

enter image description here

Also, In your current code. Modify the logging line in your code to include the real names of the models you are downloading if you want to view this output in Azure Monitor. Simply replace the line logging.info(key) with logging.info(f"downloading model: key"). This will record the model's name as it is downloaded.

import datetime
import logging
import azure.functions as func
from azureml.core import Model

def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
    
        keys = model_file.keys()
        logging.info("downloading the following  models:")
        for key in keys:
            logging.info(f"downloading model: key")
            model_obj = Model(ws, key)
            model_path = model_obj.download(target_dir=downloaded_models_path)
            print(model_path)

    logging.info(
        "download_ml_models timer trigger function ran at %s", utc_timestamp)