从 DevOps 中运行的 C# 测试项目查询 Azure 函数调用

问题描述 投票:0回答:1

我正在寻找一种方法来查询 Azure Functions 或 ApplicationInsights,以了解对 Azure 函数应用程序中给定函数进行的所有调用。

我发现我可以使用 AZ cli 轻松完成此操作,例如:

az monitor app-insights query --app "{AZURE_FUNCTION_NAME}" --resource-group "{RESOURCE_GROUP_NAME}" --analytics "requests | where name == '{FUNCTION_NAME}' | top 1 by timestamp desc"

有什么办法可以在 C# 中做到这一点吗?

我实际上只是想知道我的函数是否已运行以及是否已完成。基本上我想知道这一点,以便我可以启动我的集成测试。

我已经研究过 TelemetryClient 但我无法真正让它工作。如果有人有使用 TelemetryClient 的示例,我也很乐意看一下。 但首先,我主要是在寻找一种方法来检查我的函数在最近一小时内是否运行。

azure azure-devops azure-functions azure-application-insights
1个回答
0
投票

TelemetryClient
只能用于发送遥测数据。您无法用它查询任何数据。

您可以使用 适用于 .NET 的 Azure Monitor 查询客户端库 或利用 Log Analytics REST API

使用客户端的示例:

var client = new LogsQueryClient(new DefaultAzureCredential());
string workspaceId = "<workspace_id>";

// Query TOP 10 resource groups by event count
Response<IReadOnlyList<MyLogEntryModel>> response = await client.QueryWorkspaceAsync<MyLogEntryModel>(
    workspaceId,
    "AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count",
    new QueryTimeRange(TimeSpan.FromDays(1)));

foreach (var logEntryModel in response.Value)
{
    Console.WriteLine($"{logEntryModel.ResourceGroup} had {logEntryModel.Count} events");
}

使用 api 的示例:

GET https://api.loganalytics.io/v1/workspaces/63613592-b6f7-4c3d-a390-22ba13102111/query?query=union * | where TimeGenerated > ago(1h) | summarize count() by Type, TenantId
© www.soinside.com 2019 - 2024. All rights reserved.