來源: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
不需要代碼或配置更改