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

时间:2018-06-07 03:21:25

标签: azure azure-application-insights azure-sdk-python

我使用以下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?

3 个答案:

答案 0 :(得分:1)

没有用于检索数据的SDK。只有REST API。

答案 1 :(得分:0)

答案 2 :(得分:0)

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