從 ML Web 服務端點監視及收集資料

適用於:Python SDK azureml v1

在本文中,您會了解如何在 Azure Kubernetes Service (AKS) 或 Azure 容器執行個體 (ACI) 中,從部署至 Web 服務端點的模型收集資料。 使用 Azure 應用程式 Insights 從端點收集下列資料:

  • 輸出資料
  • 回應
  • 要求速率、回應時間和失敗率
  • 相依性速率、回應時間和失敗率
  • 例外狀況

enable-app-insights-in-production-service.ipynb 筆記本會示範本文中的概念。

了解如何依照使用 Jupyter 筆記本來探索這項服務一文來執行筆記本。

重要

本文中的資訊取決於工作區所建立的 Azure Application Insights 執行個體。 如果刪除 Application Insights 執行個體,則無法重建,只能刪除並重新建立工作區。

提示

如果要改用線上端點,請改為參考監視線上端點一文中的資訊。

必要條件

使用 Python SDK 設定記錄

在本節中,您會了解如何使用 Python SDK 來啟用 Application Insight 記錄。

更新已部署的服務

使用下列步驟來更新現有的 Web 服務:

  1. 識別您工作區中的服務。 ws 的值是您工作區的名稱

    from azureml.core.webservice import Webservice
    aks_service= Webservice(ws, "my-service-name")
    
  2. 更新您的服務,並啟用 Azure Application Insights

    aks_service.update(enable_app_insights=True)
    

記錄您服務中的自訂追蹤

重要

Azure Application Insights 只會記錄最多 64kb 的承載。 如果達到此限制,您可能會看到錯誤,例如記憶體不足,或可能未記錄任何資訊。 如果您想要記錄的資料大於 64kb,則應該改為使用在生產環境中收集模型資料中的資訊,將該資料儲存至 Blob 儲存體。

針對更複雜的情況,例如 AKS 部署內的模型追蹤,我們建議使用協力廠商程式庫,例如 OpenCensus

若要記錄自訂追蹤,請依循部署方式及位置文件中的 AKS 或 ACI 標準部署程序。 然後,使用下列步驟:

  1. 藉由新增 print 陳述式來更新評分檔案,以在推斷期間將資料傳送至 Application Insights。 如需更複雜的資訊,例如要求資料和回應,請使用 JSON 結構。

    下列範例 score.py 檔案會記錄模型初始化的時間、推斷期間的輸入和輸出,以及發生任何錯誤的時間。

    import pickle
    import json
    import numpy 
    from sklearn.externals import joblib
    from sklearn.linear_model import Ridge
    from azureml.core.model import Model
    import time
    
    def init():
        global model
        #Print statement for appinsights custom traces:
        print ("model initialized" + time.strftime("%H:%M:%S"))
    
        # note here "sklearn_regression_model.pkl" is the name of the model registered under the workspace
        # this call should return the path to the model.pkl file on the local disk.
        model_path = Model.get_model_path(model_name = 'sklearn_regression_model.pkl')
    
        # deserialize the model file back into a sklearn model
        model = joblib.load(model_path)
    
    
    # note you can pass in multiple rows for scoring
    def run(raw_data):
        try:
            data = json.loads(raw_data)['data']
            data = numpy.array(data)
            result = model.predict(data)
            # Log the input and output data to appinsights:
            info = {
                "input": raw_data,
                "output": result.tolist()
                }
            print(json.dumps(info))
            # you can return any datatype as long as it is JSON-serializable
            return result.tolist()
        except Exception as e:
            error = str(e)
            print (error + time.strftime("%H:%M:%S"))
            return error
    
  2. 更新服務設定,並確定啟用 Application Insights。

    config = Webservice.deploy_configuration(enable_app_insights=True)
    
  3. 建置映像並將其部署在 AKS 或 ACI 上。 如需詳細資訊,請參閱部署方法和位置

在 Python 中停用追蹤

若要停用 Azure Application Insights,請使用下列程式碼:

## replace <service_name> with the name of the web service
<service_name>.update(enable_app_insights=False)

使用 Azure Machine Learning 工作室設定記錄

您也可以從 Azure Machine Learning 工作室啟用 Azure Application Insights。 當您準備好將模型部署為 Web 服務時,請使用下列步驟來啟用 Application Insights:

  1. 登入工作室,網址為 https://ml.azure.com

  2. 移至 [模型],然後選取您想要部署的模型。

  3. 選取 [+部署]

  4. 填入 [部署模型] 表單。

  5. 展開 [進階] 功能表。

    Deploy form

  6. 選取 [啟用 Application Insights 診斷及資料收集]

    Enable App Insights

檢視計量和記錄

查詢已部署模型的記錄

連線端點的記錄屬於客戶資料。 您可以使用 get_logs() 函數,從先前部署的 Web 服務擷取記錄。 記錄可能包含部署期間發生之任何錯誤的相關詳細資訊。

from azureml.core import Workspace
from azureml.core.webservice import Webservice

ws = Workspace.from_config()

# load existing web service
service = Webservice(name="service-name", workspace=ws)
logs = service.get_logs()

如果您有多個租用戶,則可能需要在 ws = Workspace.from_config() 之前新增下列驗證程式碼

from azureml.core.authentication import InteractiveLoginAuthentication
interactive_auth = InteractiveLoginAuthentication(tenant_id="the tenant_id in which your workspace resides")

檢視工作室中的記錄

Azure Application Insights 會將您的服務記錄儲存在與 Azure Machine Learning 工作區相同的資源群組中。 請使用下列步驟,以使用工作室來檢視您的資料:

  1. 工作室中,移至您的 Azure Machine Learning 工作區。

  2. 選取端點

  3. 選取已部署的服務。

  4. 選取 [Application Insights URL] 連結。

    Locate Application Insights url

  5. 在 Application Insights 中,從 [概觀] 索引標籤或 [監視] 區段中,選取 [記錄]

    Overview tab of monitoring

  6. 若要從 score.py 檔案檢視記錄的資訊,請查看追蹤資料表。 下列查詢會搜尋已記錄輸入值的記錄:

    traces
    | where customDimensions contains "input"
    | limit 10
    

    trace data

若要如何使用 Azure Application Insights 的詳細資訊,請參閱什麼是 Application Insights?

Web 服務中繼資料和回應資料

重要

Azure Application Insights 只會記錄最多 64kb 的承載。 如果達到此限制,則您可能會看到錯誤,例如記憶體不足,或可能未記錄任何資訊。

若要記錄 Web 服務要求資訊,請將 print 陳述式新增至 score.py 檔案。 每個 print 陳述式都會在 STDOUT 訊息底下的 Application Insights 追蹤資料表中產生一個項目。 Application Insights 會將 print 陳述式輸出儲存在 customDimensionsContents 追蹤資料表中。 列印 JSON 字串會在 Contents 下的追蹤輸出中產生階層式資料結構。

匯出用於保留和處理的資料

重要

Azure Application Insights 僅支援匯出至 Blob 儲存體。 如需此實作限制的詳細資訊,請參閱從 App Insights 匯出遙測資料

使用 Application Insights 的連續匯出,將資料匯出至 Blob 儲存體帳戶,您可以在其中定義保留設定。 Application Insights 會以 JSON 格式匯出資料。

Continuous export

下一步

在本文中,您已了解如何針對 Web 服務端點啟用記錄和檢視記錄。 請試讀下列文章以取得後續步驟: