这背后的原因是我工作的公司可能很快就会有很多微服务,并且不想管理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 标签、副本集名称等。