All Products
Search
Document Center

Managed Service for Prometheus:Manage alerting rules as code using Managed Service for Prometheus rules

Last Updated:Dec 12, 2025

The ruleup command of the Managed Service for Prometheus promtool reads, validates, and manages PrometheusRule configuration files. It loads rule files from a local directory, validates them, and uploads them to a Managed Service for Prometheus instance.

This solution is applicable to:

  • Users who want to manage alerting rules with Infrastructure as Code (IaC).

  • This topic is intended for users who want to set up a CI pipeline to publish alerting rules.

Preparations

  1. Download the promtool tool

    curl -fsSL "https://o11y-addon-hangzhou-public.oss-cn-hangzhou.aliyuncs.com/share/promtool/install.sh" | bash
  2. Understand the rule file format

    PrometheusRule files must follow this format:

    # Extended fields, data source configuration (required)
    datasources:
      - type: aliyun_prometheus #
        instance: <prometheus-instance-id>
        regionId: <region-id>
    
    # Rule group
    groups:
      - name: <group-name>
        interval: <evaluation-interval>  # Optional, such as "30s"
        rules:
          - alert: <alert-name>
            expr: <promql-expression>
            for: <duration>  # Optional, such as "5m"
            labels:
              severity: <severity-level>  # such as warning, critical, or info
              # Other labels...
            annotations:
              summary: <summary-text>
              description: <description-text>
              # Other annotations...

    Data source configuration

    • type: Specifies the data source type. Only aliyun_prometheus is supported.

    • instance: Specifies the ID of the Managed Service for Prometheus instance.

    • regionId: Specifies the Alibaba Cloud region ID, such as cn-hangzhou or cn-beijing.

    Rule level mapping

    The labels.severity field in a rule is mapped to the level field of a Managed Service for Prometheus alerting rule. The mapping is as follows:

    • warningwarning

    • criticalcritical

    • infoinfo

    • Other values → warning (default)

Notes

  1. Security

    • Do not enter sensitive information, such as an AccessKey ID or AccessKey secret, directly in the command line.

    • Use environment variables or configuration files to manage credentials.

    • Ensure that rule files do not contain sensitive information.

  2. Data source configuration

    • Upload and delete operations affect all data sources that are configured in the rule file.

    • Ensure that the data source configuration is correct to prevent operational errors.

  3. Rule overwriting

    • An upload operation overwrites rule groups that have the same name in the target data source.

    • A delete operation removes all rules in the specified rule groups.

  4. Network requirements

    • Upload and delete operations require access to Alibaba Cloud APIs.

    • Ensure that you have stable network connectivity and the required access permissions.

  5. Concurrent operations

    • Do not upload to and delete from the same rule group at the same time.

    • After an operation is complete, wait for a short period before you start the next operation.

  6. Exit codes

    • 0: The operation was successful.

    • 1: The operation failed. This can be caused by a validation failure, upload failure, or deletion failure.

Best practices

