Search code examples
python-3.xazureazure-blob-storageappinsights

Python azure function with app insights dependancy tracking


I have a python azure function, that gets data from blob storage.

import azure.functions as func
from azure.storage.blob import BlobServiceClient
from azure.identity import DefaultAzureCredential


def main(req: func.HttpRequest) -> func.HttpResponse:    
    default_credential = DefaultAzureCredential(exclude_shared_token_cache_credential=True)
    blob_storage_uri = "https://mystorage.blob.core.windows.net"

    blob_service_client = BlobServiceClient(blob_storage_uri, credential=default_credential)
    container_client = blob_service_client.get_container_client("my-container")
    blob_client = container_client.get_blob_client("files/stefan/my_file.json")
    blob_data = blob_client.download_blob().readall()
    return func.HttpResponse(blob_data, status_code=200)

I want to track getting data from blob storage with app insights dependencies:

enter image description here

my host.json looks like that:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "enableDependencyTracking": true,
      "dependencyTrackingOptions": {
        "enableSqlCommandTextInstrumentation": true
      },
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request;Exception;Dependency"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"
  }
}

What else should I do?


Solution

    • Followed this documation Dependency tracking in Application Insights .
    • Dependency tracking in Application Insights tracks the length of dependency calls, whether they succeed or fail, and other details like the dependence's name.
    • used Azure Blob Storage name in log_dependency
    • The log_dependency logs an Azure Blob Storage dependency with target "sampath23.blob.core.windows.net" and name "get_blob" and correlate them to requests and exceptions.

    Sample code to Set up Dependency Tracking:

    import  logging
    import  azure.functions  as  func
    from  applicationinsights  import  TelemetryClient
    from  azure.storage.blob  import  BlobServiceClient
    instrumentation_key = '57596d9d-e36c-4d39-bb87-19250c7c2812'
    tc = TelemetryClient(instrumentation_key)
    logging.basicConfig(level=logging.INFO)
    def  log_dependency(dependency_type,  target,  name,  success=True,  duration=None):
    tc.track_dependency(dependency_type,  target,  name,  success,  duration)
    tc.flush()
    def  log_to_blob(log_data):
    try:
    blob_service_client = BlobServiceClient.from_connection_string('DefaultEndpointsProtocol=https;AccountName=sampath23;AccountKey=KyJusUk7qzr9lZQGVfJs2kdCbMNK+ASt0aYv9w==;EndpointSuffix=core.windows.net')
    container_client = blob_service_client.get_container_client('sampath')
    blob_client = container_client.get_blob_client('function-logs1')
    blob_client.upload_blob(log_data)
    logging.info("Log data uploaded to Azure Blob Storage.")
    except  Exception  as  e:
    logging.exception("Error while uploading log data to Blob Storage: " + str(e))
    def  main(req:  func.HttpRequest)  ->  func.HttpResponse:
    try:
    log_dependency("Azure Blob Storage",  "sampath23.blob.core.windows.net",  "get_blob")
    log_to_blob("Function execution output")
    return  func.HttpResponse("Function executed successfully.")
    except  Exception  as  e:
    tc.track_exception()
    return  func.HttpResponse("Function failed.",  status_code=500)
    

    enter image description here enter image description here

    In Azure Application Insights:

    enter image description here

    • Azure Function to retrieve data from an Azure Blob Storage container and uses Application Insights to monitor and track dependencies and exceptions.

    To trace Dependency:

    def  main(req:  func.HttpRequest)  ->  func.HttpResponse:
    try:
    connection_string = 'DefaultEndpointsProtocol=https;AccountName=sampath23;AccountKey=Ky;EndpointSuffix=core.windows.net'
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)
    container_client = blob_service_client.get_container_client("sampath")
    blob_client = container_client.get_blob_client("function-logs")
    blob_data = blob_client.download_blob().readall() 
    tc.track_dependency("Azure Blob Storage",  blob_client.url,  "get_blob")
    return  func.HttpResponse(blob_data,  status_code=200)
    except  Exception  as  e:
    tc.track_exception()
    return  func.HttpResponse("Error occurred.",  status_code=500)
    

    enter image description here

    enter image description here