Monitoring extensions with custom metrics in Kyma

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Monitor extensions with custom metrics in Kyma

Monitor Extensions with Custom Metrics in Kyma

Business Scenario

After deploying your microservice-based application to Kyma, you want to monitor it continuously with Kyma's observability capabilities.

Exercise Options

You can perform this exercise in two ways:

  1. Platform Simulation – follow the step-by-step instructions within the simulation.
  2. Live Environment - by using the instructions provided below, you can perform the steps in the SAP BTP Free Tier account.
Note
We strongly recommend first performing the steps in the live environment.

Platform Simulation

Choose the Start Exercise button below to open a simulation of the platform. Then follow the step-by-step instructions to monitor extensions with custom metrics in Kyma.

Live Environment

In this exercise, you will perform the following steps:

  1. Create a deployment and service for a sample application that exposes a metrics endpoint.
  2. Configure Prometheus to scrape the metrics endpoint and use Prometheus UI to query the metrics.
  3. Use Grafana to visualize metrics.

Prerequisites

  • You have successfully created a SAP BTP, Kyma runtime instance in the SAP BTP subaccount.
  • You have configured kubectl to work with your SAP BTP, Kyma runtime instance.

Task 1: Create a deployment and service for a sample application

Steps

  1. Create a deployment and service for a sample application that exposes a metrics endpoint.

    In this task, you will create a new hello-kyma deployment and service in a new namespace called monitoring-exercise. The deployment will use the ghcr.io/sap-samples/kyma-runtime-learning-journey/hello-kyma:1.0.0 image, which exposes a metrics endpoint on port 8080.

    1. Create a new namespace called monitoring-exercise:

      Code snippet
      kubectl create namespace monitoring-exercise
      Copy code
    2. Create a new deployment called hello-kyma in the monitoring-exercise namespace:

      Code snippet
      kubectl apply -n monitoring-exercise -f https://raw.githubusercontent.com/SAP-samples/kyma-runtime-learning-journey/main/unit_8/hello-kyma-deployment-svc.yaml
      Copy code
    3. Verify the successful creation of the deployment:

      Code snippet
      kubectl get deployments -n monitoring-exercise
      Copy code

      You should see the following output:

      Code snippet
      NAME         READY   UP-TO-DATE   AVAILABLE   AGE
      hello-kyma   1/1     1            1           2m
      Copy code
    4. Port-forward the hello-kyma service to your local machine:

      Code snippet
      kubectl port-forward -n monitoring-exercise svc/hello-kyma 8080:8080
      Copy code

      Leave this terminal window open and open http://localhost:8080/metrics in a new browser tab. You should see a lot of metrics being exposed by the hello-kyma application.

    5. Stop the port-forwarding by pressing Ctrl+C in the terminal window.

Result

Bravo! You have completed this exercise: you have used Kyma's observability capabilities to monitor your extensions and custom metrics with Prometheus and Grafana.

Task 2: Configure Prometheus to scrape the metrics endpoint and use Prometheus UI to query the metrics

Steps

  1. Create a deployment and service for a sample application that exposes a metrics endpoint.

    In this task, you will create a new hello-kyma deployment and service in a new namespace called monitoring-exercise. The deployment will use the ghcr.io/sap-samples/kyma-runtime-learning-journey/hello-kyma:1.0.0 image, which exposes a metrics endpoint on port 8080.

    1. Create a new namespace called monitoring-exercise:

      Code snippet
      kubectl create namespace monitoring-exercise
      Copy code
    2. Create a new deployment called hello-kyma in the monitoring-exercise namespace:

      Code snippet
      kubectl apply -n monitoring-exercise -f https://raw.githubusercontent.com/SAP-samples/kyma-runtime-learning-journey/main/unit_8/hello-kyma-deployment-svc.yaml
      Copy code
    3. Verify the successful creation of the deployment:

      Code snippet
      kubectl get deployments -n monitoring-exercise
      Copy code

      You should see the following output:

      Code snippet
      NAME         READY   UP-TO-DATE   AVAILABLE   AGE
      hello-kyma   1/1     1            1           2m
      Copy code
    4. Port-forward the hello-kyma service to your local machine:

      Code snippet
      kubectl port-forward -n monitoring-exercise svc/hello-kyma 8080:8080
      Copy code

      Leave this terminal window open and open http://localhost:8080/metrics in a new browser tab. You should see a lot of metrics being exposed by the hello-kyma application.

    5. Stop the port-forwarding by pressing Ctrl+C in the terminal window.

  2. Configure Prometheus to scrape the metrics endpoint and use Prometheus UI to query the metrics.

    1. Create a ServiceMonitor resource for the hello-kyma service:

      1. In a new file called hello-kyma-servicemonitor.yaml, add the following content:

        Code snippet
        apiVersion: monitoring.coreos.com/v1
        kind: ServiceMonitor
        metadata:
          namespace: kyma-system
          name: monitoring-hello-kyma
          labels:
             app: hello-kyma
        spec:
          selector:
             matchLabels:
                app: hello-kyma
          endpoints:
             - port: http
          namespaceSelector:
             any: true
        Copy code
      2. Apply the ServiceMonitor resource to the kyma-system namespace:

        Code snippet
        kubectl apply -n kyma-system -f hello-kyma-servicemonitor.yaml
        Copy code

      3. Verify that the ServiceMonitor resource was created successfully:

        Code snippet
        kubectl get servicemonitors -n kyma-system
        Copy code

    2. Verify that Prometheus is scraping the metrics endpoint of the hello-kyma service:

      1. Open the Prometheus UI by running the following command:

        Code snippet
        kubectl port-forward -n kyma-system svc/monitoring-prometheus 9090:9090
        Copy code

      2. Open http://localhost:9090/targets in a new browser tab. You should see serviceMonitor/kyma-system/monitoring-hello-kyma listed as a target.
      3. Open http://localhost:9090/graph and enter the following query:

        Code snippet
        promhttp_metric_handler_requests_total{service="hello-kyma", code="200"}
        Copy code

        This query will return the number of successful requests to the hello-kyma service. You should see a graph for the last 5 minutes.

      4. Stop the port-forwarding by pressing Ctrl+C in the terminal window.

Task 3: Use Grafana to visualize metrics

Steps

  1. Use Grafana to visualize metrics.

    1. Open the Grafana UI by running the following command:

      Code snippet
      kubectl port-forward -n kyma-system svc/monitoring-grafana 3000:80
      Copy code
    2. Open http://localhost:3000 in a new browser tab.

    3. On the left side, click the Explore button.

    4. In the Explore view, click the Add Query button.

    5. In the Query field, enter the following query:

      Code snippet
      rate(promhttp_metric_handler_requests_total{service="hello-kyma", code="200"}[1m])
      Copy code

      This query will return the number of successful requests to the hello-kyma service per minute.

      Note
      To learn more about Prometheus Query Language (PromQL), see Prometheus Query Language.

Result

Bravo! You have completed this exercise: you have used Kyma's observability capabilities to monitor your extensions and custom metrics with Prometheus and Grafana.

Log in to track your progress & complete quizzes