本文是Azure Application Insights REST API的简单介绍,并会包含一个经过Python消费API的示例/小工具。html
新加入的team中的一项工做是制做平常的运维报表,制做方式是手工前往portal.azure.com,在网页中屡次执行不一样的查询语句、导出excel,以后再人工进行合并、分组、汇总、分析等等。这是一个繁琐的过程,其中大部分步骤其实不值得花费人工,应该交给程序。为了自动化这一过程,下降报表的制做成本,我尝试使用了Azure Application Insights REST API查询数据,使用python客户端进行处理、输出。下面把相关的一些知识和经验写在这里。python
本文连接:http://www.javashuo.com/article/p-tilvbbli-gc.html json
原创内容,转载请注明api
Application Insights是Azure平台的监控功能的一部分,用于收集、分析和处理来自Azure或其它本地环境的遥测数据。它包含强有力的分析工具,能够帮助你发现问题、诊断问题、理解用户在app上的行为,能够支持你持续地改进应用的性能和可用性。它能够和DevOps过程集成,和不少开发工具备链接点。并发
它支持多种语言和框架,好比.NET, Java, 和Node.js等。app
更多信息,参考:What is Application Insights?框架
除了在Azure中使用外,Application Insights收集的数据也能够经过REST API获取,这使得你能够用本身的其它应用来使用相关数据。API能够分为3种:运维
Metrics: 用于查询聚合结果,好比必定时间范围内的系统异常总数量。async
Events: 使用OData语法访问event数据,支持$filter, $orderBy, $search, $apply, $top, $skip and $format,能够返回单独的event数据或者event集的聚合数据。ide
Query: 容许用户发送和在Application Insights Analytics中同样的Query查询数据,返回数据的同时也会返回数据的schema。这是我用到的类型。
API的格式以下,
https://{hostname}/{api-version}/apps/{resource}/{area}/[path]?[parameters]
其中,
(这里是有关Public API format的部分,此外还有Azure API format)
须要使用上文提到的Application ID和下面提到的API Key来访问API,不然调用接口会失败,返回认证错误的消息,好比,
在API Access选项下选择Create API key,填写描述并勾选"Read telemetry"。
点击Generate key,会获得一个key字符串。注意,在这里必须保存key,由于关闭页面以后,没法经过任何方式再查询到生成的key。若是key丢失,只能重建另外一个key。
有了Application ID和API key,就能够访问API了。
这个页面有一个很好的例子,能够参考:
能够用postman之类的工具测试http请求。
注意,query中使用的Kusto查询引擎是一个即席查询引擎(ad-hoc query engine),它会尝试在内存中保存所有相关数据来知足查询,这致使查询可能会无限制地占用服务资源,带来风险。所以,Kusto提供了一些内置的查询限制以免风险。好比,单次查询的结果大小不能够超过64MB。
更多限制,请参考:Query limits
由于程序可能须要对不一样的Application Insight的不一样的API执行不一样的Query,所以,基本的处理思路是在配置文件中配置相关信息,程序从配置文件中读取须要执行的所有query,逐一查询后,返回结果列表。
下面是json格式的配置文件(profile.json)和python代码。
{ "application_insight": { "host": "api.applicationinsights.io", "apps": { "my-app-insights": { "id": "d1e9f429-c437-6034b32df878", "description": "it is an example", "apis": { "exception_monitor": { "description": "daily report", "key": "01234qwerrttyypolmknbshjdfggu", "version": "v1" } } } }, "queries": [ { "name": "query1", "app": "my-app-insights", "api": "exception_monitor", "statement": "exceptions | where operation_Name == \"\"", "time_field_name": "timestamp" }, { "name": "query2", "app": "my-app-insights", "api": "exception_monitor", "statement": "exceptions | where operation_Name contains \"AdapterV1\"", "time_field_name": "timestamp" } ], "default_filter": { "time_range": "between( endofday( now(), -8) .. endofday( now(), -1) )" } } }
说明,
查询代码以下:
import requests import json import asyncio async def request_get(url, headers, name): return {name: json.loads(requests.get(url, headers=headers).text)} async def __execute_query(config): default_filter = config["default_filter"] http_requests = [] for query in config["queries"]: app = config["apps"][query["app"]] api = app["apis"][query["api"]] query_url = f'''https://{config["host"]}/{api["version"]}/apps/{app["id"]}/query?query={query["statement"]}''' if query["time_field_name"] and default_filter["time_range"]: query_url = query_url + f''' and {query["time_field_name"]} {default_filter["time_range"]} ''' headers = {'X-Api-Key': api["key"]} http_requests.append(request_get(query_url, headers, query["name"])) return await asyncio.gather(*http_requests) def execute_query(): with open('profile.json', 'r') as config_file: query_config = json.load(config_file)["application_insight"] return asyncio.run(__execute_query(query_config))
基本思路是从配置文件加载queries,逐个放入任务列表中,最后统一并发执行、获取结果。
其中使用了request发送http请求、asyncio实现并发。
本文是我关于Azure Application Insights REST API的知识和实践的总结。这不是Azure Application Insights REST API的所有,能够参考微软文档以获取更多信息。