Python azure函数,具有应用洞察依赖性跟踪

tsm1rwdh  于 11个月前  发布在  Python
关注(0)|答案(1)|浏览(73)

我有一个python azure函数,它从blob存储中获取数据。

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)

字符串
我希望跟踪从blob存储获取数据的情况,并使用应用洞察依赖项:


的数据
我的host.json看起来像这样:

{
  "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)"
  }
}


我还能怎么办?

yyhrrdl8

yyhrrdl81#

  • 遵循此文档Dependency tracking in Application Insights
  • Application Insights中的依赖关系跟踪跟踪依赖关系调用的长度,无论它们是成功还是失败,以及依赖关系的名称等其他细节。
  • log_dependency中使用了Azure Blob存储名称
  • log_dependency记录具有目标"sampath23.blob.core.windows.net"和名称"get_blob"的Azure Blob存储依赖项,并将其与请求和异常关联。
    设置依赖跟踪的示例代码:
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)

字符串

在Azure Application Insights中:


  • Azure函数从Azure Blob Storage容器中检索数据,并使用Application Insights监视track dependencies和异常。
    跟踪依赖关系:
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)



相关问题