<p>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.</p>

<p>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).</p>

<p>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.</p>

<p>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).</p>

<p>I thought about adding custom fields to Application Insights, but it does not seem like it can help in my situation. <a href="https://learn.microsoft.com/en-us/azure/azure-monitor/platform/custom-fields" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/azure/azure-monitor/platform/custom-fields</a></p>

<p>Thanks!</p>

<p>UPDATE: 
Sample code:
<a href="https://i.stack.imgur.com/72IYd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/72IYd.png" alt="Telemetry Clas"></a>
Program.cs:
<a href="https://i.stack.imgur.com/IbttM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IbttM.png" alt="enter image description here"></a>
Custom dimensions:
<a href="https://i.stack.imgur.com/szMUD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/szMUD.png" alt="enter image description here"></a></p>

<p>After reading this post: <a href="https://stackoverflow.com/questions/57073603/adding-custom-dimension-to-request-telemetry-azure-functions">Adding Custom Dimension to Request Telemetry - Azure functions</a> 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.</p>

<blockquote>
  <p>The reason behind this is that the company I work fore 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)</p>
</blockquote>

<p>Do mind that App Insights price model is mainly based on cost per GB of data ingested. The first couple of GB per month is free. So your solution might not be the most cost efficient.</p>

<blockquote>
  <p>I thought about adding custom fields to Application Insights, but it does not seem like it can help in my situation. <a href="https://learn.microsoft.com/en-us/azure/azure-monitor/platform/custom-fields" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/azure/azure-monitor/platform/custom-fields</a></p>
</blockquote>

<p>For App Insights you could use custom properties. There are several ways to set them (using code!). One is to set them directly as shown <a href="https://learn.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics#defaults" rel="nofollow noreferrer">here</a>:</p>

<pre class="lang-cs prettyprint-override"><code>using Microsoft.ApplicationInsights.DataContracts;

var gameTelemetry = new TelemetryClient();
gameTelemetry.Context.GlobalProperties["Game"] = currentGame.Name;
// Now all telemetry will automatically be sent with the context property:
gameTelemetry.TrackEvent("WinGame");
</code></pre>

<p>Do mind that this method will only add properties to the telemetry created using the <code>TelemetryClient</code> instance! </p>

<p>the other one is using a <a href="https://learn.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling#addmodify-properties-itelemetryinitializer" rel="nofollow noreferrer">telemetry initializer</a>:</p>

<pre class="lang-cs prettyprint-override"><code>using System;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;

namespace MvcWebRole.Telemetry
{
  /*
   * Custom TelemetryInitializer that overrides the default SDK
   * behavior of treating response codes &gt;= 400 as failed requests
   *
   */
  public class MyTelemetryInitializer : ITelemetryInitializer
  {
    public void Initialize(ITelemetry telemetry)
    {
        var requestTelemetry = telemetry as RequestTelemetry;
        // Is this a TrackRequest() ?
        if (requestTelemetry == null) return;
        int code;
        bool parsed = Int32.TryParse(requestTelemetry.ResponseCode, out code);
        if (!parsed) return;
        if (code &gt;= 400 &amp;&amp; code &lt; 500)
        {
            // If we set the Success property, the SDK won't change it:
            requestTelemetry.Success = true;

            // Allow us to filter these requests in the portal:
            requestTelemetry.Properties["Overridden400s"] = "true";
        }
        // else leave the SDK to set the Success property
    }
  }
}
</code></pre>

<p>I prefer this one as it will also apply to telemetry created by the framework, not only to custom telemetry created using your own code.</p>

<blockquote>
  <p>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.</p>
</blockquote>

<p>Now, there might already be something in place. All telemetry have properties like <code>cloud_Rolename</code> &amp; <code>cloud_RoleInstance</code>:</p>

<pre><code>requests
| project cloud_RoleInstance , cloud_RoleName 
</code></pre>

<p>it will show the container name and pod name so that might already be good enough for you.</p>

<p>So, for example, this will show requests split by pod:</p>

<pre><code>requests
| summarize count() by cloud_RoleName 
</code></pre>

<p>If you use C# you could also add <a href="https://github.com/microsoft/ApplicationInsights-Kubernetes" rel="nofollow noreferrer">this</a> package created by Microsoft that will attach a lot of AKS details to the telemetry like pod id, pod labels, replica set name etc.</p>
