Skip to content

Scrape MongoDB Flex Observability data with Prometheus

With the following steps you can learn how to scrape your MongoDB Flex Observability data with Prometheus.

  • You have created a MongoDB Flex instance.
    Check Create and manage instances of MongoDB Flex.
  • You have a running instance of Prometheus. This guide assumes, that you use a container. This is, why we reference it in the text. You can also use a Virtual Machine or run it in another environment.

Before configuring your Prometheus environment, you need to prepare the needed service account. This guide uses the Portal for achieving this task. You can also use the STACKIT CLI.

Create a Service Account and grant access to the API

Section titled “Create a Service Account and grant access to the API”
  1. Navigate to IAM and management > Service accounts.

  2. Click + Create service account.

  3. Give the new service account a name and click on Create. The account has been created.

  4. Go into the account by clicking its name.

  5. Navigate to Service account keys.

  6. Click + Create service account key.

  7. Select Create new key pair and click on Create.

  8. Download the newly created credentials. At least you need the JSON file that contains all relevant information.

  9. Navigate to IAM and management* Access.

  10. Click + Grant Access.

  11. Add the mail address (ID) of the newly created service account.

  12. Select the role MongoDB Flex Reader and click Save.

  1. Copy the JSON file, you downloaded in the Preparation, to your virtual machine.

    In this guide we copy it to /usr/local/etc/stackit/sa/service-account.json. If you choose another location, you need to adapt the commands in the following steps.

  2. Create a cronjob for updating the Bearer Token.

    Manually try to acquire a token with the STACKIT CLI.

    Terminal window
    stackit auth activate-service-account --service-account-key-path /usr/local/etc/stackit/sa/service-account.json --only-print-access-token

    The maximum token lifetime is 60 minutes, so the token needs to be renewed beforehand. To automate this step, you can place this command in crontab and write the token in a new file. This file should also be protected.

    */5 * * * * /usr/bin/stackit auth activate-service-account --service-account-key-path /usr/local/etc/stackit/sa/service-account.json --only-print-access-token > /run/secrets/bearer_token 2>/dev/null
  3. Configure Prometheus Now configure Prometheus to your needs. Use the following code snippet for scraping the MongoDB Flex metrics. Replace projectID, region and MongoDBFlexInstanceID with the according values.

    scrape_configs:
    - job_name: "<job name>"
    metrics_path: "/v2/projects/[projectID]/regions/[region]/instances/[MongoDBFlexInstanceID]/metrics"
    scheme: "https"
    bearer_token_file: /run/secrets/bearer_token
    static_configs:
    - targets: ['mongodb-prom-proxy.api.stackit.cloud:443']
  4. Restart the container or service

  5. Test Prometheus

    Open your Prometheus instance with a browser. Navigate to Status > Monitoring status: “Target health”. The State of the defined scraper should be UP:

    Prometheus with MongoDB Flex