简体   繁体   English

如何从 Azure API 管理获取 API 使用数据?

[英]How to get API usage data from Azure API Management?

We are trying to collect some reports about how our API is used by customers.我们正在尝试收集一些关于客户如何使用我们的 API 的报告。 We use Azure API Management, and I can see that in API Management portal such data exist, I can see what I need to know going to portal in Admin>Activity section.我们使用 Azure API 管理,我可以看到在 API 管理门户中存在此类数据,我可以在“管理”>“活动”部分看到我需要了解的门户。 Like how many calls individual user made for particular API, and I can filter it by date.就像单个用户为特定 API 进行了多少次调用一样,我可以按日期对其进行过滤。

Question: How to get this data out of the system.问题:如何从系统中获取这些数据。 Preferable using some API to have continues export.最好使用一些 API 来继续导出。 But, even manually?但是,即使是手动?

The API to get request level analytics is GET https://management.azure.com/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.ApiManagement/service/apimService1/reports/byRequest?$filter=timestamp ge datetime'2017-06-01T00:00:00' and timestamp le datetime'2017-06-04T00:00:00'&api-version=2017-03-01获取请求级别分析的 API 是 GET https://management.azure.com/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.ApiManagement/service/apimService1/reports/byRequest?$filter=timestamp ge datetime'2017-06-01T00:00:00' and timestamp le datetime'2017-06-04T00:00:00'&api-version=2017-03-01

The response includes the ApiId, OperationId, UserId, his subscriptionId to the Product etc, which might be beneficial to you.响应包括 ApiId、OperationId、UserId、他对产品的 subscriptionId 等,这可能对您有益。

{
    "value": [
     {
      "apiId": "/apis/5931a75ae4bbd512a88c680b",
      "operationId": "/apis/5931a75ae4bbd512a88c680b/operations/-",
      "productId": "/products/-",
      "userId": "/users/1",
      "method": "GET",
      "url": "https://apimService1.azure-api.net/echo/resource?param1=sample",
      "ipAddress": "207.xx.155.xx",
      "responseCode": 404,
      "responseSize": 405,
      "timestamp": "2017-06-03T00:17:00.1649134Z",
      "cache": "none",
      "apiTime": 221.1544,
      "serviceTime": 0,
        "apiRegion": "East Asia",
      "subscriptionId": "/subscriptions/5600b59475ff190048070002",
        "requestId": "63e7119c-26aa-433c-96d7-f6f3267ff52f",
      "requestSize": 0
      }]
  }

Check this out Reports_ByRequest看看这个Reports_ByRequest

Also, check the Azure monitor integration此外,检查Azure 监视器集成

For those who are looking for aggregate usage by user (perhaps for monetization) - there is a " byUser " endpoint as well.对于那些正在寻找用户汇总使用情况(可能是为了货币化)的人 - 还有一个“ byUser ”端点。 The request is structured like below:请求的结构如下:

https://{api-service-name}.management.azure-api.net/subscriptions/{subscription}/resourceGroups/{resource-group}/providers/Microsoft.ApiManagement/service/{api-service-name}/reports/byUser?$filter=timestamp ge datetime'2019-12-01T00:00:00' and timestamp le datetime'2019-12-04T00:00:00'&api-version=2017-03-01

The documentation says to make a request to " https://management.azure-api.net/[...] " but I had to prepend the resource name like in the request above.文档说要向“ https://management.azure-api.net/[...] ”发出请求,但我必须像上面的请求一样预先添加资源名称。

And the response:和回应:

{
  "value": [
    {
      "name": "Administrator",
      "userId": "/users/1",
      "callCountSuccess": 13,
      "callCountBlocked": 1,
      "callCountFailed": 0,
      "callCountOther": 0,
      "callCountTotal": 14,
      "bandwidth": 11019,
      "cacheHitCount": 0,
      "cacheMissCount": 0,
      "apiTimeAvg": 1015.7607923076923,
      "apiTimeMin": 330.3206,
      "apiTimeMax": 1819.2173,
      "serviceTimeAvg": 957.094776923077,
      "serviceTimeMin": 215.24,
      "serviceTimeMax": 1697.3612
    },
    {
      "name": "Samir Solanki",
      "userId": "/users/56eaec62baf08b06e46d27fd",
      "callCountSuccess": 0,
      "callCountBlocked": 0,
      "callCountFailed": 0,
      "callCountOther": 0,
      "callCountTotal": 0,
      "bandwidth": 0,
      "cacheHitCount": 0,
      "cacheMissCount": 0,
      "apiTimeAvg": 0,
      "apiTimeMin": 0,
      "apiTimeMax": 0,
      "serviceTimeAvg": 0,
      "serviceTimeMin": 0,
      "serviceTimeMax": 0
    },
    {
      "name": "Anonymous",
      "userId": "/users/54c800b332965a0035030000",
      "callCountSuccess": 0,
      "callCountBlocked": 0,
      "callCountFailed": 0,
      "callCountOther": 0,
      "callCountTotal": 0,
      "bandwidth": 0,
      "cacheHitCount": 0,
      "cacheMissCount": 0,
      "apiTimeAvg": 0,
      "apiTimeMin": 0,
      "apiTimeMax": 0,
      "serviceTimeAvg": 0,
      "serviceTimeMin": 0,
      "serviceTimeMax": 0
    }
  ],
  "count": 3,
  "nextLink": ""
}

If you need to filter by type of request or API, you can do that as well - List by User如果您需要按请求或 API 的类型进行过滤,您也可以这样做 - 按用户列出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM