Last active
April 24, 2024 15:26
-
-
Save satheeshpayoda/92065d9fbaf5b0158728a8537d79af0e to your computer and use it in GitHub Desktop.
Simple Python Script to export the old data from Application Insight without reaching Application Insight's API limit. Exported data can be used for Deep analytics or for Archival purpose. More Details: https://payodatechnologyinc.medium.com/archiving-azure-application-insights-data-for-deep-analytics-cb2a656c1a21
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from datetime import datetime | |
from datetime import timedelta | |
import json | |
import sys | |
import argparse | |
import time | |
import csv | |
#Arguments Parser | |
parser = argparse.ArgumentParser(description='Export the data from Application Insight between from_range and to_range in split_range interval.') | |
parser.add_argument('app_id', type=str, help='Application Insight\'s Application ID') | |
parser.add_argument('api_key', type=str, help='Application Insight\'s API Key with Read Permission') | |
parser.add_argument('from_range', type=str, help='From Date Range. Format Eg: 2021-01-01T00:00:00Z') | |
parser.add_argument('to_range', type=str, help='To Date Range. Format Eg: 2021-01-02T00:00:00Z') | |
parser.add_argument('--split_range', type=int, default=3600, help='Split Range in seconds.To overcome the Application Insights API limits.Default: 3600 seconds (1 Hour)') | |
parser.add_argument('--save_prefix', type=str, default="data", help='File Name Prefix. Default: data') | |
parser.add_argument('--sleep', type=int, default=1, help='Sleep time between each API request. To overcome the Application Insights API limits.Default: 1 second') | |
parser.add_argument('--toCSV', type=bool, default=False, help='Convert the exported data into CSV format. Note:By deafult API returns in JSON format.') | |
args = parser.parse_args() | |
# Application Insight's API Endpoint | |
base_url='https://api.applicationinsights.io/v1/apps/%s/'%(args.app_id) | |
# URL Headers for API Request with API Key | |
headers={'X-Api-Key':args.api_key} | |
# Application Insight Tables | |
tables=['availabilityResults','browserTimings','customEvents','customMetrics','dependencies','exceptions','pageViews','performanceCounters','requests','traces'] | |
# From and End date range | |
from_date= datetime.strptime(args.from_range, '%Y-%m-%dT%H:%M:%SZ') | |
end_date= datetime.strptime(args.to_range, '%Y-%m-%dT%H:%M:%SZ') | |
while from_date < end_date: | |
# Looping through the 10 tables | |
for table in tables: | |
temp_date=from_date | |
# Acutal Application Query - in our case simply the table name | |
query={"query": table} | |
# Timespan argument for requesting api | |
timespan=temp_date.strftime('%Y-%m-%dT%H:%M:%S.000Z') | |
temp_date=temp_date + timedelta(seconds=args.split_range) # Add Split Range | |
timespan=timespan+"/"+temp_date.strftime('%Y-%m-%dT%H:%M:%S.000Z') | |
print(datetime.now(),table,timespan) | |
# Forming Query with timespan as paramter to end point | |
query_url='query?timespan=%s'%(timespan) | |
# Sending POST request with Query and headers | |
r = requests.post(base_url+query_url,json=query, headers=headers) | |
# Check the status of API Request | |
if r.status_code is 200: | |
# File Name to save the exported data | |
if not args.toCSV: | |
filename='%s-%s-%s.json'%(args.save_prefix,table,timespan.replace("/","_")) | |
else: | |
filename='%s-%s-%s.csv'%(args.save_prefix,table,timespan.replace("/","_")) | |
data=r.json() | |
if 'error' in data: | |
print(datetime.now(),"Error:",data['error']) | |
else: | |
with open("%s"%(filename),'w') as file: | |
if not args.toCSV: | |
json.dump(data,file) | |
else: # CSV Conversion | |
csv_headers=list() | |
#Get Columns | |
for column in data['tables'][0]['columns']: | |
csv_headers.append(column['name']) | |
csvwriter = csv.writer(file, delimiter=',',quoting=csv.QUOTE_MINIMAL) | |
csvwriter.writerow(csv_headers) | |
for row in data['tables'][0]['rows']: | |
csvwriter.writerow(row) | |
time.sleep(args.sleep) | |
# If api failed just log to stdout with status code and message | |
else: | |
print(datetime.now(),"API Failed:",r.status_code,r.text) | |
sys.exit(1) | |
from_date = from_date + timedelta(seconds=args.split_range) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment