Application Insights - 5 Tips to Improve Your Observability

fagner

Fagner Oliveira

Posted on May 4, 2024

Application Insights - 5 Tips to Improve Your Observability

Grey image with a magnifier at the center and lines representing a digital circuit around it

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 create customized logs and metrics, filter, and visualize them easily.

All the data stored can be retrieved using the Kusto Query Language (KQL), which is a rich language that allows you to write complex queries and transform the results of them on Graphs or Alerts easily.

1. Log Correlation

When an API endpoint is called, a correlation ID value is created. The value can later be used to fetch all the information App Insights ingested during the request.

The correlation ID should be a number or a string value, and it can be sent as part of the request Header or generated when reaching the API.

The greater benefit is allowing you to visualize the sequence of the events. Microservices architectures take big advantage of it, giving you information about all the multiple requests involved during a transaction

There are at least two ways you can add a correlation ID:

a) W3C Traceparent header key

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 method. All requests made between the Start and Stop operations will receive a common correlation ID.

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

    try
    {
    // Operations
    }
    finally
    {
        // Update status code and success as appropriate.
        telemetryClient.StopOperation(operation);
    }
}
Enter fullscreen mode Exit fullscreen mode

For more information about operations, access link

2. Add custom dimension fields to your logs

Creating multidimensional logs that include information beyond a simple text message is a good practice. This helps to understand better the context in which the events were created and makes querying easier.

TelemetryClient

public void ProcessPayment()
{
    telemetryClient.TrackTrace("Processing credit card payment", new Dictionary<string, string>()
    {
        {"CustomerId", "12345"},
        {"OrderId", "54"},
    });
}
Enter fullscreen mode Exit fullscreen mode

ILogger

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

3. Add the Application version

During Application Insights' initialization, you have the option to specify your application's version. Then, all your logs will include the version value. This is useful to identify whether an error occurred prior to or after a specific deployment.

var options = new ApplicationInsightsServiceOptions();

options.ApplicationVersion = "1.0.0.1";

builder.Services.AddApplicationInsightsTelemetry(options);
Enter fullscreen mode Exit fullscreen mode

4. Enable Developer Mode

When debugging, you can use the developer mode to send logs to Application Insights instantly. This saves time since you don't need to wait for the logs before analyzing them. However, this option should not be activated in production environments as it has a significant performance impact.

var options = new ApplicationInsightsServiceOptions();

options.DeveloperMode = true;

builder.Services.AddApplicationInsightsTelemetry(options);
Enter fullscreen mode Exit fullscreen mode

5. 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. However, you might want to write your own metrics.
The SDK is flexible enough to allow you to disable logs/metrics you don't want to auto-collect. For that, you just need to set the options during the API initialization.

Example:

var options = new ApplicationInsightsServiceOptions();

// Enable/disable metrics related to external requests. E.g. Database, service bus, and 3rd party APIs.
options.EnableDependencyTrackingTelemetryModule = false;

// Enable/Disable metrics related to all requests received by the API
options.EnableRequestTrackingTelemetryModule = false;

builder.Services.AddApplicationInsightsTelemetry(options);
Enter fullscreen mode Exit fullscreen mode

For a full list of ApplicationInsightsServiceOptions settings, check out this link

Conclusion

Implementing these simple tips can significantly enhance the quality of your App. Insights logs, enabling you to quickly identify problems, resolve bugs and improve the overall reliability of your applications.

💖 💪 🙅 🚩
fagner
Fagner Oliveira

Posted on May 4, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related