5

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 ?

explorer
  • 737
  • 1
  • 8
  • 23
  • Can you provide more details on the scenario/goal (e.g. integration with other tools such as Remedy, Splunk, etc.)? Have you consider configuring the Application Insights Continuous Export to push the data to a Azure Storage account (longer retention, cheap, etc.) and read from there? Would that approach meet your scenario/goal? – Evandro de Paula Jun 07 '18 at 06:07
  • I have a python APIs sending metrics from device running in Azure. I want to create similar python APIs to retrieve those metrics that are uploaded and analyze them. – explorer Jun 07 '18 at 14:26
  • I'm probably missing something, you can query/analyze (including building charts, etc.) the data using Log Analytics https://learn.microsoft.com/en-us/azure/application-insights/app-insights-analytics. – Evandro de Paula Jun 07 '18 at 14:59
  • so ...the github page has example of pythonic way to send metric to app insight ...tc.track_metric('My Metric', 42)...Now how I retrieve the same metrics in pythonic way... – explorer Jun 07 '18 at 15:40

3 Answers3

6

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)
Doug
  • 32,844
  • 38
  • 166
  • 222
3

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

ZakiMa
  • 5,637
  • 1
  • 24
  • 48