Author: Antony Augustus/Thursday, October 25, 2018/Categories: General
Application insights is a great measurement tool which provides you more insights into the usage of your application. This will help you solve exceptions; performance problems and it can even be helpful to improve the user experience of your application. This service can be used for several types of applications, web, mobile and windows applications. Multiple programming languages are supported like .NET, PHP, Python, Ruby and many more. Application Insights is a tool for developers.
The default installation of Application Insights collects several kind of telemetry data:
Page views
Speak for itself, tracks all page views.
Requests
All requests, pages, images, stylesheets, scripts, etc.
Exceptions
All requests that return exceptions are tracked. 400 and 500 status codes.
Dependencies
Queries to the database or calls to external web services.
Trace
Trace information is tracked.
Events
Events can be tracked via the API. I'll explain how you can do this in the next section.
Performance counters
Performance counters provides telemetry data about your servers.
Availability
Availability tests can be created in the portal. This can be either URL ping tests or multi-step tests.
Telemetry initializers can be useful to enrich the telemetry data that is pushed to Application Insights. The Microsoft.ApplicationInsights.Web package includes initializers that we can use out of the box. We can enable those in the applicationinsights.config or via code. Let's look at the ClientIpHeaderTelemetryInitializer which ships with the package. This class inherits the WebTelemetryInitializerBase class, the OnInitializationTelemetry method needs to be implemented. This method will set the additional information on the telemetry data.
/// </summary>
/// </summary> /// <param name="platformContext">Http context.</param> /// <param name="requestTelemetry">Request telemetry object associated with the current request.</param> /// <param name="telemetry">Telemetry item to initialize.</param> protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry){ if (string.IsNullOrEmpty(telemetry.Context.Location.Ip)) { LocationContext location = requestTelemetry.Context.Location; if (string.IsNullOrEmpty(location.Ip)) { this.UpdateRequestTelemetry(platformContext, location); } telemetry.Context.Location.Ip = location.Ip; } }
<TelemetryInitializers> <Add Type="Microsoft.ApplicationInsights.DependencyCollector.HttpDependenciesParsingTelemetryInitializer, Microsoft.AI.DependencyCollector" /> <Add Type="Microsoft.ApplicationInsights.WindowsServer.AzureRoleEnvironmentTelemetryInitializer, Microsoft.AI.WindowsServer" /> <Add Type="Microsoft.ApplicationInsights.WindowsServer.AzureWebAppRoleEnvironmentTelemetryInitializer, Microsoft.AI.WindowsServer" /> <Add Type="Microsoft.ApplicationInsights.WindowsServer.BuildInfoConfigComponentVersionTelemetryInitializer, Microsoft.AI.WindowsServer" /> <Add Type="Microsoft.ApplicationInsights.Web.WebTestTelemetryInitializer, Microsoft.AI.Web" /> <Add Type="Microsoft.ApplicationInsights.Web.SyntheticUserAgentTelemetryInitializer, Microsoft.AI.Web"> <Filters>search|spider|crawl|Bot|Monitor|AlwaysOn</Filters> <Add Type="Microsoft.ApplicationInsights.Web.ClientIpHeaderTelemetryInitializer, Microsoft.AI.Web" /> <Add Type="Microsoft.ApplicationInsights.Web.OperationNameTelemetryInitializer, Microsoft.AI.Web" /> <Add Type="Microsoft.ApplicationInsights.Web.OperationCorrelationTelemetryInitializer, Microsoft.AI.Web" /> <Add Type="Microsoft.ApplicationInsights.Web.UserTelemetryInitializer, Microsoft.AI.Web" /> <Add Type="Microsoft.ApplicationInsights.Web.AuthenticatedUserIdTelemetryInitializer, Microsoft.AI.Web" /> <Add Type="Microsoft.ApplicationInsights.Web.AccountIdTelemetryInitializer, Microsoft.AI.Web" /> <Add Type="Microsoft.ApplicationInsights.Web.SessionTelemetryInitializer, Microsoft.AI.Web" /> </TelemetryInitializers> protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); TelemetryConfiguration.Active.TelemetryInitializers.Add(new ClientIpHeaderTelemetryInitializer()); }
Learn how Cloud Assert can build an effective Hybrid Cloud Platform
Filter out telemetry data with Telemetry Processors
With telemetry processors, you can filter out data that you don't want in Application Insights. There are two reasons why you should do this, first you don't want data in Application Insights that you will not analyse. The portal has some good search and filter capabilities, but less is more. Second, one of the goals when using Azure services should be reducing the costs. You'll be charged for the data that is pushed to Application Insights, you need to consider what data is valuable for you. Like the initializer, you can configure a processor either in the configuration file or via code. You can create your own processor by inheriting the ITelemetryProcessor interface. Below an example of filtering out all requests that return a 404 response code and how could configure the processor.
public class NotFoundFilter : ITelemetryProcessor
{ private ITelemetryProcessor Next { get; set; } public NotFoundFilter(ITelemetryProcessor next) { this.Next = next; } public void Process(ITelemetry item) { var request = item as RequestTelemetry; if (request != null && request.ResponseCode.Equals(((int)HttpStatusCode.NotFound).ToString(), StringComparison.OrdinalIgnoreCase)) { // To filter out an item, just terminate the chain: return; } // Send everything else: this.Next.Process(item); } } <TelemetryProcessors> <Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryProcessor, Microsoft.AI.PerfCounterCollector" /> <Add Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.AdaptiveSamplingTelemetryProcessor, Microsoft.AI.ServerTelemetryChannel"> <MaxTelemetryItemsPerSecond>5</MaxTelemetryItemsPerSecond> </Add> <Add Type="ApplicationInsightDemoSite.NotFoundFilter, ApplicationInsightDemoSite" /> </TelemetryProcessors> protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); TelemetryConfiguration.Active.TelemetryInitializers.Add(new ClientIpHeaderTelemetryInitializer()); var builder = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder; builder.Use((next) => new NotFoundFilter(next)); builder.Build(); }
A lot of telemetry data is collected and available in Application Insights. An API is provided by Microsoft to customize tracking, for example, enrich the telemetry data with other useful information, filter out data and push your own data to Application Insights.
In a web application, we either can use the JavaScript API or the C# API. Whether you are using the client or server-side API, it's super simple. Events can be used to provide more insights how visitors are using your web application. Again, we can push events both client and server side. An example of a client-side event can be that a visitor opens/close a popup or use filters/categories on a complex search page. Let's start with an example how you can push a client-side event. In the code snippet, an event is pushed when an anchor with the class 'home-page-link' is clicked.
$(document).ready(function() {
$(".home-page-link").click(function() { window.appInsights.trackEvent("homePageLinkClicked", $(this).text()); }); }); [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return View(model); } // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, change to shouldLockout: true var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); switch (result) { case SignInStatus.Success: return RedirectToLocal(returnUrl); case SignInStatus.LockedOut: return View("Lockout"); case SignInStatus.RequiresVerification: return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); case SignInStatus.Failure: default: var dic = new Dictionary<string, string>(); dic.Add("email", model.Email); _telemetryClient.TrackEvent("InvalidLogin", dic); ModelState.AddModelError("", "Invalid login attempt."); return View(model); } }
By default, the post parameters aren't pushed to Application Insights. We could send this information in two ways. We can create a telemetry initializer and enrich the telemetry data with the post parameters. Or we can use the Track method (JavaScript or C#) to push this information to Application Insights.
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken] public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { var postVariables = Request.Form.ToString(); var dic = new Dictionary<string, string>(); dic.Add("Post", postVariables); _telemetryClient.TrackTrace("Forgot password url", dic); var user = await UserManager.FindByNameAsync(model.Email); if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id))) { return View("ForgotPasswordConfirmation"); } } return View(model); }
Events and trace information are just two examples of custom telemetry data that we can push ourselves. Other methods for pushing data are:
· TrackAvailability
· TrackDependency
· TrackException
· TrackMetry
· TrackPageView
· TrackRequest
Be aware that you don't track unnecessary data. Only do this when this can be valuable to solve issues, understand the usage of your application or other valid reasons.
Conclusion
Application Insights gives a lot of insights into the usage and performance of your application. You can investigate your data in the portal in several ways. When you're not satisfied with the telemetry data that's tracked by default, you can track telemetry data yourself by using the server or client-side API. This can help you solve exceptions and improve the performance of your application.
Number of views (7441)/Comments (12)
dissertationhelp
10/27/2018 5:29 AM
Not sure what the MP indicates right before telemetry; however; telemetry is communication(s) with an on the internet place where information transactions to. So, let's go with receptors on getaway. Receptors select up quantity of boosting, rate and impact. Once all information has been completed it is sent to another place, more than likely a pc so that it can validate if the facts is appropriate or wrong.
Muhammad Nawaz
11/11/2018 5:58 AM
The method requires supplying brief tеchnicаl help region to take part in an assessment this is certainly organized οf data when compared with intercontinental criteria, basically an outwardly facilitated sеlf evaluation. paraphrasing tool
11/27/2018 10:53 AM
I am Mary Lewis,an Expert Educational writer, I have been working in this field many years and I want to present the best academic help company is a world's leading online academic writing service
firefox
12/15/2018 5:00 AM
Hey, If you need to download free browser so here you can have Mozilla Firefox web browser which is one of the latest technology browsers and includes an advanced feature which is safe and secure to use. Also, the feedback of this browser is all positive
Academic Proofreading
1/17/2019 12:53 PM
Not certain what the MP shows just before telemetry; be that as it may; telemetry is correspondence with an on the web put where data exchanges too. Along these lines, how about we run with receptors on an escape. Receptors select up the amount of boosting, rate and effect.
pikbee
1/29/2019 12:07 AM
Thank you! I hope to see more updates from you from now on.
http://www.magzinepost.com/
2/5/2019 3:08 AM
I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.
back linking
2/21/2019 4:10 AM
This was among the best posts and episode from your team it let me learn many new things.
2/28/2019 3:10 AM
It’s very informative and you are obviously very knowledgeable in this area. You have opened my eyes to varying views on this topic with interesting and solid content.
Elly Wolter
4/29/2019 11:24 PM
You're probably fine forging ahead on your own, if calculations are your thing, though you might want to keep a tax professional on speed-dial because you'll probably have at least a few questions as you go along. Otherwise, you might want to pay someone to deal with your return, especially for the 2018 year here https://123helpme.org/articles/write-my-speech/ .
Olivia Green
5/14/2019 7:59 PM
Sometimes it's impossible to go on without good assignment help writing service, which you may find at the https://primeessays.com/ . This writing service writes only original papers, plagiarism-free and for a reasonable prize.
Lily Brown
6/7/2019 4:39 AM
Hey there! When you see that writing papers take so much of your time then it's better to apply to the https://essaysprofessor.com/ This is one of the best writing services that provide professional help for people from all over the world. I hope this service can help you too.