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
Explore the status product and review the default metrics.
In the 3scale Admin Portal, navigate to the monitoring_analytics product page.
In the left pane, click → .
The default metric, Hits, is incremented every time that a request for the product arrives.
Create a new metric to track the number of requests to the /status endpoint of the Status API.
In the left pane, click → . Then, click . Complete the form according to the following values:
| Field | Value |
|---|---|
| Friendly name |
status
|
| System name |
status
|
| Unit |
1
|
| Description | Tracking requests to the status endpoint. |
Click .
In the status metric row, click to attach the metric to the /status path.
Complete the form according to the following values:
| Field | Value |
|---|---|
| Verb |
GET
|
| Pattern |
/status
|
| Method or Metric to increment | Select Metric.
Then, select status. |
Then, click .
Create a new metric to track the 203 HTTP status codes returned by the status product.
In the left pane, click → . Then, click . Complete the form according to the following values:
| Field | Value |
|---|---|
| Friendly name |
status-203
|
| System name |
status-203
|
| Unit |
1
|
| Description | Tracking of 203 HTTP response codes. |
Click .
Add the Custom Metrics policy, so that APIcast can increment the status-203 metric on every request that returns a 203 HTTP status code.
In the left pane, click → . Then, click .
Within the policies list, select Custom Metrics.
Then, use the arrow icon to move the Custom Metrics policy to the first position.
Click to update the policy. Complete the form according to the following values:
| Field | Value |
|---|---|
| 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.
Click . Then, click .
Promote the changes of the status product to staging.
Navigate to → .
Click to promote the changes.
From the curl example, copy the user_key parameter.
You use this later in this exercise.
Verify that the status-203 metric is only incremented when a 203 HTTP status code is returned.
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 \Making 20 requests Responses with status code 200: 7 Responses with status code 201: 8USER_KEYResponses with status code 203: 5
Track the number of responses with the 203 status code.
In the Admin Portal, navigate to → .
In the drop-down menu, select the status-203 metric.
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.
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.
This concludes the guided exercise.