Application Insights – 5 Tips to Improve Your Observability

Application Insights – 5 Tips to Improve Your Observability

Introduction

This article aims to bring some tips about Microsoft Application Insights that can help you enhance the observability of your applications. The code examples here used the built-in SDK for ASP.NET Core and C#

Azure Applications Insights

Azure Applications Insights is an observability tool developed by Microsoft. One of its benefits is that it aggregates distributed tracing, metrics, and logs in a single place. With a few lines of code, you can instrument your whole application. You can also create customized logs and metrics, add environment context information to the logs, filter, and visualize it easily.

All the data stored can be retrieved using the Kusto Query Language (KQL), which allows you to transform the result of the queries on Graphs for easy visualization.

Summary

Customize your Instrumentation
Add correlation to your logs
Add custom dimension fields to your logs
Add the Application version
Enable Developer Mode

1. Customize your Instrumentation

The ASP.NET SDK has built-in automatic instrumentation, which allows you to instrument your whole application with a few lines of code.

On the other hand, you might want to write your metrics, and the SDK provides flexibility here. To avoid duplicate information, you can switch off what you don’t want the API to auto-collect.

Example:

var options = new ApplicationInsightsServiceOptions();

// Disables dependencies request collection
options.EnableDependencyTrackingTelemetryModule = false;

// Disables pre-aggregated metrics about requests/dependencies
options.AddAutoCollectedMetricExtractor = false;

builder.Services.AddApplicationInsightsTelemetry(options);

Full list of ApplicationInsightsServiceOptions settings:

Setting
Description
Default

EnablePerformanceCounterCollectionModule
Enable/Disable PerformanceCounterCollectionModule.
True

EnableRequestTrackingTelemetryModule
Enable/Disable RequestTrackingTelemetryModule.
True

EnableEventCounterCollectionModule
Enable/Disable EventCounterCollectionModule.
True

EnableDependencyTrackingTelemetryModule
Enable/Disable DependencyTrackingTelemetryModule.
True

EnableAppServicesHeartbeatTelemetryModule
Enable/Disable AppServicesHeartbeatTelemetryModule.
True

EnableAzureInstanceMetadataTelemetryModule
Enable/Disable AzureInstanceMetadataTelemetryModule.
True

EnableQuickPulseMetricStream
Enable/Disable LiveMetrics feature.
True

EnableAdaptiveSampling
Enable/Disable Adaptive Sampling.
True

EnableHeartbeat
Enable/Disable the heartbeats feature. It periodically (15-min default) sends a custom metric named HeartbeatState with information about the runtime, like the .NET version and Azure environment information, if applicable.
True

AddAutoCollectedMetricExtractor
Enable/Disable the AutoCollectedMetrics extractor. This telemetry processor sends preaggregated metrics about requests/dependencies before sampling occurs.
True

RequestCollectionOptions.TrackExceptions
Enable/Disable reporting of unhandled exception tracking by the request collection module.
False in netstandard2.0 (because exceptions are tracked with ApplicationInsightsLoggerProvider). True otherwise.

EnableDiagnosticsTelemetryModule
Enable/Disable DiagnosticsTelemetryModule. Disabling causes the following settings to be ignored: EnableHeartbeat, EnableAzureInstanceMetadataTelemetryModule, and EnableAppServicesHeartbeatTelemetryModule.
True

2. Correlation

Correlation is about creating a relationship between logs. It allows you to analyze distributed tracing, creating a timeline of events.

There are two ways you can add it to your logs:

a) Adding the W3C TraceContext header

Add a traceparent key to the requests header. The trace-id contained in the value will become the correlation id. For more details access the link.

b) Evolving methods within the TelemetryClient operation

public void DoSomething()
{
var operation = telemetryClient.StartOperation(requestTelemetry);

try
{
// All the operations added here
// will receive a common correlation ID
}
finally
{
// Update status code and success as appropriate.
telemetryClient.StopOperation(operation);
}
}

For more information about operations, access link

3. Add custom dimension fields to your logs

Creating multidimensional logs is a good practice. Adding more information to them besides of a simple text message helps you understand the context in which they were created and query them while troubleshooting.

TelemetryClient

public void ProcessPayment()
{
telemetryClient.TrackTrace(“Processing credit card payment”, new Dictionary<string, string>()
{
{“CustomerId”, “12345”},
{“OrderId”, “54”},
});
}

ILogger

public void ProcessPayment()
{
using (logger.BeginScope(new Dictionary<string, object>{
[“CustomerId”] = 12345,
[“OrderId”] = 54
}))
{
logger.LogInformation(“Processing credit card payment”);
}
}

4. Add the Application version

When initializing the Application Insights, you can define your application’s version. Then, all the logs created will have the version value in the field application_Version. That will help you understand if an error happened before or after a specific deployment.

var options = new ApplicationInsightsServiceOptions();

options.ApplicationVersion = “1.0.0.1”;

builder.Services.AddApplicationInsightsTelemetry(options);

5. Enable Developer Mode

You can use the developer mode to send the logs immediately to Application Insights while you are troubleshooting. When this option is active, the logs are not sent by batch and it has a significant performance impact, it shouldn’t be activated in production environments.

var options = new ApplicationInsightsServiceOptions();

options.DeveloperMode = true;

builder.Services.AddApplicationInsightsTelemetry(options);

This article is the first of a series related to Application Insights and Observability.

I hope it can help you improve your application’s observability, find bugs quickly, and make it more reliable.

Leave a Reply

Your email address will not be published. Required fields are marked *