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
Download the promtool tool
curl -fsSL "https://o11y-addon-hangzhou-public.oss-cn-hangzhou.aliyuncs.com/share/promtool/install.sh" | bashUnderstand 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. Onlyaliyun_prometheusis supported.instance: Specifies the ID of the Managed Service for Prometheus instance.regionId: Specifies the Alibaba Cloud region ID, such ascn-hangzhouorcn-beijing.
Rule level mapping
The
labels.severityfield in a rule is mapped to thelevelfield of a Managed Service for Prometheus alerting rule. The mapping is as follows:warning→warningcritical→criticalinfo→infoOther values →
warning(default)
Notes
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.
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.
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.
Network requirements
Upload and delete operations require access to Alibaba Cloud APIs.
Ensure that you have stable network connectivity and the required access permissions.
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.
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
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 --checkSample output (validation successful):
Found 1 rule file(s) in directory: /path/to/rules File: /path/to/rules/prometheus-rules.yml Type: prometheus Validation: OKSample 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' durationIf the validation fails, the command exits with an exit code of 1.
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_IDInteractive 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
yesoryto 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_IDInteractive 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/rulesSample 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: yesComplete workflow example
Validate rules before you upload them.
Use versioning:
Place rule files under a version control system, such as Git.
Back up the existing configuration before you modify rules.
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.
Follow rule naming conventions:
Use meaningful names for rule groups, such as
system_resourcesandapplication_services.Use alert names that clearly describe the alert, such as
HighCPUUsageandServiceInstanceDown.
Configure data sources:
Ensure that the
instanceandregionIdvalues in the data source configuration are accurate.Verify that the instance ID exists and is accessible.
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.
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
groupsconfiguration to the rule file.
No datasources configured in PrometheusRule
Cause: No data sources are configured in the rule file.
Solution: Add a
datasourcesconfiguration to the top of the rule file.
Datasource [N] missing instance field
Cause: The
instancefield is missing or its value is empty in the data source configuration.Solution: Configure a valid
instancevalue for each data source.
Datasource [N] missing regionId field
Cause: The
regionIdfield is missing or its value is empty in the data source configuration.Solution: Configure a valid
regionIdvalue 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
--checkparameter 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
--uploadwas used.Solution: Add the
--access-key-idparameter.
--user-id is required when --upload is set
Cause: A user ID was not provided when
--uploadwas used.Solution: Add the
--user-idparameter.
References
ruleup command help
Run promtool ruleup --help to view the command help. The following table describes the parameters.
Parameter | Type | Default value | Description |
| string |
| The path of the directory that contains the YAML rule files. |
| bool |
| Checks whether the configuration is valid. If an error is found, the command exits with a non-zero status code. |
| bool |
| Uploads PrometheusRule to a Managed Service for Prometheus instance. |
| bool |
| Deletes PrometheusRule from a Managed Service for Prometheus instance. |
| string | - | The AccessKey ID of your Alibaba Cloud account. This parameter is required for uploads or deletions. |
| string | - | The AccessKey secret of your Alibaba Cloud account. This parameter is required for uploads or deletions. |
| string | - | The ID of your Alibaba Cloud account. This parameter is required for uploads or deletions. |