Azure Application Insights REST API使用教程
本文是Azure Application Insights REST API的简单介绍,并会包含一个通过Python消费API的示例/小工具。
新加入的team中的一项工作是制作日常的运维报表,制作方式是手工前往portal.azure.com,在网页中多次执行不同的查询语句、导出excel,之后再人工进行合并、分组、汇总、分析等等。这是一个繁琐的过程,其中大部分步骤其实不值得花费人工,应该交给程序。为了自动化这一过程,降低报表的制作成本,我尝试使用了Azure Application Insights REST API查询数据,使用python客户端进行处理、输出。下面把相关的一些知识和经验写在这里。
本文链接:https://www.cnblogs.com/hhelibeb/p/11543295.html
原创内容,转载请注明
Application Insights
Application Insights是Azure平台的监控功能的一部分,用于收集、分析和处理来自Azure或其它本地环境的遥测数据。它包含强有力的分析工具,可以帮助你发现问题、诊断问题、理解用户在app上的行为,可以支持你持续地改进应用的性能和可用性。它可以和DevOps过程集成,和很多开发工具有连接点。
它支持多种语言和框架,比如.NET, Java, 和Node.js等。
更多信息,参考:What is Application Insights?

Application Insights REST API
除了在Azure中使用外,Application Insights收集的数据也可以通过REST API获取,这使得你可以用自己的其它应用来使用相关数据。API可以分为3种:
Metrics: 用于查询聚合结果,比如一定时间范围内的系统异常总数量。
Events: 使用OData语法访问event数据,支持$filter, $orderBy, $search, $apply, $top, $skip and $format,可以返回单独的event数据或者event集的聚合数据。
Query: 允许用户发送和在Application Insights Analytics中一样的Query查询数据,返回数据的同时也会返回数据的schema。这是我用到的类型。
格式
API的格式如下,
https://{hostname}/{api-version}/apps/{resource}/{area}/[path]?[parameters]
其中,
- hostname: api.applicationinsights.io
- resource: Application ID ,也就是你的Application Insights app的唯一标识符,可以在app的API Access选项中看到,见下图。(注意:这不是Instrumentation Key,不要用错)
- api-version: 路径中需要包含API versions,Beta或v1。
- area: 3中查询类型之一metrics, events或query。
- path: 查询的详细信息,比如要查询哪个metric。
- parameters: 和path相关的具体参数。

