Managing metrics

You can collect metrics to monitor how cluster components and your own workloads are performing.

Understanding metrics

In OKD 4, cluster components are monitored by scraping metrics exposed through service endpoints. You can also configure metrics collection for user-defined projects. Metrics enable you to monitor how cluster components and your own workloads are performing.

You can define the metrics that you want to provide for your own workloads by using Prometheus client libraries at the application level.

In OKD, metrics are exposed through an HTTP service endpoint under the /metrics canonical name. You can list all available metrics for a service by running a curl query against http://<endpoint>/metrics. For instance, you can expose a route to the prometheus-example-app example application and then run the following to view all of its available metrics:

  1. $ curl http://<example_app_endpoint>/metrics

Example output

  1. # HELP http_requests_total Count of all HTTP requests
  2. # TYPE http_requests_total counter
  3. http_requests_total{code="200",method="get"} 4
  4. http_requests_total{code="404",method="get"} 2
  5. # HELP version Version information about this binary
  6. # TYPE version gauge
  7. version{version="v0.1.0"} 1

Additional resources

Setting up metrics collection for user-defined projects

You can create a ServiceMonitor resource to scrape metrics from a service endpoint in a user-defined project. This assumes that your application uses a Prometheus client library to expose metrics to the /metrics canonical name.

This section describes how to deploy a sample service in a user-defined project and then create a ServiceMonitor resource that defines how that service should be monitored.

Deploying a sample service

To test monitoring of a service in a user-defined project, you can deploy a sample service.

Procedure

  1. Create a YAML file for the service configuration. In this example, it is called prometheus-example-app.yaml.

  2. Add the following deployment and service configuration details to the file:

    1. apiVersion: v1
    2. kind: Namespace
    3. metadata:
    4. name: ns1
    5. ---
    6. apiVersion: apps/v1
    7. kind: Deployment
    8. metadata:
    9. labels:
    10. app: prometheus-example-app
    11. name: prometheus-example-app
    12. namespace: ns1
    13. spec:
    14. replicas: 1
    15. selector:
    16. matchLabels:
    17. app: prometheus-example-app
    18. template:
    19. metadata:
    20. labels:
    21. app: prometheus-example-app
    22. spec:
    23. containers:
    24. - image: ghcr.io/rhobs/prometheus-example-app:0.4.1
    25. imagePullPolicy: IfNotPresent
    26. name: prometheus-example-app
    27. ---
    28. apiVersion: v1
    29. kind: Service
    30. metadata:
    31. labels:
    32. app: prometheus-example-app
    33. name: prometheus-example-app
    34. namespace: ns1
    35. spec:
    36. ports:
    37. - port: 8080
    38. protocol: TCP
    39. targetPort: 8080
    40. name: web
    41. selector:
    42. app: prometheus-example-app
    43. type: ClusterIP

    This configuration deploys a service named prometheus-example-app in the user-defined ns1 project. This service exposes the custom version metric.

  3. Apply the configuration to the cluster:

    1. $ oc apply -f prometheus-example-app.yaml

    It takes some time to deploy the service.

  4. You can check that the pod is running:

    1. $ oc -n ns1 get pod

    Example output

    1. NAME READY STATUS RESTARTS AGE
    2. prometheus-example-app-7857545cb7-sbgwq 1/1 Running 0 81m

Specifying how a service is monitored

To use the metrics exposed by your service, you must configure OKD monitoring to scrape metrics from the /metrics endpoint. You can do this using a ServiceMonitor custom resource definition (CRD) that specifies how a service should be monitored, or a PodMonitor CRD that specifies how a pod should be monitored. The former requires a Service object, while the latter does not, allowing Prometheus to directly scrape metrics from the metrics endpoint exposed by a pod.

This procedure shows you how to create a ServiceMonitor resource for a service in a user-defined project.

Prerequisites

  • You have access to the cluster as a user with the cluster-admin cluster role or the monitoring-edit cluster role.

  • You have enabled monitoring for user-defined projects.

  • For this example, you have deployed the prometheus-example-app sample service in the ns1 project.

    The prometheus-example-app sample service does not support TLS authentication.

