来源:https://stackify.com/application-insights-things-to-know/
Application Insights – 16 things every developer needs to know
每个开发人员需要知道的16件事
Matt Watson June 29, 2017 Developer Tips, Tricks & Resources
1. Make sure your app targets .NET 4.6, and .NET 2.0 is not supported确保你的应用平台目标。NET 4.6,。不支持NET 2.0
There are some changes in the 4.6 version of the .NET framework that enables some additional data collection abilities. Be sure to update your app and redeploy it. App Insights will work with 4.5 or newer but 4.6 is preferred.
在4.6版本中有一些变化。支持一些额外数据收集能力的网络框架。确保更新你的应用程序并重新部署它。App Insights最好是使用4.5或更新版本,但4.6是首选
I know it sounds crazy, but you wouldn’t believe how many people have older apps written in 2.0 that they still have to support and have never been upgraded. App Insights does not support 2.0 and they don’t have any plans to do so.
我知道这听起来很疯狂,但你不会相信还有多少人仍然使用基于2.0版本旧应用程序, 而且从来没有升级过,他们需要仍然支持2.0。不幸的是,App Insights并不支持2.0,也并没有要支持2.0的计划。
2. Warning! Sensitive data is not scrubbed from dynamic SQL queries
警告!敏感数据不会从动态SQL查询中删除
If your code uses dynamic SQL, Application Insights will collect the full query and upload it to Azure. This means if the query contained personal information, credit card numbers, or anything else that could be sensitive, it is getting uploaded from your server to Microsoft.
如果您的代码使用动态SQL,那么Application Insights将收集完整的查询并将其上传到Azure。这意味着,如果查询包含个人信息、信用卡号或其他敏感信息,那么它将从你的服务器上传到微软
Another thing to know on this topic, App Insights never collects SQL parameters.
关于这个主题,另一件需要了解的事情是,App Insights永远不会收集SQL参数
3. Key limitation: Finding slow SQL queries
关键限制:查找缓慢的SQL查询
Application Insights provides reporting down to the server and database name being accessed. There is no to see how long individual queries take, which is the slowest or being called the most often. There is also no way to set up monitoring for a specific SQL query.
Application Insights提供了对正在访问的服务器和数据库名称的报告。但我们不知道单独的查询需要多长时间,最慢的或者最频繁的查询。也没有办法为特定的SQL查询设置监视。
To get a list of slow SQL queries, you would need to query SQL Server’s DMVs or use a different APM solution. If you are using SQL Azure it has some built in tuning advisor functions for this too.
要获得一个慢SQL查询列表,您需要查询SQL Server的dmv(动态管理视图),或者使用不同的APM(应用性能管理)解决方案。如果你使用的是SQL Azure它也有一些内置的优化顾问函数
4. SQL query times are not always correct
SQL查询时间并不总是正确的
Only the amount of time to execute the query on the server is included in the timings. A simple query that selects a lot of data may show that it didn’t take very long at all, but may have taken a lot more time to actually download the query results.
只有在服务器上执行查询的时间,才包括在计时中。一个大量数据的简单查询可能表明它并不需要花费很长时间,但是可能需要花费更多的时间来实际下载查询结果
A simple example of of a query that took 83 milliseconds, but it took another 60 to download and iterate the results. Look for missing gaps of time in your requests. They could be caused by this scenario if they return a lot of data.
一个简单的查询示例,它花费了83毫秒,但是它又花了60个时间下载并迭代了结果。在请求中寻找丢失的时间间隔。如果他们返回大量数据,他们可能会被这个场景所影响
Learn more: How to measure real world SQL performance for ASP.NET
5. Azure App Services require a site extension for all features
Azure应用程序服务需要为所有特性提供一个站点扩展
You can add Application Insights to your project and deploy it to Azure as an App Service. App Insights will seem to work, but there are some things that don’t work until you install the site extension. You can install it via the Azure Portal. I specifically noticed full SQL queries were not collected without it.
您可以将Application Insights添加到您的项目中,并将其部署到Azure作为应用程序服务。Application Insights似乎会起作用,但在安装网站扩展之前,有些事情是行不通的。您可以通过Azure门户安装它。我特别注意到,如果没有它,就不会收集完整的SQL查询
6. View top exceptions in your application
查看应用程序中的顶级异常
Application Insights has good reporting to group application exceptions so you can uniquely see which ones are occurring and how often.
Application Insights对分组应用程序异常有良好的报告,因此您可以独特地看到哪些是正在发生的,哪些是经常发生的
How to send exceptions to App Insights
如何向Application Insights发送异常
To ensure that all of your exceptions are collected you must install their SDK and make some code and config changes.
为了确保所有的异常都被收集起来,您必须安装它们的SDK,并进行一些代码和配置更改
- Modify code to collect unhandled exceptions for MVC, global.asax, etc.
修改代码以收集未处理的MVC的异常,global.asax 等 - If you use log4net, NLog, etc you can configure it to send your exceptions
如果你使用log4net,NLog等,你可以参看文章“配置它来发送你的异常” - Manually report exceptions via their SDK if needed
如果需要的话,可以通过他们的SDK手动报告异常
Limitations and missing exceptions features
限制和缺失的异常特性
App Insights is missing some key features around exceptions. If you are having SQL timeouts, for example, odds are they impact multiple applications. App Insights has no reporting across apps to easily see this.
App Insights缺少一些例外情况的关键功能。例如,如果您有SQL超时,可能会影响多个应用程序。App Insights在应用上没有任何报告可以很容易地看到这一点
- No way to see exceptions across all applications
无法在所有应用程序中看到异常 - Some other APM solutions don’t require any code or config changes to collect unhandled exceptions
一些其他APM解决方案不需要任何代码或配置更改来收集未处理的异常 - No way to collect all “first chance” exceptions
没有办法收集所有“第一可能”的例外情况
Retrace can collect all exceptions, including first chance exceptions, with no code changes.
Retrace可以收集所有异常,包括第一可能异常,而不需要做代码更改
7. Asynchronous HttpClient calls are not supported
异步HttpClient调用不受支持
Almost all new ASP.NET development is now done in async and uses the common async/await pattern. Unfortunately, it does not support the recommended way to make HTTP calls with ASP.NET.
现在几乎所有新的ASP.NET开发已经在异步中完成,并且使用了通用的async异步/await等待模式。不幸的是,它不支持使用ASP.NET推荐方法进行HTTP调用的
public async Task HttpClientAsync()
{
log.Debug("Starting HttpClient.GetStringAsync()");
string data;
using (HttpClient hc = new HttpClient())
{
data = await hc.GetStringAsync("http://microsoft.com");
}
log.Debug("Completed HttpClient.GetStringAsync()");
return Request.CreateResponse(HttpStatusCode.OK, data);
}
App Insights does pick up the 2 logging messages via log4net, but doesn’t show anything for dependency calls.
APP Insights通过log4net获取了2条日志消息,但没有显示依赖性调用的任何内容。
It does see it, it just doesn’t associate it with the ASP.NET web request. ☹
它确实看到了,它只是不和ASP.net web请求联系起来
8. Tracking application dependencies
跟踪应用程序的依赖关系
App Insights supports tracking dependency calls in your code. Microsoft’s documentation says SQL databases, HTTP calls, and Azure storage libraries are supported. Anything else requires using their SDK to manually track each dependency call.
App Insights支持在代码中跟踪依赖项调用。微软的文档说支持SQL数据库、HTTP调用和Azure存储库。其他方面需要使用SDK来手动跟踪每个依赖项调用
Limitations
限制
App Insights does not support some other notable key Azure dependencies include Service Bus and redis.
App Insights不支持一些著名的Azure依赖项,包括服务总线和redis。
Some other APM solutions, like Retrace, also support MongoDB, RavenDB, Elasticsearch, Redis, Memcached, AppFabric, MSMQ, NServiceBus, AWS services and more.
其他一些APM解决方案,比如Retrace,也支持MongoDB、RavenDB、Elasticsearch、Redis、Memcached、AppFabric、MSMQ、NServiceBus、AWS服务等。
9. View all details for a single web request (transaction trace)
查看单个web请求的所有细节(事务跟踪)
One of the key features to help track down what a request does is seeing the complete picture of logging, dependency calls, and other key events associated with a single web request.
帮助跟踪请求的主要功能之一是查看日志记录、依赖项调用以及与单个web请求相关联的其他关键事件的完整图片
The best way to do this in Application Insights is to click “All available telemetry for this operation”.
在Application Insights中,最好的方法是点击“所有可用的远程遥测技术”
10. How to Identify why a request or entire application is slow
如何识别请求或整个应用程序的缓慢
One suggestion is to customize the chart that you see when you filter for a specific operation to add the “dependency duration”. This will help you see how that compares to overall server response time.
一个建议是定制筛选特定操作时所看到的图表,以添加“依赖持续时间”。这将帮助您了解如何与总体服务器响应时间进行比较
The built-in map feature can be useful for trying to identify the performance of dependencies. It will tell you the average time and % of failures.
内置的映射特性对于识别依赖关系的性能非常有用。它会告诉你平均的时间和失败的百分比
I would also suggest going to “Failures” from the menu to look for dependency failures and exceptions.
我还建议从菜单中选择“失败”,以查找依赖项失败和异常。
One thing that would be nice is a visual way to see how each dependency impacts overall performance. When there is a problem with one it is usually easy to see it spike.
有一件事是很好的,它是一种可视化的方式来查看每个依赖关系对整体性能的影响。当存在一个问题时通常很容易看到它突出显示
Screenshot from Retrace
11. Monitoring & Alerts features and limitations
监视和警报功能和限制
Application Insights supports configuring alerts for a lot of different conditions. Including alerting on metrics like Server Exceptions, Request Rate, Process CPU, etc.
Application Insights支持为许多不同的条件配置警报。包括服务器异常、请求率、进程CPU等指标的警报
Monitoring limitations
监测的局限性
- No way to monitor a specific request (URL/action)
无法监视特定的请求(url/动作) - No way to monitor a specific SQL query
无法监视特定的SQL查询 - No way to monitor % of requests having errors
无法监控有错误的请求的百分比 - Can only monitor averages
只能监控平均数
Alerting limitations
报警限制
- Email or webhooks only
仅用电子邮件或网络连接 - Can use a Logic App to send to SMS, Slack, and others
可以使用逻辑应用发送短信、Slack(国外流行的一种企业级沟通工具)和其他应用 - No built-in concept of alert “escalation”
没有内置的警报“升级”概念 - No concept of alert severity like warning vs critical
没有像警告和批评这样的警告严重的概念 - Alerts go to everyone or a hard-coded list of emails. No good way to manage distribution lists unless done outside via email groups.
警报会发送给每个人或硬编码的电子邮件列表。除非通过电子邮件组进行外部操作,否则无法管理分发列表。
12. Reporting across applications
报告跨应用程序
In Azure Application Insights, each app is more or less a “bucket” that all the diagnostics about each app gets reported to. There is no easy way to see exceptions, slow SQL queries, slow web requests and etc across all of your apps. The only workaround that can provide some very minimal visibility is making a custom dashboard and selecting some widgets across multiple apps.
在Azure应用中,每一款应用都或多或少都是一个“木桶”(木桶理论),每个应用程序的所有诊断信息都会被报告。在所有的应用程序中都没有简单的方法可以看到异常、缓慢的SQL查询、缓慢的web请求等等。唯一能够提供非常低的可见性的解决方案是制作一个定制的仪表板,并在多个应用程序中选择一些小部件。
13. No support for the concept of multiple “environments” (QA, Prod, etc)
不支持多个“环境”(QA、Prod等)的概念
Within an individual application, there is no high-level way to separate out and report data by the environment (i.e. Dev, QA, Prod). Microsoft has a couple workarounds that they recommend:
在一个单独的应用程序中,没有高级的方法可以根据环境(例如Dev、QA、Prod)来分离和报告数据。微软有几个变通方案:
- Configure a unique “application” via separate instrumentation keys. This works but then there is no reporting across them. They are completely isolated.
通过单独的插装键配置一个独特的“应用程序”。这是可行的,但是没有报告。他们是完全隔离的 - Use a custom field for the environment. You would then need to probably make your own dashboards and widgets in App Insights filtered down by that custom field.
为环境使用自定义字段。然后你可能需要在App Insights中创建你自己的仪表盘和小部件在这个定制字段中过滤掉
14. App Insights is not a code profiler
App Insights不是代码分析器
Most application performance management (APM) solutions use code profiling to do data collection. Microsoft Application Insights does not use profiling. Instead, it relies on collecting data via the .NET framework via ETW and other sources.
大多数应用程序性能管理(APM)解决方案使用代码概要来进行数据收集。Microsoft Application Insights不使用概要分析。相反,它依赖于通过收集数据来收集数据。通过ETW(Event Tracing for Windows 事件跟踪机制)和其他来源的网络框架
Because App Insights does not the CLR profiler, it causes a few limitations:
因为App Insights并不是CLR分析器,它会导致一些限制
- Lack of dependency support – The reason it does not support a wide array of 3rd party dependencies is primarily because they don’t do code profiling that would let them instrument the libraries.
缺乏依赖性支持——它不支持第三方依赖性的原因主要是因为它们不做代码分析,这样就可以让它们对库进行测试 - No custom profiling – One of the very useful features of APM solutions is being able to profile custom methods in your code to track their usage and performance. App Insights doesn’t support this. You would instead need to use their SDK and modify your code to report it.
没有自定义概要——APM解决方案的一个非常有用的特性是能够在代码中配置定制方法来跟踪它们的用法和性能。App Insights并不支持这一点。相反,您需要使用他们的SDK并修改代码来报告它
Microsoft is working on some profiling features and may offer it in the future.
微软正在开发一些性能分析功能,将来可能会提供这些功能。
15. Application Insights has a robust API, and you will likely need it
App Insights有一个健壮的API,您可能需要它
If your boss likes it when you spend all your time instrumenting your apps manually to report performance data, they will love Application Insights. They have a robust API and SDKs for several programming languages (.NET, Java, node.js) that lets you report requests, pages views, dependency calls, trace (log) messages, exceptions and more via their TelemetryClient.
如果你的老板喜欢这样做,当你把所有的时间都花在手动地检测你的应用程序上的性能数据时,他们会喜欢Application Insights。它们有一个健壮的API和用于几种编程语言的sdk(。NET、Java、node.js)允许您报告请求、页面视图、依赖项调用、跟踪(日志)消息、异常,以及更多通过它们的远程客户端
Microsoft’s own description highlights it as being “extensible” as it relies on a lot of custom SDK usages to get rich data.
微软自己的描述强调它是“可扩展的”,因为它依赖大量的定制SDK使用来获取丰富的数据
Application Insights is an extensible Application Performance Management (APM) service for web developers.
应用程序洞察是面向web开发人员的可扩展应用程序性能管理(APM)服务
If your app uses Elasticsearch, MongoDB, Redis, or any other dependency and you would like to see it show up in Application Insights on Microsoft Azure, you will need to change your code and manually report it.
如果你的应用程序使用了Elasticsearch,MongoDB,Redis,或者其他任何依赖项你希望看到它出现在微软Azure的应用中,你需要改变你的代码并手动报告它
16. How to use Application Insights with Windows Services
如何使用Windows服务的应用程序洞察力
If you have a Windows Service type application that you want to track with App Insights, you will have to integrate their API into your app. Every single place you make a database call, you can simply add a couple lines of code to report the query via their SDK.
如果你有一个想要追踪App Insights的Windows服务类型应用,你需要将他们的API整合到你的应用中,每一个你做一个数据库调用的地方,你只需添加几行代码就可以通过他们的SDK报告这个查询
As an example, check out this article about using Microsoft Azure Worker Roles with Application Insights.
作为一个示例,请阅读本文中关于使用Microsoft Azure工作人员角色和应用程序洞察力的文章
Need more than Application Insights?
需要的不仅仅是应用程序的洞察力
Application Insights is awesome. However, like any application, it has its limitations and best use cases. I would describe App Insights as a “light APM” solution. If you want to take the time to use their SDK, you can make it do a lot of different things.
应用程序的见解太棒了。然而,与任何应用程序一样,它也有其局限性和最佳用例。我认为应用洞察力是一种“轻APM”解决方案。如果你想花时间去使用他们的SDK,你可以让它做很多不同的事情
Visual Studio Application Insights
App Insights has a Visual Studio plugin to show some data in the developer’s IDE. If you are looking for a tool to help you during the development experience, be sure to check out Prefix. It is the #1 tool for helping to optimize and prevent application problems as you test and write your code.
Visual Studio有一个App Insights插件,可以在开发者的IDE中显示一些数据。如果您在开发过程中寻找一个工具来帮助您,一定要检查前缀。在测试和编写代码时,它是帮助优化和防止应用程序问题的工具
Azure Insights
However, if you want the best APM available for ASP.NET developers and azure insights, try Retrace. It was designed with the entire goal of finding and fixing application problems as fast as possible. It is actually written in ASP.NET and is hosted on Azure. Monitoring Azure is our expertise!
但是,如果您希望使用ASP的最佳APM。NET开发者和azure insights,尝试重新追踪。它的设计目标是尽可能快地查找和修复应用程序问题。它实际上是用ASP写的。NET并托管在Azure上。监视Azure是我们的专长
Retrace Differences
追溯差异
- All major application dependencies supported
所有主要应用程序依赖项都支持 - Excellent reporting across apps and environments
出色的应用和环境报告 - Industry leading transaction trace views for developers
为开发人员提供行业领先的事务跟踪视图 - 100% support for async and .NET Core
100%支持异步.NET Core - Included and advanced logging functionality
包括和高级的日志功能 - No code or config changes required
不需要代码或配置更改