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

2024-03-23

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

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

话虽如此,我现在将所有微服务都链接到相同的 Application Insights。我希望能够通过微服务过滤我的日志查询。

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

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

读完这篇文章后:添加自定义维度以请求遥测 - Azure 函数 https://stackoverflow.com/questions/57073603/adding-custom-dimension-to-request-telemetry-azure-functions我意识到我需要调用 TrackEvent()、TrackTrace() 等才能查看新的自定义维度条目。但是,我需要向给定微服务生成的任何请求/事件/跟踪添加一个微服务列。


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

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

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

对于 App Insights,您可以使用自定义属性。有多种设置它们的方法(使用代码!)。一种是直接如图所示设置here https://learn.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics#defaults:

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实例!

另一个正在使用遥测初始化器 https://learn.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling#addmodify-properties-itelemetryinitializer:

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

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

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

现在,可能已经有一些东西了。所有遥测技术都具有以下属性cloud_Rolename & cloud_RoleInstance:

requests
| project cloud_RoleInstance , cloud_RoleName 

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

例如,这将显示按 pod 分割的请求:

requests
| summarize count() by cloud_RoleName 

如果您使用 C#,您还可以添加this https://github.com/microsoft/ApplicationInsights-Kubernetes由 Microsoft 创建的软件包,会将大量 AKS 详细信息附加到遥测数据,例如 pod id、pod 标签、副本集名称等。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将所有 AKS 微服务输送到单个 Application Insights 中,并能够按微服务名称进行筛选 的相关文章

随机推荐