Procedure

  1. Create a YAML file for the ServiceMonitor resource configuration. In this example, the file is called example-app-service-monitor.yaml.

  2. Add the following ServiceMonitor resource configuration details:

    1. apiVersion: monitoring.coreos.com/v1
    2. kind: ServiceMonitor
    3. metadata:
    4. labels:
    5. k8s-app: prometheus-example-monitor
    6. name: prometheus-example-monitor
    7. namespace: ns1
    8. spec:
    9. endpoints:
    10. - interval: 30s
    11. port: web
    12. scheme: http
    13. selector:
    14. matchLabels:
    15. app: prometheus-example-app

    This defines a ServiceMonitor resource that scrapes the metrics exposed by the prometheus-example-app sample service, which includes the version metric.

    A ServiceMonitor resource in a user-defined namespace can only discover services in the same namespace. That is, the namespaceSelector field of the ServiceMonitor resource is always ignored.

  3. Apply the configuration to the cluster:

    1. $ oc apply -f example-app-service-monitor.yaml

    It takes some time to deploy the ServiceMonitor resource.

  4. You can check that the ServiceMonitor resource is running:

    1. $ oc -n ns1 get servicemonitor

    Example output

    1. NAME AGE
    2. prometheus-example-monitor 81m

Additional resources

Viewing a list of available metrics

As a cluster administrator or as a user with view permissions for all projects, you can view a list of metrics available in a cluster and output the list in JSON format.

Prerequisites

  • You are a cluster administrator, or you have access to the cluster as a user with the cluster-monitoring-view cluster role.

  • You have installed the OKD CLI (oc).

  • You have obtained the OKD API route for Thanos Querier.

  • You are able to get a bearer token by using the oc whoami -t command.

    You can only use bearer token authentication to access the Thanos Querier API route.

Procedure

  1. If you have not obtained the OKD API route for Thanos Querier, run the following command:

    1. $ oc get routes -n openshift-monitoring thanos-querier -o jsonpath='{.status.ingress[0].host}'
  2. Retrieve a list of metrics in JSON format from the Thanos Querier API route by running the following command. This command uses oc to authenticate with a bearer token.

    1. $ curl -k -H "Authorization: Bearer $(oc whoami -t)" https://<thanos_querier_route>/api/v1/metadata (1)
    1Replace <thanos_querier_route> with the OKD API route for Thanos Querier.

Querying metrics

The OKD monitoring dashboard enables you to run Prometheus Query Language (PromQL) queries to examine metrics visualized on a plot. This functionality provides information about the state of a cluster and any user-defined workloads that you are monitoring.

As a cluster administrator, you can query metrics for all core OKD and user-defined projects.

As a developer, you must specify a project name when querying metrics. You must have the required privileges to view metrics for the selected project.

Querying metrics for all projects as a cluster administrator

As a cluster administrator or as a user with view permissions for all projects, you can access metrics for all default OKD and user-defined projects in the Metrics UI.

Prerequisites

  • You have access to the cluster as a user with the cluster-admin cluster role or with view permissions for all projects.

  • You have installed the OpenShift CLI (oc).

Procedure

  1. From the Administrator perspective in the OKD web console, select ObserveMetrics.

  2. To add one or more queries, do any of the following:

    OptionDescription

    Create a custom query.

    Add your Prometheus Query Language (PromQL) query to the Expression field.

    As you type a PromQL expression, autocomplete suggestions appear in a drop-down list. These suggestions include functions, metrics, labels, and time tokens. You can use the keyboard arrows to select one of these suggested items and then press Enter to add the item to your expression. You can also move your mouse pointer over a suggested item to view a brief description of that item.

    Add multiple queries.

    Select Add query.

    Duplicate an existing query.

    Select the Options menu kebab next to the query, then choose Duplicate query.

    Disable a query from being run.

    Select the Options menu kebab next to the query and choose Disable query.

  3. To run queries that you created, select Run queries. The metrics from the queries are visualized on the plot. If a query is invalid, the UI shows an error message.

    Queries that operate on large amounts of data might time out or overload the browser when drawing time series graphs. To avoid this, select Hide graph and calibrate your query using only the metrics table. Then, after finding a feasible query, enable the plot to draw the graphs.

    By default, the query table shows an expanded view that lists every metric and its current value. You can select ˅ to minimize the expanded view for a query.

  4. Optional: The page URL now contains the queries you ran. To use this set of queries again in the future, save this URL.

  5. Explore the visualized metrics. Initially, all metrics from all enabled queries are shown on the plot. You can select which metrics are shown by doing any of the following:

    OptionDescription

    Hide all metrics from a query.

    Click the Options menu kebab for the query and click Hide all series.

    Hide a specific metric.

    Go to the query table and click the colored square near the metric name.

    Zoom into the plot and change the time range.

    Either:

    • Visually select the time range by clicking and dragging on the plot horizontally.

    • Use the menu in the left upper corner to select the time range.

    Reset the time range.

    Select Reset zoom.

    Display outputs for all queries at a specific point in time.

    Hold the mouse cursor on the plot at that point. The query outputs will appear in a pop-up box.

    Hide the plot.

    Select Hide graph.

