Bookmark this page

Guided Exercise: Configuring API Analytics

In this exercise, you will use an APIcast policy to create a metric that measures the number of 203 HTTP status codes for a product.

Outcomes

You should be able to configure the Custom Metrics APIcast policy to increment a metric based on the HTTP status code of the response.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

[student@workstation ~]$ lab start monitoring-analytics

The start function deploys the status-api application. The application exposes an endpoint, /status/{code}, which returns the HTTP status code (200, 201 or 203) provided in the path parameter. You will use this API to track the number of responses that return the 203 status code. At the same time, a product and a backend, called status, are created to perform the exercise.

Procedure 6.1. Instructions

  1. Explore the status product and review the default metrics.

    1. In the 3scale Admin Portal, navigate to the monitoring_analytics product page.

    2. In the left pane, click AnalyticsTraffic. The default metric, Hits, is incremented every time that a request for the product arrives.

  2. Create a new metric to track the number of requests to the /status endpoint of the Status API.

    1. In the left pane, click Integration:Methods & Metrics. Then, click New metric. Complete the form according to the following values:

      FieldValue
      Friendly name status
      System name status
      Unit 1
      DescriptionTracking requests to the status endpoint.

      Click Create Metric.

    2. In the status metric row, click Add a mapping rule to attach the metric to the /status path. Complete the form according to the following values:

      FieldValue
      Verb GET
      Pattern /status
      Method or Metric to incrementSelect Metric. Then, select status.

      Then, click Create Mapping Rule.

  3. Create a new metric to track the 203 HTTP status codes returned by the status product.

    1. In the left pane, click Integration:Methods & Metrics. Then, click New metric. Complete the form according to the following values:

      FieldValue
      Friendly name status-203
      System name status-203
      Unit 1
      DescriptionTracking of 203 HTTP response codes.

      Click Create Metric.

  4. Add the Custom Metrics policy, so that APIcast can increment the status-203 metric on every request that returns a 203 HTTP status code.

    1. In the left pane, click Integration:Policies. Then, click Add policy.

    2. Within the policies list, select Custom Metrics. Then, use the arrow icon to move the Custom Metrics policy to the first position.

    3. Click Custom Metrics to update the policy. Complete the form according to the following values:

      FieldValue
      Metric to increment status-203
      left {{status}}
      right 203
      op matches
      right_type Evaluate 'right' as plain text
      left_type Evaluate 'left' as liquid

      This policy increments the status-203 metrics whenever the status code of the response is 203.

    4. Click Update Policy. Then, click Update Policy Chain.

  5. Promote the changes of the status product to staging.

    1. Navigate to Integration:Configuration.

    2. Click Promote v.1 to Staging APIcast to promote the changes.

    3. From the curl example, copy the user_key parameter. You use this later in this exercise.

  6. Verify that the status-203 metric is only incremented when a 203 HTTP status code is returned.

    1. In your command-line terminal, run the DO240/labs/monitoring-analytics/test_status_api.sh script by providing your user key as a parameter. The script sends 20 requests to the Status API. The script also collects the number of times that every status code is returned.

      [student@workstation ~]$ DO240/labs/monitoring-analytics/test_status_api.sh \
       USER_KEY
      Making 20 requests
      Responses with status code 200: 7
      Responses with status code 201: 8
      Responses with status code 203: 5

      Track the number of responses with the 203 status code.

    2. In the Admin Portal, navigate to Analytics:Traffic. In the drop-down menu, select the status-203 metric.

    3. The number that the status-203 metric displays should be equal to the number of responses with the 203 status code provided by the test-status-api.sh script. Although the Status API has returned other status codes, only the 203 status code has incremented the metric.

    4. In the drop-down menu, select the status metric. The number displayed should be equal to 20, which is the number of total requests sent to the product.

Finish

On the workstation machine, use the lab command to complete this exercise. This is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish monitoring-analytics

This concludes the guided exercise.

Revision: do240-2.11-40390f6