Search code examples
azurekubernetesmicroservicesazure-application-insights

Pipe All AKS microservices into a single Application Insights and be able to filter by microservice name


I would like to know if it is possible to have multiple Azure Kubernetes micro services pointing to the same Application Insights Resources and be able to use Log Queries to split the logs per micro services name.

At the moment, I have been able to create an Application Insights resource. I also configured the micro services to point to that Application Insights resource using Visual Studio 2017 IDE (all done via a GUI).

That being said, I now have all my micro services linked to the same Application Insights. I would like to be able to filter my log queries by micro service.

The reason behind this is that the company I work for might soon have a lot of micro services and do not want to manage N application insights resources. Instead, we want everything centralized in the same Application Insights (so I cannot use the union operator to join multiple application insights since we only want to have one).

I thought about adding custom fields to Application Insights, but it does not seem like it can help in my situation. https://learn.microsoft.com/en-us/azure/azure-monitor/platform/custom-fields

Thanks!

UPDATE: Sample code: Telemetry Clas Program.cs: enter image description here Custom dimensions: enter image description here

After reading this post: Adding Custom Dimension to Request Telemetry - Azure functions I realized that I need to call TrackEvent(), TrackTrace(), etc. in order to see the new custom dimensions entry. However, I needed to add a Microservice column to any request/event/trace produced by a given microservice.


Solution

  • I have been able to fix the issue by adding some steps to the solution Peter gave me.

    1. I created a custom telemetry initializer that looks like the following:
     public class MicroserviceNameTelemetry : ITelemetryInitializer
        {
            private string microserviceName;
            public MicroserviceNameTelemetry(string microserviceName)
            {
                this.microserviceName = microserviceName;
            }
            public void Initialize(ITelemetry telemetry)
            {
                telemetry.Context.GlobalProperties["Microservice"] = microserviceName;
            }
    
        }
    

    I then had to overwrite the default telemetry initializer in the Startup.cs (the step I added):

    public static IServiceCollection AddApplicationInsights(this IServiceCollection services, IConfiguration configuration)
    {
                services.AddApplicationInsightsTelemetry(configuration);
                services.AddSingleton<ITelemetryInitializer, MicroserviceNameTelemetry>((serviceProvider) => {
                    return new MicroserviceNameTelemetry(configuration.GetSection("Microservice").GetValue<string>("name"));
                });
                // Enable K8s telemetry initializer            
               services.AddApplicationInsightsKubernetesEnricher();
    
                return services;
    }