Additional resources

Querying metrics for user-defined projects as a developer

You can access metrics for a user-defined project as a developer or as a user with view permissions for the project.

In the Developer perspective, the Metrics UI includes some predefined CPU, memory, bandwidth, and network packet queries for the selected project. You can also run custom Prometheus Query Language (PromQL) queries for CPU, memory, bandwidth, network packet and application metrics for the project.

Developers can only use the Developer perspective and not the Administrator perspective. As a developer, you can only query metrics for one project at a time.

Prerequisites

  • You have access to the cluster as a developer or as a user with view permissions for the project that you are viewing metrics for.

  • You have enabled monitoring for user-defined projects.

  • You have deployed a service in a user-defined project.

  • You have created a ServiceMonitor custom resource definition (CRD) for the service to define how the service is monitored.

Procedure

  1. From the Developer perspective in the OKD web console, select ObserveMetrics.

  2. Select the project that you want to view metrics for in the Project: list.

  3. Select a query from the Select query list, or create a custom PromQL query based on the selected query by selecting Show PromQL. The metrics from the queries are visualized on the plot.

    In the Developer perspective, you can only run one query at a time.

  4. Explore the visualized metrics by doing any of the following:

    OptionDescription

    Zoom into the plot and change the time range.

    Either:

    • Visually select the time range by clicking and dragging on the plot horizontally.

    • Use the menu in the left upper corner to select the time range.

    Reset the time range.

    Select Reset zoom.

    Display outputs for all queries at a specific point in time.

    Hold the mouse cursor on the plot at that point. The query outputs appear in a pop-up box.

Additional resources

Getting detailed information about a metrics target

In the Administrator perspective in the OKD web console, you can use the Metrics targets page to view, search, and filter the endpoints that are currently targeted for scraping, which helps you to identify and troubleshoot problems. For example, you can view the current status of targeted endpoints to see when OKD Monitoring is not able to scrape metrics from a targeted component.

The Metrics targets page shows targets for default OKD projects and for user-defined projects.

Prerequisites

  • You have access to the cluster as an administrator for the project for which you want to view metrics targets.

Procedure

  1. In the Administrator perspective, select ObserveTargets. The Metrics targets page opens with a list of all service endpoint targets that are being scraped for metrics.

    This page shows details about targets for default OKD and user-defined projects. This page lists the following information for each target:

    • Service endpoint URL being scraped

    • ServiceMonitor component being monitored

    • The up or down status of the target

    • Namespace

    • Last scrape time

    • Duration of the last scrape

  2. Optional: The list of metrics targets can be long. To find a specific target, do any of the following:

    OptionDescription

    Filter the targets by status and source.

    Select filters in the Filter list.

    The following filtering options are available:

    • Status filters:

      • Up. The target is currently up and being actively scraped for metrics.

      • Down. The target is currently down and not being scraped for metrics.

    • Source filters:

      • Platform. Platform-level targets relate only to default Red Hat OpenShift Service on AWS projects. These projects provide core Red Hat OpenShift Service on AWS functionality.

      • User. User targets relate to user-defined projects. These projects are user-created and can be customized.

    Search for a target by name or label.

    Enter a search term in the Text or Label field next to the search box.

    Sort the targets.

    Click one or more of the Endpoint Status, Namespace, Last Scrape, and Scrape Duration column headers.

  3. Click the URL in the Endpoint column for a target to navigate to its Target details page. This page provides information about the target, including the following:

    • The endpoint URL being scraped for metrics

    • The current Up or Down status of the target

    • A link to the namespace

    • A link to the ServiceMonitor details

    • Labels attached to the target

    • The most recent time that the target was scraped for metrics