Skip to content

Connect MongoDB Flex with STACKIT Observability

To get a visual representation of your metrics and have your metrics saved, you can use STACKIT Observability service. With the following steps you can learn how to integrate MongoDB into your Observability service instance.

Before configuring Grafana Alloy 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. Navigate to Logging & Monitoring > Observability.

  2. Select your Observability instance.

  3. Navigate to API > Credentials.

  4. Click + Create credentials and then click Create.

Prepare the helper scripts and configure Grafana Alloy

Section titled “Prepare the helper scripts and configure Grafana Alloy”
  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 Grafana Alloy Now configure Grafana Alloy to your needs. Use the following code snippet for scraping the MongoDB Flex metrics and forward them to STACKIT Observability. Replace projectID, region, mongoDBFlexInstanceID, jobName, ObservabilityInstanceID and observabilityAPIUsername with the according values.

    prometheus.scrape "mongodb" {
    targets = [
    {
    __address__ = "mongodb-prom-proxy.api.stackit.cloud:443",
    __metrics_path__ = "/v2/projects/[projectID]/regions/[region]/instances/[mongoDBFlexInstanceID]/metrics",
    },
    ]
    forward_to = [
    prometheus.remote_write.observability.receiver,
    ]
    job_name = "[jobName]"
    scheme = "https"
    scrape_interval = "5m"
    authorization {
    type = "Bearer"
    credentials_file = "/run/secrets/bearer_token"
    }
    }
    prometheus.remote_write "observability" {
    endpoint {
    url = "https://push.metrics.stackit5.argus.eu01.stackit.cloud/instances/`ObservabilityInstanceID`/api/v1/receive"
    basic_auth {
    username = "[observabilityAPIUsername]"
    password_file = "/run/secrets/observability_write_password"
    }
    }
    }
  4. Restart the container or service

  5. Test Grafana Alloy

    Open your Grafana Alloy instance with a browser. The dashboard shows the defined components with their health state. The Health column should show “healthy” for your configurations, according to the screenshot below.

    Grafana Alloy dashboard shows a healthy state

Check the result with STACKIT Observability

Section titled “Check the result with STACKIT Observability”
  1. Navigate to Logging & Monitoring > Observability.

  2. Select your observability instance.

  3. Navigate to Grafana and follow the link to Grafana Dashboard.

  4. Login with your credentials (or SSO).

  5. In Grafana navigate to Explore > Metrics.

  6. Click on + New metric exploration.

    In the metrics exploration page, search for mongodb_. You should see now the MongoDB Flex metrics:

    mongo DB Flex metrics in Grafana

Enhance the experience with Grafana by importing our template

Section titled “Enhance the experience with Grafana by importing our template”

We offer a Grafana template that suits to STACKIT MongoDB Flex. Visit the Observability metrics of MongoDB Flex reference article, to download the file.