-
Notifications
You must be signed in to change notification settings - Fork 569
14. Using Application Insights in eShopOnContainers
The MVC and WebForms modernized applications have installed the Application Insights Nuget packages to produce metrics and send it to an Azure Application Insights service. By default the package needed is Microsoft.ApplicationInsights
. This package adds an ApplicationInsights.config
file and other nuget dependecies. Only with this packaage the library will collect already a lot of metrics, regarding web requests, exceptions thrown, etc.
In the next sections we will see how to add more metrics. Traces for instance require an extra step to be collected.
In order to send the logs of the application to the Application Insights, the traces that we add to our code, we need to do the next steps:
- Install Microsoft.ApplicationInsights.TraceListener nuget package.
- Add to the
Web.config
file this lines to add a TraceListener:
<system.diagnostics>
<trace autoflush="true" indentsize="0">
<listeners>
<add name="myAppInsightsListener" type="Microsoft.ApplicationInsights.TraceListener.ApplicationInsightsTraceListener, Microsoft.ApplicationInsights.TraceListener" />
</listeners>
</trace>
</system.diagnostics>
And this is all you need. The System.Diagnostics.Trace
logs are automatically collected and processed by the Application Insights engine:
Trace.TraceInformation($"Received request for action {filterContext.ActionDescriptor.ActionName} in controller {filterContext.Controller.GetType().Name}.");
In order to get some extra information from the metrics when the services are in a Service Fabric cluster, we must install the optional Nuget package Microsoft.ApplicationInsights.ServiceFabric
. No extra configuration is required because the automatic installation of the package via nuget manager handles all the changes. Thanks to this package, we can obtain information about the node where the container is.
If we have multiple applications, each in its container, sending metrics to the same Application Insights resource, we have extra functionality to facilitate the distinction of the requests send to different components. The Cloud-role name
field has been created in Application Inishgts for this purpose. The idea is that each different component have a different name that identifies the component, in our case the container, and you can filter all the data using this one.
To configure the Cloud-role name you need to create one telemetry initializer and add it to the list active ones. The steps are:
- Create one class that inherits from ITelemetryInitializer, and set there for the requests (type of telemetry is
RequestTelemetry
) the desired cloud role name. In this case we have just put the name of the application:
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
namespace eShopModernizedMVC
{
public class MyTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
var requestTelemetry = telemetry as RequestTelemetry;
if (requestTelemetry?.Context?.Cloud == null) return;
requestTelemetry.Context.Cloud.RoleName = "eShopModernizedMVC";
}
}
}
- Add the new telemetry initializer, in this case 'MyTelemetryInitializer' to the list during the initialization in the
Global.asax.cs
:
TelemetryConfiguration.Active.TelemetryInitializers.Add(new MyTelemetryInitializer());
Once our application is ready to collect the metrics, we have to indicate which Application Insights resource will receive the data. The only thing we need to configure here is the Instrument Key of the Azure Application Insights where we want to send the data. We can find the variable in the 'docker-compose.override.yml' file:
- AppInsightsInstrumentationKey=${APP_INSIGHTS_INSTRUMENTATION_KEY}
As for the rest of variables it needs to be set in different ways depending on the environment (VM, Serfice Fabric or Kubernetes). In the Global.asax.cs
file we configure by code the instrumentation key. We are using containers and
thus environment variables must prevail over the Web.config
, so we use the CatalogConfiguration
class that we have implemented to obtain the instrumentation key from the environment variables and set it:
var environmentKey = CatalogConfiguration.AppInsightsInstrumentationKey;
if (!string.IsNullOrEmpty(environmentKey))
{
TelemetryConfiguration.Active.InstrumentationKey = environmentKey;
}
In the next section we will see how to create one Application Insights resource in Azure and find this intrumentation key.
Open the Azure portal, click new, and search for Application Insights. In the creation form select Application Type Asp.NET web application
.
Finally retrieve the Instrumentation Key from you Application Insights service and set it in your application as the value for the AppInsightsInstrumentationKey
variable.
If we have multiple applications, each in its container, sending metrics to the same Application Insights resource, we have extra functionality to facilitate the distinction of the different components, thanks to the 'Multi-role application map' and the 'Cloud-role name'. Once created the App Insights, go to the Configuration and enable 'Multi-role application map'.
Once enabled you can go to the Application map and you will see there the different components, one per 'Cloud-role name'. Remember that you must configure the 'Cloud_role name' in your applications as explained at the begining of this entry. In the case of having installed Service Fabric package for Application insights the Cloud-role name will be set by the library automatically and you do not need to configure it manually. In the next image we can see the MVC_ContainerPkg and the Webforms_ContainerPkg, both the names of our services added by the Service Fabric configuration, and the eShopModernizedMVC and eShopModernizedWebforms components, both running in a VM and with the name set using the Telemetry Initializer in the code of the applications.
Once the instrumentation key is set in the settings of the application and the Azure Applications Insights is created in our Azure portal, everything is ready to start receiving the metrics. Go to the Portal and select the Application Insights service bound to your application. There you can click on 'Search' in order to see all the metrics. You can select multiple filters to customize the list results.
You can see in the Search list, among other metrics, the log traces of the applications under the TRACE
type. The traces have been added to the metrics by installing in the MVC and WebForms applications the optional Nuget package Microsoft.ApplicationInsights.TraceListener with its corresponding configuration.
Another metric, this one automatically retrieved by default, are the web requests under the REQUEST
type:
For the requests as extra information you can see the field 'Cloud role name'. In this field we store the name of the service, allowing us to filter all the requests by the name of the service we want to. 'Cloud role name' parameter has been configured in the applications with the class MyTelemetryInitializer
, a custom telemetry initializer which implements ITelemetryInitializer
. Otherwise the parameter will not appear. You can review this initializer to add your own values to the Cloud role name.
In order to get some extra information from the metrics when the services are in a Service Fabric cluster, we have installed the optional Nuget package Microsoft.ApplicationInsights.ServiceFabric. No extra configuration is required because the automatic installation of the package via nuget manager handles all the changes. Thanks to this package, we can obtain information about the node where the container is:
In this case as you can notice that 'Cloud role name' parameter does not appear here. By clicking the ...
at the end of the property list, we can see again the configured 'Clour role name' parameter with the name of the service, in this case the eShopModernizedWebForms:
- Home
- Release notes
- e-books
-
MVC & Web Forms Samples
- Tour of the "legacy" ASP.NET web apps to modernize
- How to containerize the .NET Framework web apps with Windows Containers and Docker
- Publishing your Windows Container images into a Docker Registry
- Deploying the Apps to Azure Web Apps for Containers
- Deploying the Apps to ACI (Azure Container Instances)
- Deploying your Windows Containers based app into Azure VMs (Including CI CD)
- Deploying into local Kubernetes in Windows 10 and Docker for Windows development environment
- How to deploy your Windows Containers based apps into Kubernetes in Azure Container Service (Including CI CD)
- How to add authentication authorization with Azure Active Directory
- How to migrate the SQL database to Azure with the Azure Database Migration Service
- Using Application Insights in eShopOnContainers
- N-Tier sample: WinForms app and WFC service
- ASP.NET to Azure App Service Migration Workshop