Skip to navigationSkip to main contentSkip to footerScaleway Docs HomepageAsk our AI
Ask our AI

Creating alerts programmatically for automated DevOps workflows

This guide shows you the basics of how to create and manage alerting rules in Scaleway Cockpit using the Mimir (metrics) and Loki (logs) HTTP APIs. Use the API snippets and YAML templates from this guide to set up and maintain automated monitoring and alerting workflows for your Scaleway resources.

Before you start

To complete the actions presented below, you must have:

  • A Scaleway account logged into the console
  • Owner status or IAM permissions allowing you to perform actions in the intended Organization
  • Created an IAM application with an API key and assigned it an IAM policy with the ObservabilityFullAccess permission set. For detailed instructions, see Authentication.
  • Retrieved your Metrics and Logs data source URLs from the Scaleway console (Cockpit > Data sources tab > Custom data sources section)
  • Enabled the Alert manager in the same region as your data source
  • Added contacts in the Scaleway console or configured contact points in Grafana
  • curl or any HTTP client tool installed
  • A text editor for creating YAML configuration files

API endpoints and authentication

Endpoints

Scaleway Cockpit exposes a subset of the Mimir and Loki HTTP APIs for managing alerting rules. Both APIs use the same endpoint pattern for rule management.

Mimir (metrics alerts):

  • Cockpit base URL: https://<data-source-id>.metrics.cockpit.<region>.scw.cloud
  • Rules endpoint: /prometheus/config/v1/rules

Loki (logs alerts):

  • Cockpit base URL: https://<data-source-id>.logs.cockpit.<region>.scw.cloud
  • Rules endpoint: /loki/api/v1/rules

Replace https://<data-source-id>.metrics/logs.cockpit.<region>.scw.cloud with the Metrics/Logs API URL that you can find in the Scaleway console, under Cockpit > Data sources tab > Custom data sources section.

Authentication

All API requests must include the X-Auth-Token header with your Scaleway API secret key.

Follow these steps to create your API secret key:

  1. Create an IAM application.
  2. Create a policy, making sure that:
    • When selecting the principal type, you choose the application you created in Step 1.
    • When adding rules, you select the Resource management > Cockpit > ObservabilityFullAccess permission set.
  3. Go back to your IAM application, select the API keys tab, and click Generate an API key.
  4. In the Generate an API keys dialog that appears:
    1. Provide an optional key description, and select a key expiration time.
    2. Leave the Object Storage as No, skip for now.
    3. Click Generate API key. A screen appears showing the access key and secret key for your new API key. Save the secret key in a secure place, it will not be shown again after closing the screen.

It is good practice to configure an environment variable for your API secret key (export SCW_SECRET_KEY="<YOUR_API_SECRET_KEY>") and reference that in your commands, instead of passing the secret key in the clear.

X-Auth-Token: $SCW_SECRET_KEY

Alert rule structure

Alert rules are defined in YAML format and organized into groups. Each group can contain multiple rules that share the same evaluation interval.

The basic structure of a rule group is as follows:

name: <group-name>
interval: <evaluation-interval>
rules:
  - alert: <alert-name>
    expr: <promql-or-logql-expression>
    for: <pending-period>
    labels:
      <label-key>: <label-value>
    annotations:
      summary: <alert-summary>
      description: <alert-description>

Where:

  • name: The name of the group
  • interval: How often the rules in the group are evaluated (e.g., 1m, 30s, 5m)
  • alert: The name of your alert rule
  • expr: The PromQL (for Mimir) or LogQL (for Loki) expression that defines the alert condition
  • for: How long the condition must be true before the alert fires (pending period)
  • labels: Key-value pairs attached to the alert (used for routing notifications)
  • annotations: Additional information displayed in the alert notification

For more information, see the following Grafana and Prometheus documentation:

Create alerts for metrics (Mimir API)

Step 1: Create your alert rule configuration

Create a YAML file (e.g., metrics-alerts.yaml) with your alert rules.

Example:

