是否有 azure python sdk 来检索应用程序洞察数据?

Is there a azure python sdk to retrieve application insights data?

我使用下面的 App insights python sdk 在 app insights 资源中发送我的自定义指标。

https://github.com/Microsoft/ApplicationInsights-Python

现在,当我想要检索它时,我只能找到基于 Rest API 的方法来检索它。

https://dev.applicationinsights.io/documentation/Using-the-API/Metrics

是否有基于 RestAPI 的 curl 或 http 请求的替代方案 - 在应用洞察 python sdk 中?

没有用于检索数据的 SDK。仅休息 API.

Azure python SDK 现在支持来自 ApplicationInsightsDataClient 的查询操作。

https://docs.microsoft.com/en-us/python/api/azure-applicationinsights/azure.applicationinsights.applicationinsightsdataclient?view=azure-python

https://azuresdkdocs.blob.core.windows.net/$web/python/azure-applicationinsights/0.1.0/_modules/azure/applicationinsights/application_insights_data_client.html

直接使用 REST API。

首先在门户中转到您的实例,然后在侧面板上向下滚动到 'API',然后创建一个密钥。此处有更多信息:

https://dev.applicationinsights.io/documentation/Authorization/API-key-and-App-ID

然后您可以像这样简单地查询 REST api:

import requests
import json

appId = "..."
appKey = "..."

query = """
  traces
  | where timestamp > ago(1d) 
  | order by timestamp
  | limit 10
"""

params = {"query": query}
headers = {'X-Api-Key': appKey}
url = f'https://api.applicationinsights.io/v1/apps/{appId}/query'

response = requests.get(url, headers=headers, params=params)

logs = json.loads(response.text)
for row in logs['tables'][0]['rows']:
  structured = dict(zip(logs['tables'][0], row))
  print(structured)