直接使用 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)