Skip to main content
 首页 » 编程设计

python之使用 rest api 获取 Azure 事件中心指标

2024年10月24日7mengfanrong

我正在尝试使用其余 API 提取事件中心指标, 看完https://msdn.microsoft.com/en-us/library/azure/dn163589.aspx和 https://msdn.microsoft.com/en-us/library/azure/mt652158.aspx 我有可以实际调用 url 并获得响应的 python 代码 我目前正在尝试以下代码

def get_metrics(subscription, eventhub, cert, specific_partition=None): 
    apiversion = '2014-01' 
    namespace = eventhub['namespace'] 
    eventhubname = eventhub['name'] 
    url = "https://management.core.windows.net/{}/services/ServiceBus/Namespaces/{}/eventhubs/{}/Metrics/requests.total/Rollups/P1D/Values/?$filter=timestamp%20gt%20datetime'2016-04-09T00:00:00.0000000Z'&api-version={}".format( 
        subscription, namespace, eventhubname, apiversion) 
    request = requests.Request('GET', url, headers=DEFAULT_HEADERS).prepare() 
    session = requests.Session() 
    if cert is None or not os.path.isfile(cert): 
        raise ValueError('You must give certificate file') 
    session.cert = cert 
    result = session.send(request) 
    return result 

我的问题是 url,当在上面的代码中使用 url 时,我得到了

<Error xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Code>InternalError</Code><Message>The server encountered an internal error. Please retry the request.</Message></Error> 

我可以让 API 输出所有可能的汇总和所有可能的指标,但在尝试获取实际值时失败了。

是 url 有问题还是 azure/azure documantation 中的错误?

请您参考如下方法:

通常,当我们出现这个问题时,这意味着我们为 Rest Apis 组合的端点有问题,因此服务在解析端点时会引发异常。

与我成功的测试相比,我发现有趣的是过滤器参数timestamp的第一个字母应该大写为Timestamp引发的问题。以下端点在我这边工作正常。希望对您有所帮助。

url = "https://management.core.windows.net/{}/services/ServiceBus/Namespaces/{}/eventhubs/{}/Metrics/requests.total/Rollups/P1D/Values/?$filter=Timestamp%20gt%20datetime'2016-04-09T00:00:00.0000000Z'&api-version={}".format( 
        subscription, namespace, eventhubname, '2012-03-01')