Use the ruleup command in the Managed Service for Prometheus promtool to validate, upload, and manage PrometheusRule configuration files from a local directory.
Applicable to:
-
Users who want to manage alerting rules with Infrastructure as Code (IaC).
-
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" | bash -
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: The data source type. Onlyaliyun_prometheusis supported. -
instance: The ID of the Managed Service for Prometheus instance. -
regionId: The Alibaba Cloud region ID, such ascn-hangzhouorcn-beijing.
Rule level mapping
The
labels.severityfield in a rule maps to thelevelfield of a Managed Service for Prometheus alerting rule:-
warning→warning -
critical→critical -
info→info -
Other values →
warning(default)
-
Notes
-
Security
-
Do not enter sensitive information such as AccessKey IDs or AccessKey secrets directly in the command line.
-
Use environment variables or configuration files to manage credentials.
-
Ensure that rule files do not contain sensitive data.
-
-
Data source configuration
-
Upload and delete operations affect all data sources that are configured in the rule file.
-
Verify that the data source configuration is correct before you proceed.
-
-
Rule overwriting
-
Uploading overwrites rule groups that have the same name in the target data source.
-
Deleting 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 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 succeeded. -
1: The operation failed due to a validation, upload, or deletion error.
-
Best practices
Upload rules
-
Validate the rule file: Validate the rule file to check its integrity and PromQL expression syntax.
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 validation, upload the rules 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 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
Delete specified rule groups from a Managed Service for Prometheus instance. The same validation performed for uploads also runs 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 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
Display basic information about all rule files in a directory, including the file type, rule group count, and data source count.
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
-
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 development, staging, and production.
-
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 and delete operations can affect multiple data sources. Review the configuration carefully before you proceed.
-
Validate operations in a staging environment before you run them in production.
-
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 all available 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. |