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

我知道这篇文章(如何检查 Azure Function 是否仍在运行)也有类似的问题。它并没有真正的帮助。


我有一个函数可以在一段时间内分批接收输入(例如: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

以及那个时期的请求数图表(来自洞察力资源)

http://img2.mukewang.com/63a171340001eb5e06160574.jpg

我最大胆的猜测告诉我,我应该传递的 id 可能不是 azure 函数,而是另一个……我也不知道如何执行此操作。我也一直在看 azure-mgmt-applicationinsights,但它比监视器更晦涩......



富国沪深
浏览 107回答 1
1回答

largeQ

因此,几天后,我找到了一种从 azure insights 中阅读的方法,尽管我没有坚持使用该解决方案。洞察法对于任何感兴趣的人,要从您需要使用azure-applicationinsights包(而不是 azure-mgmt-applicationinsights)的见解中读取指标。然后,您使用 Azure 凭据实例化一个 ApplicationInsightsDataClient,您可以按如下方式发送查询:from&nbsp;azure.applicationinsights&nbsp;import&nbsp;ApplicationInsightsDataClient client&nbsp;=&nbsp;ApplicationInsightsDataClient(<credentials>) metric&nbsp;=&nbsp;client.metrics.get(<application_id>,&nbsp;<metric>,&nbsp;custom_headers=<custom_headers>,&nbsp;**<other_kwargs>)现在,棘手的部分是这个“application_id”和“custom_headers”。首先,您可以从 azure 中的 API 访问密钥选项卡上的洞察力资源中检索它。对于自定义标头,您需要将令牌指定为您需要在见解资源中创建的一个 API_key(与上述相同的位置)。格式应该是:custom_headers&nbsp;=&nbsp;{'x-api-key':&nbsp;<api_key>}要了解您可以从洞察资源中获得哪些指标,您可以available_metrics&nbsp;=&nbsp;client.metrics.get_metadata(<application_id>,&nbsp;custom_headers=custom_headers)此外,如果您在 CI/CD 管道中进行部署,您还可以自动检索 ApplicationID 和创建 api_key。要在我刚刚创建的 gitlab 管道日志上获取应用程序 ID 并在 terraform 中为其输出(检查关于输出的 terraform 文档)对于 api_key 创建,您需要使用azure-mgmt-applicationinsights包并实例化一个 ApplicationInsightsManagementClient(不确定这个名字现在)传递凭据并在“api_keys”属性中使用“create”方法。这可能有点棘手,因为您需要传递一些“linked_read_properties”。我的建议是首先在 azure 门户中创建它,在 python 中读取它,检查您需要的属性,然后尝试通过 python 创建它。至于我最终坚持的解决方案。我创建了我的 azure 函数,该函数将结果写入 blob_storage 以同时写入“元数据”键/值。如果还有一些批处理尚未运行,它将把它写为 False。如果是最后一批,则将其更改为 True。然后我只读取 python 中的 blob 属性,直到值为 True。希望这可以帮助任何有类似问题的人;)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python