将所有 AKS 微服务通过管道传输到单个 Application Insights 中,并能够按微服务名称进行筛选

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

我想知道是否可以让多个 Azure Kubernetes 微服务指向相同的 Application Insights 资源,并能够使用日志查询按微服务名称拆分日志。

目前,我已经能够创建 Application Insights 资源。我还使用 Visual Studio 2017 IDE 将微服务配置为指向该 Application Insights 资源(全部通过 GUI 完成)。

也就是说,我现在将所有微服务都链接到同一个 Application Insights。我希望能够通过微服务过滤我的日志查询。

这背后的原因是,我工作的公司可能很快就会有很多微服务,不想管理 N 个应用洞察资源。相反,我们希望所有内容都集中在同一个 Application Insights 中(因此我不能使用联合运算符来加入多个应用程序洞察力,因为我们只想拥有一个)。

我考虑过向 Application Insights 添加自定义字段,但它似乎对我的情况没有帮助。 https://docs.microsoft.com/en-us/azure/azure-monitor/platform/custom-fields

谢谢!

更新: 示例代码: Program.cs: 自定义维度:

阅读此 post 后:Adding Custom Dimension to Request Telemetry - Azure functions 我意识到我需要调用 TrackEvent()、TrackTrace() 等才能看到新的自定义维度条目。但是,我需要向给定微服务生成的任何 request/event/trace 添加微服务列。

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)

请注意,App Insights 价格模型主要基于每 GB 数据摄取的成本。每月前几个 GB 是免费的。所以您的解决方案可能不是最具成本效益的。

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

对于 App Insights,您可以使用自定义属性。有几种方法可以设置它们(使用代码!)。一种是直接设置它们,如图here:

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");

请注意,此方法只会将属性添加到使用 TelemetryClient 实例创建的遥测中!

另一个正在使用 telemetry initializer:

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 >= 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 >= 400 && code < 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
    }
  }
}

我更喜欢这个,因为它也适用于框架创建的遥测,而不仅仅是使用您自己的代码创建的自定义遥测。

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.

现在,可能已经有了一些东西。所有遥测都具有 cloud_Rolename & cloud_RoleInstance:

等属性
requests
| project cloud_RoleInstance , cloud_RoleName 

它将显示容器名称和 pod 名称,因此这对您来说可能已经足够了。

因此,例如,这将显示按 pod 拆分的请求:

requests
| summarize count() by cloud_RoleName 

如果您使用 C#,您还可以添加 this 由 Microsoft 创建的程序包,它将大量 AKS 详细信息附加到遥测数据中,例如 pod id、pod 标签、副本集名称等。

通过在 Peter 给我的解决方案中添加一些步骤,我已经能够解决这个问题。

  1. 我创建了一个自定义遥测初始化程序,如下所示:
 public class MicroserviceNameTelemetry : ITelemetryInitializer
    {
        private string microserviceName;
        public MicroserviceNameTelemetry(string microserviceName)
        {
            this.microserviceName = microserviceName;
        }
        public void Initialize(ITelemetry telemetry)
        {
            telemetry.Context.GlobalProperties["Microservice"] = microserviceName;
        }

    }

然后我不得不覆盖 Startup.cs 中的默认遥测初始化器(我添加的步骤):

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;
}