Bookmark this page

Chapter 6. Monitoring APIs with Red Hat 3scale API Management

Abstract

Goal Configure monitoring and analytics for APIs and the 3scale API management infrastructure.
Objectives
  • Configure API analytics to gather runtime metrics for APIs.

  • Configure monitoring and metrics collection for the 3scale API Management infrastructure.

Sections
  • Configuring API Analytics (and Guided Exercise)

  • Monitoring 3Scale Infrastructure (and Guided Exercise)

  • Monitoring APIs with Red Hat 3scale API Management (Quiz)

Configuring API Analytics

Objectives

After completing this section, you should be able to configure API analytics to gather runtime metrics for APIs.

Introducing Metrics in 3scale

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.

FieldExplanation
Friendly nameThe name used in the Admin Portal UI.
System nameA unique name that identifies the metric within the 3scale API Management system.
UnitThe quantifiable measurement that is incremented.
DescriptionAdditional information that you might find useful to identify the metric.

Managing Metrics

To manage the metrics you can use the Admin Portal or the CLI Toolbox.

Managing Metrics Through the Admin Portal

To manage the metrics through the Admin Portal, navigate to the product page of the product that you want to manage. Then, click Integration:Methods & Metrics. The page displays the metrics for the product.

To create a new metric, click New metric and complete the form.

Managing Metrics Through the CLI Toolbox

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=1

You 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=metric2

You 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 203

The 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.

Incrementing Metrics

The main purpose of a metric is to measure, therefore, it must be incremented based on your specific use cases.

Incrementing Metrics Linked to a Mapping Rule

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.

Incrementing Metrics Through an APIcast Custom Policy

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 Integration:Policies and click Add policy. 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.

Figure 6.1: Sample product with a configured Custom Metrics policy

The following table explains the fields involved in the configuration of the policy.

FieldExplanation
leftThe left operand of the expression.
left_typeThe type of the left operand (either plain text or liquid expression).
opThe operation to apply to the left and right operands.
rightThe right operand of the expression.
right_typeThe type of the right operand (either plain text or liquid expression).
Metric to incrementThe system name of the metric to increment if the expression evaluates to true.
IncrementThe number that is added to the metric.
Revision: do240-2.11-40390f6