Azure Application Insights 不显示在 Azure 函数中创建的自定义事件
Azure Application Insights doesn't show custom events created in Azure function
我有一个用 C# 编写的函数应用程序,它是在 Azure 中创建的,由 HttpTrigger 触发。
我正在尝试记录几个自定义事件来记录分析性能的有趣时间点。
我所做的是在构造函数中创建 TelemetryClient。
static ThumbnailGenerator()
{
telemetryClient = new TelemetryClient(TelemetryConfiguration.CreateDefault());
}
然后在函数中:
[FunctionName("Upload")]
[StorageAccount("AzureWebJobsStorage")]
public static async Task<IActionResult> Upload(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "upload/{name}")] HttpRequest req,
string name,
ILogger log,
ExecutionContext context)
{
var evt = new EventTelemetry("Upload function called");
evt.Context.User.Id = name;
telemetryClient.TrackEvent(evt);
telemetryClient.Flush();
....
DateTime start = DateTime.UtcNow;
// Log a custom dependency in the dependencies table.
var dependency = new DependencyTelemetry
{
Name = "Upload-Image-Operation",
Timestamp = start,
Duration = DateTime.UtcNow - start,
Success = true
};
telemetryClient.TrackDependency(dependency);
telemetryClient.Flush();
return new OkObjectResult(name + "Uploaded successfully.");
}
此应用程序见解可以始终正确显示函数的默认 RequestTelemetry 和跟踪。但是,自定义事件和DependencyTelemetry 非常不稳定,有时在应用程序洞察中显示,有时根本不显示。
我做了很多研究并添加了如下内容:
telemetryClient.Flush();
但不稳定的情况还是差不多。
我使用的库是:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.9.0" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.14.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.0-beta.2" />
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.25" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
有人知道为什么吗?看来 Azure 在这部分真的很不稳定而且有问题。请给我一些提示。
您可能 运行 进入 Sampling feature of Application Insights。
[Sampling] is the recommended way to reduce telemetry traffic, data costs, and storage costs, while preserving a statistically correct analysis of application data.
This page 还有一些特定于 Azure Functions 的更多详细信息:
When the rate of incoming executions exceeds a specified threshold, Application Insights starts to randomly ignore some of the incoming executions.
这听起来像是您遇到的行为。您可以使用 host.json
文件来配置哪些类型的遥测被排除在采样之外;尝试将 Event
添加到 excludedTypes
:
{
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"maxTelemetryItemsPerSecond" : 20,
"excludedTypes": "Request;Exception;Event"
}
}
}
}
确保您的遥测配置设置了正确的检测密钥。我不确定 TelemetryConfiguration.CreateDefault()
得到了正确的值。
此外,我建议使用依赖注入在构造函数中注入 TelemetryClient。它已经开箱即用。这样您就不必自己创建实例,也不必担心设置检测密钥的正确方法:
[FunctionName("Upload")]
[StorageAccount("AzureWebJobsStorage")]
public static async Task<IActionResult> Upload(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "upload/{name}")] HttpRequest req,
string name,
ILogger log,
TelemetryClient telemetryClient,
ExecutionContext context)
{
telemetryClient.XXX();
...
}
刷新客户端应该不是必需的,只有在终止时才需要,即使这样你也应该等待一段时间,因为它是一个异步进程。
我有一个用 C# 编写的函数应用程序,它是在 Azure 中创建的,由 HttpTrigger 触发。
我正在尝试记录几个自定义事件来记录分析性能的有趣时间点。
我所做的是在构造函数中创建 TelemetryClient。
static ThumbnailGenerator()
{
telemetryClient = new TelemetryClient(TelemetryConfiguration.CreateDefault());
}
然后在函数中:
[FunctionName("Upload")]
[StorageAccount("AzureWebJobsStorage")]
public static async Task<IActionResult> Upload(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "upload/{name}")] HttpRequest req,
string name,
ILogger log,
ExecutionContext context)
{
var evt = new EventTelemetry("Upload function called");
evt.Context.User.Id = name;
telemetryClient.TrackEvent(evt);
telemetryClient.Flush();
....
DateTime start = DateTime.UtcNow;
// Log a custom dependency in the dependencies table.
var dependency = new DependencyTelemetry
{
Name = "Upload-Image-Operation",
Timestamp = start,
Duration = DateTime.UtcNow - start,
Success = true
};
telemetryClient.TrackDependency(dependency);
telemetryClient.Flush();
return new OkObjectResult(name + "Uploaded successfully.");
}
此应用程序见解可以始终正确显示函数的默认 RequestTelemetry 和跟踪。但是,自定义事件和DependencyTelemetry 非常不稳定,有时在应用程序洞察中显示,有时根本不显示。
我做了很多研究并添加了如下内容:
telemetryClient.Flush();
但不稳定的情况还是差不多。
我使用的库是:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.9.0" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.14.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.0-beta.2" />
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.25" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
有人知道为什么吗?看来 Azure 在这部分真的很不稳定而且有问题。请给我一些提示。
您可能 运行 进入 Sampling feature of Application Insights。
[Sampling] is the recommended way to reduce telemetry traffic, data costs, and storage costs, while preserving a statistically correct analysis of application data.
This page 还有一些特定于 Azure Functions 的更多详细信息:
When the rate of incoming executions exceeds a specified threshold, Application Insights starts to randomly ignore some of the incoming executions.
这听起来像是您遇到的行为。您可以使用 host.json
文件来配置哪些类型的遥测被排除在采样之外;尝试将 Event
添加到 excludedTypes
:
{
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"maxTelemetryItemsPerSecond" : 20,
"excludedTypes": "Request;Exception;Event"
}
}
}
}
确保您的遥测配置设置了正确的检测密钥。我不确定 TelemetryConfiguration.CreateDefault()
得到了正确的值。
此外,我建议使用依赖注入在构造函数中注入 TelemetryClient。它已经开箱即用。这样您就不必自己创建实例,也不必担心设置检测密钥的正确方法:
[FunctionName("Upload")]
[StorageAccount("AzureWebJobsStorage")]
public static async Task<IActionResult> Upload(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "upload/{name}")] HttpRequest req,
string name,
ILogger log,
TelemetryClient telemetryClient,
ExecutionContext context)
{
telemetryClient.XXX();
...
}
刷新客户端应该不是必需的,只有在终止时才需要,即使这样你也应该等待一段时间,因为它是一个异步进程。