Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I use below App insights python sdk to send my custom metrics in app insights resource.

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

Now when I want to retrieve it, all i can find is Rest API based method to retrieve it.

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

Is there an alternative to RestAPI based curl or http request - in app insights python sdk ?

like image 971
explorer Avatar asked Jun 07 '18 03:06

explorer


2 Answers

There are no SDKs for retrieving data. Only REST API.

like image 193
ZakiMa Avatar answered Nov 14 '22 20:11

ZakiMa


Use the REST API directly.

First go to your instance in the portal and on the side panel scroll down to 'API', and create a key. There is more info of doing this here:

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

Then you can simply query the REST api like this:

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)
like image 20
Doug Avatar answered Nov 14 '22 20:11

Doug