Abstract
| Goal | Configure monitoring and analytics for APIs and the 3scale API management infrastructure. |
| Objectives |
|
| Sections |
|
After completing this section, you should be able to configure API analytics to gather runtime metrics for APIs.
You use a metric to track the usage of your APIs deployed on Red Hat 3cale API Management.
For example, you can define a new metric to track the number of times that the 403 HTTP status code is returned.
Every time your API returns a 403 status code, the metric is incremented.
You can define a new metric at the 3cale API Management product level, therefore, a product is composed of one or many metrics.
By default, 3scale creates the Hits metric, which counts the number of requests that reach the product at any path.
For example, if you create a product called product1, then 3cale API Management creates the Hits metric for the / path, which means that a request to any path increments the metric.
A metric is composed of several fields. The following table gives an explanation of every field.
| Field | Explanation |
|---|---|
| Friendly name | The name used in the Admin Portal UI. |
| System name | A unique name that identifies the metric within the 3scale API Management system. |
| Unit | The quantifiable measurement that is incremented. |
| Description | Additional information that you might find useful to identify the metric. |
To manage the metrics you can use the Admin Portal or the CLI Toolbox.
To manage the metrics through the Admin Portal, navigate to the product page of the product that you want to manage. Then, click → . The page displays the metrics for the product.
To create a new metric, click and complete the form.
To manage the metrics through the CLI Toolbox, you can use the 3scale metric super command.
You can create a new metric by using the 3scale metric create command.
For example, if you want to create a metric metric1 for the product1 product in the 3scale tenant, then a possible command is as follows:
[student@workstation ~]$ 3scale metric create 3scale-tenant product1 metric1 --unit=1You can update an existing metric by using the 3scale metric apply command.
The following command updates the name for the metric1 metric.
[student@workstation ~]$ 3scale metric apply 3scale-tenant product1 metric1 --name=metric2You can list the existing metrics by using the 3scale metric list.
[student@workstation ~]$ 3scale metric list 3scale-tenant product1
ID FRIENDLY_NAME SYSTEM_NAME UNIT DESCRIPTION
8 Hits hits hit Number of API hits
10 status-203 status-203 1 status 203The previous commands include several options to modify the metric, such as name or description. For a complete understanding of the options associated to every command, visit the official Red Hat documentation.
The main purpose of a metric is to measure, therefore, it must be incremented based on your specific use cases.
A metric can be linked to a mapping rule. When you create a mapping rule, you have the option to select a metric to increment. If a metric is linked to a mapping rule, then the metric gets incremented every time that the mapping rule receives a request.
If the metric is not linked to a mapping rule, then an external system must perform the incrementation.
APIcast includes a policy, Custom Metrics, which you can use to increment a metric based on the HTTP status code of the response of the API.
For example, if you want to track the number of 300 status codes that your API returns, then you can configure the Custom Metrics policy to increment a metric on every response that meets the criteria.
To add the policy, navigate to → and click .
Then, select Custom Metrics from the policies list.
For the policy to work, you must give it priority over the default APIcast policy policy.
The policy is composed of one or several operations combined by a condition.
You can consider that every operation is an equality expression with two operands: left and right.
The operation holds an op field, which is applied to the left and right operands.
left (op) right
The left and right fields can be plain text, or Liquid (the programming language) expressions.
In the Liquid programming language, variables are enclosed with two brackets (for example, {{variable}}).
The policy provides the built-in variable {{status}}, which is used to get the status code of the response.
For example, if you only want the metric to be incremented when the status code is 404, then the expression is similar to the following:
404 matches {{status}}3scale API Management evaluates the previous expression on every request, and only increments the metric if the evaluation is true.
You can include complex expressions by creating several operations combined by a logical expression (and or or).
(404 matches {{status}}) OR (500 matches {{status}})The previous expression increments the metric if the status code is equal to 404 or 500.
To configure the policy, you must complete the form, which includes several fields.
![]() |
The following table explains the fields involved in the configuration of the policy.
| Field | Explanation |
|---|---|
| left | The left operand of the expression. |
| left_type | The type of the left operand (either plain text or liquid expression). |
| op | The operation to apply to the left and right operands. |
| right | The right operand of the expression. |
| right_type | The type of the right operand (either plain text or liquid expression). |
| Metric to increment | The system name of the metric to increment if the expression evaluates to true. |
| Increment | The number that is added to the metric. |