Counting total API calls for your Analytics service

Count the API calls for the IBM® API Connect Analytics subsystem and return the total categorized by response status.

Before you begin

This procedure requires you to deploy the Analytics subsystem and store data in IBM API Connect. If your organization did not deploy the Analytics subsystem, then you cannot use this process to track usage. If you deployed Analytics but offload all of your data and do not store it in IBM API Connect, then you must capture usage information from the third-party application where you offload data.

About this task

If your IBM API Connect bases costs on usage, you can use the following procedure to track this information so that you can accurately report it.

Procedure

  1. Find an analytics-storage pod (for example, coordinating, data, master, or basic) by running the following command.
    kubectl get pods
  2. Build a query for your request to the analytics datastore.
    1. Set the size to 0 to disable the return of raw data.

      The following example shows how to specify the size.

      {
        "size": 0
      }
      Tip: If you want to return some raw data to verify that the range query in the next step works, set the size to 1 instead.
    2. Add a range query for date filtering.

      The following example shows how to specify the time range in your query:

      {
        "size": 0,
        "query": {
          "range": {
            "datetime": {
              "gte": "YYYY-MM-DD",
              "lt": "YYYY-MM-DD"
            }
          }
        }
      } 

      This setting filters the query by specifying a time range that begins on or after (is greater than or equal to) the gte date, and ends before (is less than) the lt.

      Note:

      When specifying the time range, note the following considerations about the value that you specify for the date:

      • Format: the value must use the ISO-8601 format (YYYY-MM-DDThh:mm:ss.yyyZ) or a shortened version of that format (for example, YYYY-MM-DD).
      • Timezone: The value must follow UTC time (Coordinated Universal Time) because the query will be executed by the analytics-storage pod (which runs on UTC time), so it's important to allow for any time difference when you run a query.
    3. Build the aggregation.

      The aggregation code uses regular expressions to match the patterns for response codes (for example all of the responses with a code that begins with "1"). The query counts the documents within the specified time range that match each set of response codes, and then calculates the total number of documents.

      The following example shows the complete query. The aggs section contains the code to count the total number of API calls.

      {
        "size": 0,
        "query": {
          "range": {
            "datetime": {
              "gte": "YYYY-MM-DD",
              "lt": "YYYY-MM-DD"
            }
          }
        },
        "aggs": {
          "status_codes": {
            "filters": {
              "filters": {
                "1xx": {
                  "regexp": {
                    "status_code": "1.*"
                  }
                },
                "2xx": {
                  "regexp": {
                    "status_code": "2.*"
                  }
                },
                "3xx": {
                  "regexp": {
                    "status_code": "3.*"
                  }
                },
                "4xx": {
                  "regexp": {
                    "status_code": "4.*"
                  }
                },
                "5xx": {
                  "regexp": {
                    "status_code": "5.*"
                  }
                }
              }
            }
          }
        }
      }
  3. Submit a request with your query to the analytics-storage pod that you located in step 1.

    Run the following command, where:

    • analytics-storage-pod represents the name of the analytics-storage pod that you identified in step 1.
    • analytics_query is the content of the query, as shown in step 2c.
    kubectl exec -it analytics-storage-pod -- \
    curl_es /apic-api-r/_search?pretty \
    -d 'analytics_query' \
    -H 'Content-Type: application/json'
  4. Review the response.

    Most of the information in the response can be ignored because it doesn't apply to your use case. Locate the aggregations.status_codes.buckets field, which contains the individual fields that represent the status codes.

    The status code fields are labeled as 1xx, 2xx, 3xx, 4xx, and 5xx. Each of the status code fields has an object as its value with a field called doc_count.

    In each status code field, locate the doc_count field and note its value, which represents the total number of API calls for the current status code within the specified time range.

    The following example shows a sample response with an aggregated total of 100,000 API calls.

    {
      "took" : 19,
      "timed_out" : false,
      "_shards" : {
        "total" : 2500,
        "successful" : 2500,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 100000,
        "max_score" : 0.0,
        "hits" : [ ]
      },
      "aggregations" : {
        "status_codes" : {
          "buckets" : {
            "1xx" : {
              "doc_count" : 10000
            },
            "2xx" : {
              "doc_count" : 60000
            },
            "3xx" : {
              "doc_count" : 5000
            },
            "4xx" : {
              "doc_count" : 15000
            },
            "5xx" : {
              "doc_count" : 10000
            }
          }
        }
      }
    }

What to do next

To ensure you correctly track the API calls, you should run the query on a daily basis and store the accumulated daily totals where you can make sure they are backed up. You can create a script based on the query and schedule it to run daily. For more information about creating the script, see Creating a script to count API calls.