目前在 Application Insights 中,我们遇到了 2 个非常奇怪的问题。 我们的基础设施使用 API 管理,后端是带有多个虚拟应用程序的 Azure 应用服务。
我们使用 NET 6 Web API 与 Application Insights SDK 集成。
问题如下:
有趣的是,当我们查看其中一个依赖调用(作为单独的请求记录)时,它会适当地显示相关的遥测数据。
API 管理启用了 Application Insights 集成,并将关联模式设置为 W3C。
要在 DI 中注册遥测客户端,我们执行以下操作:
public static IServiceCollection RegisterAppInsightsTelemetry(
this IServiceCollection serviceCollection,
Action<ApplicationInsightsServiceOptions>? modifyOptions = null)
{
serviceCollection.AddOptions<ApplicationInsightsServiceOptions>()
.Configure<IConfiguration>((settings, configuration) =>
configuration.GetSection("ApplicationInsights").Bind(settings));
using var provider = serviceCollection.BuildServiceProvider();
var options = provider.GetRequiredService<IOptions<ApplicationInsightsServiceOptions>>();
modifyOptions?.Invoke(options.Value);
serviceCollection.AddApplicationInsightsTelemetry(options.Value);
return serviceCollection;
}
ApplicationInsights
配置如下所示,我们还在代码中显式禁用 AdaptiveSampling:
"ApplicationInsights": {
"ConnectionString":"XXXXXX"
}
builder.Services.RegisterAppInsightsTelemetry(options =>
{
options.EnableAdaptiveSampling = false;
});
在我们的内部服务中,我们的 HTTP 客户端注入标头
traceparent
,并将值设置为当前的 OperationId:
public abstract class HttpService<T> : IHttpService where T : HttpClientConfiguration
{
private readonly HttpClient _httpClient;
private readonly IIdentityAccessor _identityAccessor;
private readonly HttpClientConfiguration _configuration;
private readonly IServiceAccountResolver _serviceAccountResolver;
protected HttpService(
HttpClient httpClient,
IIdentityAccessor identityAccessor,
IOptions<T> options)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
_identityAccessor = identityAccessor;
_serviceAccountResolver = serviceAccountResolver ?? throw new ArgumentNullException(nameof(serviceAccountResolver));
_configuration = options?.Value ?? throw new ArgumentNullException(nameof(options));
InjectHeaders(identityAccessor.CurrentTelemetryId);
}
private void InjectHeaders(string telemetryId)
{
const string traceParentHeader = "traceparent";
if (_httpClient.DefaultRequestHeaders.Contains(traceParentHeader))
return;
if (string.IsNullOrEmpty(telemetryId))
{
telemetryId = Activity.Current?.Id;
}
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation(traceParentHeader, telemetryId);
}
}
我们最近从 Azure Functions 切换到应用服务,我们在 Azure Functions 上没有遇到这些问题,因此我假设问题出在我们注册应用洞察 SDK 的方式上。
在每个 API 上注册 SDK 时,我们尝试手动添加 traceparent 标头和多种设置变体。
任何帮助将不胜感激!