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

3

6

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 ?

Propylaeum answered 7/6, 2018 at 3:21 Comment(4)
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?Wayne
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.Propylaeum
I'm probably missing something, you can query/analyze (including building charts, etc.) the data using Log Analytics learn.microsoft.com/en-us/azure/application-insights/….Wayne
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...Propylaeum
E
3

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

Eisler answered 8/6, 2018 at 17:27 Comment(0)
A
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)
Augustine answered 24/6, 2021 at 4:40 Comment(2)
Thank you, this is wonderfulBrake
Could you take a look at this modification? Your solution gives me column names: name, columns and rows. I pulled the d['name'] value from items in'logs['tables'][0]['columns'], and now have column names: timestamp, operation_Id, message (which I think are the intended column names).Barclay
E
3

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

Eisler answered 8/6, 2018 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.