Python:如何检查Azure函数是否仍在运行并具有队列

2024-04-24 21:38:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道有一个帖子有一个类似的问题。这真的没用

我有一个函数,可以在一段时间内分批接收输入(例如:1000批,每个批有n个样本)。批次不会同时到达。该函数处理每个批并将输出写入blobstoragecontainer中的blob

我的问题是,我如何知道结果都已写入blob存储,以便触发下载

我试图查看azure mgmt monitor,以检查我是否可以在最后一分钟监控请求/函数调用的数量,并将其放入while循环,直到某些指标(可能是其他指标)为0。我可以通过一些聚合在时间跨度上调用一些度量,但出于某种原因,我一直将所有值都设置为0(当我知道有调用时)

下面是一些代码(azure_mngr.monitor是azure.mgmt.monitor.MonitorManagementClient的一个实例):

start_date = datetime.datetime(2020, 6, 2, 10, 0, 0)
end_date = datetime.datetime(2020, 6, 2, 11, 0, 0)
azure_mngr.monitor.metrics.list(azure_function_id, metricnames='Requests,RequestsInApplicationQueue,FunctionExecutionCount', interval='PT1M', timespan=f'{start_date}/{end_date}', aggregation='Count,Average,Maximum,Minimum')

结果

for metric in a.value:
    for datapoint in metric.timeseries[0].data:
        print(f'{metric.name.value} | {datapoint.time_stamp} | {datapoint.count}')

Requests | 2020-06-02 10:00:00+00:00 | 0.0
Requests | 2020-06-02 10:15:00+00:00 | 0.0
Requests | 2020-06-02 10:30:00+00:00 | 0.0
Requests | 2020-06-02 10:45:00+00:00 | 0.0
RequestsInApplicationQueue | 2020-06-02 10:00:00+00:00 | 0.0
RequestsInApplicationQueue | 2020-06-02 10:15:00+00:00 | 0.0
RequestsInApplicationQueue | 2020-06-02 10:30:00+00:00 | 0.0
RequestsInApplicationQueue | 2020-06-02 10:45:00+00:00 | 0.0
FunctionExecutionCount | 2020-06-02 10:00:00+00:00 | 0.0
FunctionExecutionCount | 2020-06-02 10:15:00+00:00 | 0.0
FunctionExecutionCount | 2020-06-02 10:30:00+00:00 | 0.0
FunctionExecutionCount | 2020-06-02 10:45:00+00:00 | 0.0

并用该期间的请求计数绘制图表(来自insights资源) enter image description here

我最疯狂的猜测告诉我,我应该传递的id可能不是azure函数,而是另一个。。。 我也不知道该怎么做。我也一直在看azure mgmt应用程序Insights,但它甚至比监视器更模糊


Tags: 函数datetimedateazure指标requestsmetricstart
1条回答
网友
1楼 · 发布于 2024-04-24 21:38:57

因此,几天后我找到了一种阅读azure insights的方法,尽管我没有坚持使用这种解决方案

洞察法

对于任何感兴趣的人,要从洞察中读取度量,您需要使用azure applicationinsights包(而不是azure mgmt applicationinsights)。 然后,使用azure凭据实例化ApplicationInsightsDataClient,并可以按如下方式发送查询:

from azure.applicationinsights import ApplicationInsightsDataClient

client = ApplicationInsightsDataClient(<credentials>)
metric = client.metrics.get(<application_id>, <metric>, custom_headers=<custom_headers>, **<other_kwargs>)

现在,棘手的部分是“应用程序\u id”和“自定义\u头”。 首先,您可以在API访问密钥选项卡上的insights资源中从azure检索它

对于自定义头,您需要将令牌指定给需要在insights资源中创建的一个API_密钥(与上面相同的位置)。格式应为:

custom_headers = {'x-api-key': <api_key>}

了解您可以从insights资源中获得哪些指标

available_metrics = client.metrics.get_metadata(<application_id>, custom_headers=custom_headers)

此外,如果部署在CI/CD管道中,还可以自动检索应用程序ID和创建api_密钥

为了在gitlab管道日志上获取应用程序id,我刚刚创建了它,并将其输出到terraform中(查看terraform文档中的输出) 对于api_密钥创建,然后需要使用azure mgmt applicationinsights包并实例化ApplicationInsightsManagementClient(目前不确定该名称)

传递凭证并在“api_密钥”属性中使用“创建”方法。这可能有点棘手,因为您需要传递一些“linked\u read\u属性”

我建议首先在azure portal中创建它,在python中阅读它,检查您需要的属性,然后尝试通过python创建它

至于我最终坚持的解决方案。

我创建了azure函数,该函数将结果写入blob_存储,以同时写入“元数据”键/值

如果有批处理尚未运行,它将把它写为False。如果是最后一批,则将其更改为True

然后我只读取python中的blob属性,直到该值为真

希望这能帮助任何有类似问题的人;)

相关问题 更多 >