Profile & Monitor Web Apps with Azure Application Insights || Code Quality & Performance Conference
Nov 9, 2023
Profile & Monitor Web Application with Azure Application Insights: In the session, the below will be discussed
1. Introduction to Application Insights
2. Understand App Insight’s features for tracing Performance issues
3. Understand the architecture of how App Insights fits in a Web Application along with other Azure Components
4. Learn Distributed Tracing of the Application with end-to-end co-relation of the various Components
5. How Application Map provides performance metrics of Individual Components
6. Learn how to Enable Profiling for Windows and Linux apps hosted in Platform as a Service
7. Enable the Profiler, Simulate traffic, troubleshoot the app performance and identify the exact line of the code that’s causing the performance bottlenecks
Conference Website: https://globaltechconferences.com/event/code-quality-performance-virtual-conference-2021/
C# Corner - Community of Software and Data Developers: https://www.c-sharpcorner.com
C# Live - Dev Streaming Destination: https://csharp.live
#webapps #Codequality #opensource #Architecture #Azure
Show More Show Less View Video Transcript
0:00
Hi everyone, good morning, good evening. So today we are going to learn about application
0:09
insights profiling. Before we start, a quick intro with myself and thanks for the nice
0:22
and yeah I am Pravind Shira, and I am working as a cloud solution architect and developer
0:31
So that's a good introvert lesson. And yeah, today our topic is profiling using application insights
0:40
So before you dive into different terms, so let's understand what is profiling
0:46
So in software engineering, profiling is a form of ysis, some kind of ysis to a code that measures different metrics like CPU percentage, memory, data applications, consumer
1:05
So today we are going to learn how to profile an application running inside iShow with the
1:16
help of a service called Application Insights. It's very easy, you don't have to install anything on your phone, all the tools required
1:27
for profiling the application are put into an Application Insights service on the Linux
1:33
So, let's dive in. So, today we are going to learn about our overview of App Insights
1:42
Before we do profiling, we first understand what is App Insights and how it fits into
1:50
various components in a LinkedIn project. I will just take a simple application with various components and I will show you where
1:58
App Insights fits in that application. And then we will discuss about some of the app insight features that can be used to reveal performance
2:10
And then we will understand how to enable profiling. There are different things that we should do depending on whether it is Windows operating
2:22
system or Windows operating system. That we will understand what it does to do
2:28
And then we will try to see if you can simulate some traffic
2:32
We already simulated some very basic traffic. So let's first understand what is Azure Application Insights
2:46
So if you look at this diagram, this is Application Insights Service
2:50
It's a PaaS offering by Microsoft. Now, once you create this application insights, it provides us, it automatically tracks the
3:04
application telemetry and any other custom application logs that our apps would push
3:11
So if you see this diagram here, this is a front end probably in this application
3:16
It could be an Angular application or it could be any other application
3:21
So all we need to do is install an instrumentation package. For .NET it's a new get package
3:28
For Angular it could be an IPM package. For Java it could be some kind of package
3:36
So developer needs to add that package and write one or two lines of code
3:43
All the telemetry, everything will be automatically pushed to add insights as synchronous
3:50
So we can integrate App Insights into our web applications, or APIs, or even background services
3:59
It could be any console application, a background application, or a Xamarin application, anything
4:05
So once we integrate that, the application will start pushing the telemetry logs to App Insights
4:15
And from here, basically, this App Insights stored all the data. Of course, it provides many built-in reports
4:22
You can yze the data and you can get insights about our application performance
4:29
exceptions, top three exceptions, top three exception reports, response reports, top three
4:35
explore any pages. All that information you get out of the box from application insights without we creating
4:42
any reports. Now, apart from using the default reports, what we can also do is we can do all this
4:51
We can get alerts, we can get custom reports in Power BI, we can export the data and put
4:59
it in somewhere and we can do further ysis. all that, all those integrations are possible and they are seamless to enjoy it
5:11
So this is a simple application that I have done where we create various services. I already
5:22
created all these services. The web application which talks to a database. The web application
5:28
also talks to some storage with all Azure services. And it talks to an API. It could be our own API or it could be an external API as well
5:40
And probably you are using some base cache. So in real time an application or an API can talk to many different services
5:51
So now you probably want to track the performance of each of the calls made to all the services
5:58
The problem with web application is making some database calls, database chips
6:04
Our application might be uploading some files to storage account. Storage is another service in Azure
6:12
Our application might be talking to an API, it is a NIST client
6:18
It could be our API or it could be an API managed by someone
6:22
So now we want to track all the exceptions, logs, performance metrics, everything
6:29
So all that can be done very easily with the help of App Insights
6:35
All we need to do is link our web app with App Insights with the help of a connection
6:41
string with the help of a key called instrumentation key. Every App Insights will have a unique instrumentation key that should be plugged inside the web application
6:53
Along with that instrumentation key, you also need to add the new get package in just one
7:00
line of code. just need to register app application in a service in your application
7:07
So now, when an application is making calls to storage, database or APIs, automatically
7:15
all the calls are tracked out of the box by App Insights without being able to hold
7:20
Unfortunately, there are some services which are not tracked automatically. For example, today's
7:24
So as of today, the calls that you make today's are not tracked automatically by App Insights
7:30
So you should write some custom code for that. Even if there is another service called Key Vault, Key Vault is tracked by our app inside automatically
7:42
Not everything is tracked automatically. So there are some services like Webiscast which is called not tracked automatically
7:50
by App Insight. Of course you can write your custom code. If you want to love those metrics, performance metrics or anything else, you have to write
8:00
So let's explore the code, it's very simple code. So let me switch to my visual studio
8:10
I'm using the .NET Core 3.1 application. And if you look at this is my web application and I have installed it in the get package This is specific to Core but similarly if you are using Java based or PHP based or
8:29
Angular, very simple. Similarly you have to download the proper packages. So this particular package is sufficient for tracking everything except profiling
8:41
So for profiling, you have to add additional package. These two are required for a profile or application
8:52
Once you add these libraries, you have to add the instrumentation key for our application results
9:05
And then, you have to add one line of code to track all the default
9:11
termimetry for App Insights. But if you are interested in the profiler, you have to add this line of code
9:17
That's it. Now, entire application, whatever calls it, application needs, all those will track asynchronous
9:27
So, adding application insights will not add any additional load to the application
9:36
There might be slight load but it's negligible because all the telemetry will be pushed as
9:44
synchronous to the lab insights. And once you add this in startup class, all you need to do is controller classes, action
9:57
methods, you just need to have ilogger and you can use ilogger wherever you want
10:04
For example, you can track the exceptions and push these exceptions in case there is
10:10
an exception, these messages will be pushed. you can also log your custom messages as well, just one line of code
10:19
Now we have . We have various methods for various messages, logcritical, logdevert, loggerash, and four-trays in one
10:32
All of them are loggerash. But for profiling, which is our topic today
10:38
topics today. We don't have read any line of code except this. Now in order to show some code, I have
10:49
added some thread dot sleeves. But in real time there could be a lot of complex logic in the
10:56
application. So there in case if the developer did not write the code properly, all that will be
11:05
shown in the profiler once you've enabled. So this is the code that I have done. I have
11:12
a couple of controllers and I will be running this application very soon. Let's go back
11:23
Now before even we talk about profiler, I would like to explain some nice features where
11:29
we can get performance metrics. If you say this application, this is a screenshot taken by one of the features called application map
11:38
So I will also show you the screenshots on how to navigate to this application map feature
11:46
in the portal in just a moment. This is the application map
11:50
If you see this, these are web application which is Tockel to database
11:58
You see this, there are 10 calls made by this application in this screenshot
12:04
And some of them are fade, 11% of them were filled. And 10% of the database called were fade
12:12
And it was taking 11.2 milliseconds on average. Some of them might have taken no time and some of them might have taken less time
12:21
But overall, on average, this is what database calls, which is possible
12:26
But in case if overall, by looking at this application map, you can see where exactly
12:33
the performance button is solved right. So, here it's making a call to storage and the average amount of time is 32 million seconds
12:42
So, at a high level, when our application is talking to multiple components, here this
12:49
view gives us which component calls are taking time. Right now everything looks good but in
12:58
real time there's a lot of code is in place and if it is not optimized code then looking
13:05
at this you understand okay calls to storage are the culprit. Calls to database is the culprit
13:13
one view where you can get the insights about performance that field is called as application
13:22
right and now this is at a very high level per component but what if you want to yze an
13:31
individual transaction, individual request that request may be making calls to multiple components
13:37
properties are uploaded to storage and once the file is uploaded to storage, probability
13:42
is making a database update and once that is done probability is making call to another
13:48
API. A lot of components might be involved in one transaction or one request. So how
13:53
do you do that? So there is one more feature called distributed tracing. So I have captured
14:02
couple of slides. So this one request, so end-to-end transaction. This feature is called
14:12
as distributed tracing wherein you track a request and you see this is web application
14:21
and this is my action method, this is a URL which made calls to multiple different components
14:27
suppose there is a middle and but it made calls to storage multiple times these are all storage
14:34
account calls and then you have a call to database in that SQL server. Now here you see this it is
14:46
giving us duration that it took for every other entry here you are seeing empty spaces but if you
14:54
So click on this, on the right hand side you will see what exactly that call was
14:59
I was trying to create some storage related components. So it was taking around 49 milliseconds for that operation and we can use the database call was hardly 3 milliseconds
15:10
But now, this is another view. It's a single request which is making lot of interactions with various components
15:18
Like overall it took 10.2 seconds but if you combine all this it hardly comes to one second
15:27
So where is that next nine seconds? That's a problem with this approach
15:32
So that nine seconds or 10 seconds is my weight sleep time
15:36
So by intentionally added some sleep. So that sleep is not shown here
15:41
But otherwise, this distributed tracing uses lots of insights about various components
15:47
that are being interacted in that particular single request. This is another view where you get performance numbers
15:55
In the previous view, you get insights about average metrics, average numbers for individual
16:05
component. But here, for a given request, you get the actual amount of time taken for
16:12
each component. If you have more components, you get all those here in a sequence. But
16:20
this is not giving some information about the calls which are not supported. For example
16:29
this call had made a call to contract with redis that would not be shown So at this point of time redis calls are not supported right so that is where profiler comes in the picture
16:46
right so if you see this small icon this is a profiler icon so by default you don't see this
16:55
you see this only when you enable profile so if you are not happy with these numbers and you still
17:02
want to research further, when you click on this clip, it will take you to another view
17:07
where you can see more details. For example, here you don't see how much time framework
17:13
is taking, .NET framework is taking for doing some other operations. Here you don't see
17:19
UI execution. But when you click on this, it shows all that. Of course, the propellers
17:27
are not 100% accurate because it's just a sampling, it captures only a sampling data
17:34
We will talk more about that in the comments that are supposed to know that
17:39
So now this icon is shown only when you enable profiling. So how do you enable profiling
17:52
So navigate to your app insights and profiling can be enabled by navigating to a blade called performance
17:59
So click on performance and you check into this blade where you see profiler, click on
18:06
that, it opens up a popup, it opens up another blade, in this slide you see that as a popup
18:14
By watching the portal you see this popup somewhere in the right side
18:19
Now if you see this, these are the two recent profiling sessions
18:24
It is triggered on demand by manually clicked on profile now and it is one time policy
18:32
That will show you what policy is on. And this app name is a server name and this is a time and there was no load during that
18:41
point of time. it will also show us if there is load while the profiler is capturing the data then it will show us how much CPU, what was the CPU load while that profiler session was running
18:56
So it can configure some policies here, you can extract map insights on when to start profiling session and if you want to do it right now then you can click on this profile ysis
19:10
Sometimes the profile will not work properly, so in that case you can use this pattern to
19:17
troubleshoot further. So now these are examples that I have captured
19:27
Because sometimes profile doesn't load up for various different reasons, but if you see
19:34
this, this is one of the profile traces for one of my calls
19:38
I am making a call to external API. So this is the URL for external API and it has taken 14 seconds approximately, out of
19:49
which 10 seconds is sleep time. It is giving us all the complete data
19:54
And if you observe this, these are hard parts. You see these frames
20:02
So when you come to this trace, the following days it directly shows us that this particular
20:08
particular request or this particular section has hard parts and this is where you should
20:14
troubleshoot failure. And these are good. These are some other framework related stuff for certain execution but this is where most
20:25
of the time was consumed for this particular request. And this is the actual wait time
20:32
because I have added sleep time and it has waited because it is making an external call
20:39
to some other area. So for all 10 plus 3 seconds it shows 13 seconds
20:44
Out of 14 seconds, entire 14 seconds for this request, 13 seconds were almost wait time
20:54
This is how our profiler helps us quickly get insights about the performance of our applications
21:04
This is one example. I have one more example where I made some calls for the database
21:13
This is our .NET code which is called in entity framework and internally calling this
21:18
to a post-stitution. So this took around 5 seconds, wherein unmanaged code, right
21:24
This is, unmanaged code is a catchall category, which contains a lot of other things that
21:31
multiple code, some dependencies. So here it took 2 seconds, probably this database took approximately 2 seconds to respond
21:42
This is another example. This is another example for storage. So these are application code and this amount of network latency problems
21:58
So these are how Profiler helps us in tracking all the performance issues
22:04
There are multiple different views in which application sets can be used to look at performance issues
22:11
there are some limitations or issues sometimes when you run profile error it will fail
22:20
so you have to retry again and it only supports app services posted in
22:26
app service plans which are either basic or about so sometimes even basic output but standard
22:34
or about will definitely so these are the pricing tiers of app service plans in which
22:40
web applications are used. So many people think that profilers will hinder the performance of the server, because the
22:53
profiler runs only for two minutes every one hour. Of course, you can change to one minute or three minutes, but otherwise it's the default
23:02
is two minutes per hour. So it doesn't run within our application, but it's out-propped
23:10
so it doesn't impact the performance of the application. So let's look at that application
23:20
Sorry, let's look at the Azure portal. So I'm inside portal.azure.com
23:36
I already created the services Because in the basic or higher tier, you get a dedicated virtual machine
24:13
So the profiler doesn't work in free tier. So in case when you try to turn it on, it doesn't work, then you should upgrade it to standard
24:24
And this web app is talking to database, it's talking to another app service, which is an
24:32
API and then it talks to storage as well. So let me show you application map
24:42
I need a shortcut in the dashboard. So we are inside application insights and this report is running for last 20 hours So this is the view It a very nice view at a very high level It uses all the methods about various interactions with various components
25:04
Let's say your app might be talking to multiple storage accounts. In this case I have only one storage account
25:10
In that case this becomes a cluster. And you can drill down further by that particular node
25:18
Now you can do that. We will see more details about this
25:26
If there are multiple storage accounts, then you will see all of them
25:30
And if you are interested to see what went wrong here, you can click on this and it shows
25:37
us what those calls are and why it was wrong. You can click on this and it will take you to different place and you can investigate
25:46
the performance flow version. This is one view. The other view is end to end transaction
25:55
This view gives us a high level per component. But what if you want to look at individual
26:00
transaction? You can either click on this or you can click on performance. This particular
26:07
blade will give us details about all different arbitrocesses. Again this is written by 24
26:14
You can of course change it to 13 minutes or whatever. Now you see this, this is the average duration amount of time that it took
26:29
So far this was called only once and it took 13.8 seconds
26:36
This was called 6 times and it took an average 10.5 seconds
26:40
Now if you want to understand and see what happened for individual 6 requests, usually
26:47
for all 6 requests. So click on that. On the right hand side, you will see all those 6 individual requests or you can directly
26:57
go to profile tracing. So out of 6 requests, the profile was running only while those 3 requests were happening
27:07
So this icon or this button will be shown only when the profile is enabled
27:14
This will not be shown when the profile is not enabled. So now if you want to see the load on individual components then you can click on this
27:24
So you can pick one of them automatically I can suggest us to look at this or you can
27:31
see one of them. You can sort it based on date, if you want to see the latest one, you can sort it by date
27:39
Now this is end-to-end transaction. You see this, I clicked on this
27:46
I think Microsoft has added this recently. Earlier we have not seen this
27:52
These were individual pods. I am checking to create a container which took approximately 59 seconds
28:01
And this also took similar amount of time and there was an error that said container
28:09
already exists and this was another error with some other message. And I was trying to delete but there is no such file so this was doing some error but
28:22
that call took 9 minutes seconds. So these are all individual interactions with various components in this request
28:33
Now you see the call the resolution here, in case if you don't see that here, probably
28:37
you can collapse this, sometimes this particular thing is not visible. Probably there is some UI issue, you see it is not visible
28:44
You scroll to our site, you don't see that number. So in that case, you make some room by collapsing these plates, you see that there
28:53
And here, right here, you see a profile. Again, this is only visible when we enable the profile
29:01
So, I can click on this to see what happened, additional to what it is showing
29:08
You can click on this or if you are not interested in this particular session but you are interested overall
29:16
Then, scroll to what's left, we are inside PerformanceGlade and you click on this
29:26
For the very first time, if you want to enable it, you can click on this profile
29:33
It takes some time to install the agent on our application or app service server and
29:41
It does a warm up and then it starts the session. So it takes some time for the very first time
29:48
You can also do some other triggers if you don't want to profile manually, you might
29:56
want to do automatically. Then you can click on this. If you want to trace only CPU, then you can do this
30:04
It is currently turned on and it runs only for 1.2 seconds
30:09
So it automatically runs when the CPU goes beyond 80%. It automatically runs when it reaches the CPU percentage on the server which is 80% then it runs for one-two seconds
30:22
And again runs only after four hours. Again if the CPU is beyond 80% after four hours, within four hours then it won't run
30:31
The next run will be only after three hours. And same thing you can do for memo
31:05
because if you choose this maximum impact performance, normal should be good for most of the performance
31:15
So if you change it, then for the click apply. So I have two profiling sessions
31:24
So I can even download the trace. time. Let's not wait because I already showed you that. How these days look like. That's
32:25
even why they capture all those screenshots of the event. So these are viewed. You can
32:29
see hard paths. In real time applications there could be many many calls within the application
32:36
This profiler helps us and helps the developers to directly jump into where exactly which component
32:45
means taking more time for the browser. That's how a portfolio works. You don't need to write
32:53
any, it's not in groups of and so forth, you just want to do it and so forth. That's about it
33:05
I think I'm done
#Windows & .NET
#Monitoring Software