Upload rules

  1. Validate the rule file: Validate the rule file to check its integrity and the syntax of the Prometheus Query Language (PromQL) expressions.

    promtool ruleup --dir /path/to/rules --check

    Sample output (validation successful):

    Found 1 rule file(s) in directory: /path/to/rules
    
    File: /path/to/rules/prometheus-rules.yml
    Type: prometheus
    Validation: OK

    Sample output (validation failed):

    Found 1 rule file(s) in directory: /path/to/rules
    
    File: /path/to/rules/prometheus-rules.yml
    Type: prometheus
    Validation errors:
      - group "system_resources": rule "HighCPUUsage": invalid PromQL expression: syntax error
      - group "application_services": rule "HighHTTPErrorRate": missing 'for' duration

    If the validation fails, the command exits with an exit code of 1.

  2. Upload rules: After the rules are validated, upload them to Managed Service for Prometheus.

    promtool ruleup --dir /path/to/rules --upload --access-key-id YOUR_ACCESS_KEY_ID --access-key-secret YOUR_ACCESS_KEY_SECRET --user-id YOUR_USER_ID

    Interactive confirmation: The command displays the upload configuration and prompts you for confirmation:

    === Upload Configuration ===
    User ID: 123456789
    Rule groups to upload: 5
      [1] system_resources (4 rules)
      [2] application_services (4 rules)
      [3] database (3 rules)
      [4] kubernetes (3 rules)
      [5] business_metrics (2 rules)
    Total rules to upload: 16
    Datasources: 2
      [1] Region: cn-hangzhou, Instance: rw-dfaa8e87409285b4b422b3c862ee
      [2] Region: cn-beijing, Instance: rw-abc123456789
    Workspace: will be automatically retrieved from instance
    
    Do you want to proceed with the upload? (yes/no): 

    Enter yes or y to continue. Any other input cancels the upload.

    Sample upload result:

    === Upload Result ===
    Total uploads: 10 (rule groups × datasources)
    Success: 10, Failed: 0
    
    Datasource [1]: Region=cn-hangzhou, Instance=rw-dfaa8e87409285b4b422b3c862ee
      Group: system_resources, Request ID: 12345678-1234-1234-1234-123456789abc
        Uploaded rules: 4
          - HighCPUUsage (ID: rule-001, Status: enabled, Level: warning)
          - HighMemoryUsage (ID: rule-002, Status: enabled, Level: warning)
          - HighDiskUsage (ID: rule-003, Status: enabled, Level: critical)
          - HighDiskIO (ID: rule-004, Status: enabled, Level: warning)
    
    Upload completed! 10 datasource(s) succeeded.

Delete rules from Managed Service for Prometheus

You can delete the specified rule groups from a Managed Service for Prometheus instance. The same validation that is performed for an upload is also performed before deletion.

promtool ruleup \
  --dir /path/to/rules \
  --delete \
  --access-key-id YOUR_ACCESS_KEY_ID \
  --access-key-secret YOUR_ACCESS_KEY_SECRET \
  --user-id YOUR_USER_ID

Interactive confirmation: The command displays the deletion configuration and prompts you for confirmation.

=== Delete Configuration ===
User ID: 123456789
Rule groups to delete: 5
  [1] system_resources
  [2] application_services
  [3] database
  [4] kubernetes
  [5] business_metrics
Datasources: 2
  [1] Region: cn-hangzhou, Instance: rw-dfaa8e87409285b4b422b3c862ee
  [2] Region: cn-beijing, Instance: rw-abc123456789

WARNING: This will delete all rules in the specified groups from the datasources!
Do you want to proceed with the deletion? (yes/no): 

Sample deletion result:

=== Delete Result ===
Total deletions: 10 (rule groups × datasources)
Success: 10, Failed: 0

Datasource [1]: Region=cn-hangzhou, Instance=rw-dfaa8e87409285b4b422b3c862ee
  Group: system_resources deleted, Request ID: 12345678-1234-1234-1234-123456789abc
  All rules in group deleted successfully

Deletion completed! 10 deletion(s) succeeded.

View basic information about rule files

You can display basic information about all rule files in a specified directory. This information includes the file type, the number of rule groups, and the number of data sources.

promtool ruleup --dir /path/to/rules

Sample output:

Found 2 rule file(s) in directory: /path/to/rules

File: /path/to/rules/prometheus-rules.yml
Type: prometheus
  PrometheusRule: 5 group(s), 2 datasource(s)
  Total rules: 15

File: /path/to/rules/alertmanager.yml
Type: alertmanager
  AlertManagerRule: 3 receiver(s)
  Route configured: yes