(这里是有关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
自己写的query工具
因为程序可能需要对不同的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) )"
}
}
}
说明,
- host:固定值http://api.applicationinsights.io
- apps:Application Insight相关数据。
- apis:Api相关数据。
- queries:需要执行的query。
- default_filter:默认的查询条件,目前只有默认时间功能,例子里的条件是最近7个整天。
查询
查询代码如下:
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的全部,可以参考微软文档以获取更多信息。
Azure Application Insights REST API使用教程的更多相关文章
- 【Azure Application Insights】在Azure Function中启用Application Insights后,如何配置不输出某些日志到AI 的Trace中
问题描述 基于.NET Core的Function App如果配置了Application Insights之后,每有一个函数被执行,则在Application Insights中的Logs中的tra ...
- 【应用程序见解 Application Insights】使用Azure Monitor Application Insights Agent获取Azure VM中监控数据及IIS请求指标等信息
问题情形 为了使用Application Insights也可以监控Azure VM中的相关性能数据,如CPU, Memory,IIS Reuqest等信息,可以在VM中开始一个一个扩展插件: Azu ...
- Azure Monitor(一)Application Insights
一,引言 Azure Monitor 是 Azure 中的一项完整堆栈监视服务,是一种收集和分析遥测数据的服务.它提供了一组完整的功能来监视 Azure 资源以及其他云中和本地的资源.Azure Mo ...
- 【Azure 应用程序见解】 Application Insights 对App Service的支持问题
问题描述 Web App 发布后, Application Insights 收集不到数据了 问题分析 在应用服务(App Service)中收集应用的监控数据(如Request,Exception, ...
- 【Azure 应用程序见解】Application Insights Java Agent 3.1.0的使用实验,通过修改单个URL的采样率来减少请求及依赖项的数据采集
问题描述 近日好消息,如果是一个Java Spring Cloud的项目,想使用Azure Applicaiton Insights来收集日志及一些应用程序见解.但是有不愿意集成SDK来修改代码或者配 ...
- Azure 应用服务中的 API 应用、ASP.NET 和 Swagger 入门
学习内容: 如何通过 Visual Studio 2015 中的内置工具在 Azure 应用服务中创建和部署 API 应用. 如何使用 Swashbuckle NuGet 包动态生成 Swagger ...
- 【应用程序见解 Application Insights】Application Insights 使用 Application Maps 构建请求链路视图
Applicaotn Insigths 使用 Application Maps 构建请求链路视图 构建系统时,请求的逻辑操作大多数情况下都需要在不同的服务,或接口中完成整个请求链路.一个请求可以经历 ...
- 使用Application Insights 做分析
Application Insights on Windows Desktop apps, services and worker roles : https://azure.microsoft.co ...
- 关于Application Insights遥测功能使用【遇到问题】
简介:Application Insights是微软发布的一个在线服务,可以监测自己的网站应用,进行性能管理以及使用分析. Application Insights功能一开始是出现在Visualstu ...
随机推荐
- CentOS 安装 JDK 三种形式详细总结
一.下载 JDK 点击下载:jdk-8u211-linux-x64.tar.gz 根据需要选择对应版本和位数,并将文件放入CentOS中的相关目录中,以 /java/jdk 目录为例,执行 m ...
- python面向对象初始进阶版 通过一道题带你认识面向对象
定义一个类 class Person: #公共属性 animal='高级动物' soul='有灵魂' language='语言' def init(self,country,name,sex,age, ...
- OpenResty 社区王院生:APISIX 的高性能实践
2019 年 7 月 6 日,OpenResty 社区联合又拍云,举办 OpenResty × Open Talk 全国巡回沙龙·上海站,OpenResty 软件基金会联合创始人王院生在活动上做了&l ...
- 解决问题:安装Ubuntu时遇到nouveau sched_error问题
当我们在为有独立显卡gpu的电脑安装Ubuntu系统时,有可能会遇到nouveau sched_error问题. 方法一:安装系统时,开机出现dell图标时长按F12进入启动项选择界面, 然后选择UE ...
- 网站设计:将Footer固定在浏览器底部
在设计网站的时候,如果你某个页面的内容没有满屏,那你的footer会离浏览器底部很远,整体看起来很难看,这里用JavaScript提供一种方法来将footer固定在浏览器底部. function fi ...
- aabccd统计每个字符出现的次数,结果显示{ a: 2, b: 1, c: 2, d: 1 };去掉重复的字符,使结果显示abcd
遍历字符串的方式和遍历数组的方式有点相似,或者说就是相同的.在学习数组的遍历方法之前,可以通过for循环去遍历数组,同样,字符串也可以:字符串跟数组都有一个length的属性.下面代码奉上,个人思路! ...
- java高并发系列 - 第31天:获取线程执行结果,这6种方法你都知道?
这是java高并发系列第31篇. 环境:jdk1.8. java高并发系列已经学了不少东西了,本篇文章,我们用前面学的知识来实现一个需求: 在一个线程中需要获取其他线程的执行结果,能想到几种方式?各有 ...
- python 28 网络协议
目录 网络协议 1. C/S.B/S 架构 1.1 C/S 架构: 1.2 B/S 架构: 2. 网络通信原理 3. OSI七层协议(TCP/IP五层): 3.1 物理层: 3.2 数据链路层: 3. ...
- 新手学习FFmpeg - 调用API完成录屏
调用FFMPEG Device API完成Mac录屏功能. 调用FFMPEG提供的API来完成录屏功能,大致的思路是: 打开输入设备. 打开输出设备. 从输入设备读取视频流,然后经过解码->编码 ...
- CAD数据分块,偏移校准,加载到百度地图、高德地图、谷歌等地图上
前面分享过一篇如何将CAD海量数据显示在百度地图上(百度地图Canvas实现十万CAD数据秒级加载),但是很多开发者在CAD数据提取时遇到了问题,所以接下来的文章将介绍如何将CAD数据提取. 准备软件 ...