name: instance-alerts
interval: 1m
rules:
  - alert: HighCPUUsage
    expr: avg(rate(node_cpu_seconds_total{mode="idle"}[5m])) by (instance) < 0.2
    for: 5m
    labels:
      severity: warning
      team: infrastructure
    annotations:
      summary: "High CPU usage detected on {{ $labels.instance }}"
      description: "CPU usage is above 80% for more than 5 minutes on instance {{ $labels.instance }}. Current value: {{ $value | printf \"%.2f\" }}"
  
  - alert: InstanceDown
    expr: up{job="node-exporter"} == 0
    for: 1m
    labels:
      severity: critical
      team: infrastructure
    annotations:
      summary: "Instance {{ $labels.instance }} is down"
      description: "Instance {{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minute."

Step 2: Set the rule group via the API

  1. Configure an environment variable for your API secret key. Replace the placeholder value with your actual API secret key.

    export SCW_SECRET_KEY="<YOUR_API_SECRET_KEY>"
  2. Use curl to send your alert rules to the Mimir API:

    curl -X POST \
      -H "X-Auth-Token: $SCW_SECRET_KEY" \
      -H "Content-Type: application/yaml" \
      --data-binary @metrics-alerts.yaml \
      "https://<data-source-id>.metrics.cockpit.<region>.scw.cloud/prometheus/config/v1/rules/<namespace>"

    Replace:

    • https://<data-source-id>.metrics.cockpit.<region>.scw.cloud with the Metrics API URL that you can find in the Scaleway console, under Cockpit > Data sources tab > Custom data sources section

    • <namespace> with a namespace name to organize your rules (e.g., infrastructure, production)

      The <namespace> is a logical grouping for your rules within the API. It helps organize different sets of rules but is separate from the group name defined in your YAML file.

Step 3: Verify your alert rules

Retrieve your configured Mimir alert rules to verify they were created successfully:

curl -X GET \
  -H "X-Auth-Token: $SCW_SECRET_KEY" \
  "https://<data-source-id>.metrics.cockpit.<region>.scw.cloud/prometheus/config/v1/rules/<namespace>"

Create alerts for logs (Loki API)

Step 1: Create your alert rule configuration

Create a YAML file (e.g., logs-alerts.yaml) with your log-based alert rules.

Example:

name: application-logs-alerts
interval: 1m
rules:
  - alert: HighErrorRate
    expr: sum(rate({app="my-app"} |= "ERROR" [5m])) by (app) > 10
    for: 5m
    labels:
      severity: critical
      team: backend
    annotations:
      summary: "High error rate detected in {{ $labels.app }}"
      description: "Application {{ $labels.app }} is generating more than 10 errors per second for the last 5 minutes."
  
  - alert: DiskSpaceWarning
    expr: sum(rate({resource_type="instance"} |= "disk space" |~ "warning|critical" [10m])) by (resource_id) > 0
    for: 10m
    labels:
      severity: warning
      team: infrastructure
    annotations:
      summary: "Disk space warning on instance {{ $labels.resource_id }}"
      description: "Disk space warnings detected on instance {{ $labels.resource_id }}."

Step 2: Set the rule group via the API

  1. Configure an environment variable for your API secret key. Replace the placeholder value with your actual API secret key.

    export SCW_SECRET_KEY="<YOUR_API_SECRET_KEY>"
  2. Use curl to send your log alert rules to the Loki API:

    curl -X POST \
      -H "X-Auth-Token: $SCW_SECRET_KEY" \
      -H "Content-Type: application/yaml" \
      --data-binary @logs-alerts.yaml \
      "https://<data-source-id>.logs.cockpit.<region>.scw.cloud/loki/api/v1/rules/<namespace>"

    Replace:

    • https://<data-source-id>.logs.cockpit.<region>.scw.cloud with the Logs API URL that you can find in the Scaleway console, under Cockpit > Data sources tab > Custom data sources section

    • <namespace> with a namespace name to organize your rules (e.g., infrastructure, production)

      The <namespace> is a logical grouping for your rules within the API. It helps organize different sets of rules but is separate from the group name defined in your YAML file.

Step 3: Verify your alert rules

Retrieve your configured Loki alert rules to verify they were created successfully:

curl -X GET \
  -H "X-Auth-Token: $SCW_SECRET_KEY" \
  "https://<data-source-id>.logs.cockpit.<region>.scw.cloud/loki/api/v1/rules/<namespace>"
Still need help?

Create a support ticket
No Results