Complete workflow example

  1. Validate rules before you upload them.

  2. Use versioning:

    • Place rule files under a version control system, such as Git.

    • Back up the existing configuration before you modify rules.

  3. Manage by environment:

    • Use different rule file directories for different environments, such as developer, staging, and production environments.

    • Use different data source configurations to distinguish between environments.

  4. Follow rule naming conventions:

    • Use meaningful names for rule groups, such as system_resources and application_services.

    • Use alert names that clearly describe the alert, such as HighCPUUsage and ServiceInstanceDown.

  5. Configure data sources:

    • Ensure that the instance and regionId values in the data source configuration are accurate.

    • Verify that the instance ID exists and is accessible.

  6. Confirm before batch operations:

    • Upload or delete operations can affect multiple data sources. Carefully check the configuration information before you proceed.

    • Before you perform an operation in a production environment, validate it in a staging environment.

Complete workflow example

# 1. View basic information about the rule file
promtool ruleup --dir ./example/rule

# 2. Validate the rule file
promtool ruleup --dir ./example/rule --check

# 3. Upload the rules to Alibaba Cloud (interactive confirmation required)
promtool ruleup \
  --dir ./example/rule \
  --upload \
  --access-key-id $ALIYUN_ACCESS_KEY_ID \
  --access-key-secret $ALIYUN_ACCESS_KEY_SECRET \
  --user-id $ALIYUN_USER_ID

# 4. Delete the rules (interactive confirmation required)
promtool ruleup \
  --dir ./example/rule \
  --delete \
  --access-key-id $ALIYUN_ACCESS_KEY_ID \
  --access-key-secret $ALIYUN_ACCESS_KEY_SECRET \
  --user-id $ALIYUN_USER_ID
Environment variables
export ALIYUN_ACCESS_KEY_ID="your-access-key-id"
export ALIYUN_ACCESS_KEY_SECRET="your-access-key-secret"
export ALIYUN_USER_ID="your-user-id"

Common errors

No rule files found in directory

  • Cause: No rule files were found in the specified directory.

  • Solution: Check whether the directory path is correct. Make sure the directory contains valid YAML rule files.

No PrometheusRule file found

  • Cause: No files of the PrometheusRule type were found in the directory.

  • Solution: Make sure at least one file contains a PrometheusRule configuration.

No rule groups found in PrometheusRule

  • Cause: No rule groups are configured in the PrometheusRule file.

  • Solution: Add at least one groups configuration to the rule file.

No datasources configured in PrometheusRule

  • Cause: No data sources are configured in the rule file.

  • Solution: Add a datasources configuration to the top of the rule file.

Datasource [N] missing instance field

  • Cause: The instance field is missing or its value is empty in the data source configuration.

  • Solution: Configure a valid instance value for each data source.

Datasource [N] missing regionId field

  • Cause: The regionId field is missing or its value is empty in the data source configuration.

  • Solution: Configure a valid regionId value for each data source.

Rule validation failed

  • Cause: Rule validation failed. This can be caused by a PromQL syntax error or a missing required field.

  • Solution: Use the --check parameter to view detailed validation error messages. Fix the errors and retry.

--access-key-id is required when --upload is set

  • Cause: An AccessKey ID was not provided when --upload was used.

  • Solution: Add the --access-key-id parameter.

--user-id is required when --upload is set

  • Cause: A user ID was not provided when --upload was used.

  • Solution: Add the --user-id parameter.

References

ruleup command help

Run promtool ruleup --help to view the command help. The following table describes the parameters.

Parameter

Type

Default value

Description

--dir

string

""

The path of the directory that contains the YAML rule files.

--check

bool

false

Checks whether the configuration is valid. If an error is found, the command exits with a non-zero status code.

--upload

bool

false

Uploads PrometheusRule to a Managed Service for Prometheus instance.

--delete

bool

false

Deletes PrometheusRule from a Managed Service for Prometheus instance.

--access-key-id

string

-

The AccessKey ID of your Alibaba Cloud account. This parameter is required for uploads or deletions.

--access-key-secret

string

-

The AccessKey secret of your Alibaba Cloud account. This parameter is required for uploads or deletions.

--user-id

string

-

The ID of your Alibaba Cloud account. This parameter is required for uploads or deletions.