From 566ce2bd4214a33b3af5de2f0ef59a6dadc60365 Mon Sep 17 00:00:00 2001 From: Renato Monteiro <45536168+monteiro-renato@users.noreply.github.com> Date: Fri, 18 Oct 2024 09:38:13 +0200 Subject: [PATCH] fix: update prometheus.rules.json with new schema and fix failing tests --- .../schemas/prometheus/prometheus.rules.json | 172 +++++++++++------- .../to-azure-prom-alerting-rule.test.ts | 12 +- ...o-azure-prom-rule-group-properties.test.ts | 7 +- 3 files changed, 117 insertions(+), 74 deletions(-) diff --git a/tools/az-prom-rules-converter/src/schemas/prometheus/prometheus.rules.json b/tools/az-prom-rules-converter/src/schemas/prometheus/prometheus.rules.json index bd66f44ef..3cd04005f 100644 --- a/tools/az-prom-rules-converter/src/schemas/prometheus/prometheus.rules.json +++ b/tools/az-prom-rules-converter/src/schemas/prometheus/prometheus.rules.json @@ -1,21 +1,14 @@ { "$schema": "http://json-schema.org/draft-07/schema#", - "title": "Prometheus Rules", - "description": "Prometheus rules file", - "type": "object", + "$id": "https://json.schemastore.org/prometheus.rules.json", "additionalProperties": false, - "properties": { - "groups": { - "type": "array", - "items": { - "$ref": "#/definitions/prometheus_rules_group" - } - } - }, "definitions": { "duration": { - "type": "string", - "pattern": "^([0-9]+y)?([0-9]+w)?([0-9]+d)?([0-9]+h)?([0-9]+m)?([0-9]+s)?([0-9]+ms)?$", + "type": [ + "string", + "null" + ], + "pattern": "^((([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?|0)$", "minLength": 1 }, "label_name": { @@ -26,8 +19,10 @@ "type": "string" }, "labels": { - "type": "object", - "description": "Labels.", + "type": [ + "object", + "null" + ], "patternProperties": { "^[a-zA-Z_][a-zA-Z0-9_]*$": { "$ref": "#/definitions/label_value" @@ -35,52 +30,22 @@ }, "additionalProperties": false }, - "template_string": { + "tmpl_string": { "description": "A string which is template-expanded before usage.", "type": "string" }, "annotations": { - "type": "object", + "type": [ + "object", + "null" + ], "patternProperties": { "^[a-zA-Z_][a-zA-Z0-9_]*$": { - "$ref": "#/definitions/template_string" + "$ref": "#/definitions/tmpl_string" } }, "additionalProperties": false }, - "prometheus_rules_group": { - "type": "object", - "properties": { - "name": { - "description": "The name of the group. Must be unique within a file.", - "type": "string" - }, - "interval": { - "description": "How often rules in the group are evaluated.", - "$ref": "#/definitions/duration" - }, - "limit": { - "description": "Limit the number of alerts an alerting rule and series a recording rule can produce. 0 is no limit.", - "type": "integer", - "default": 0 - }, - "rules": { - "type": "array", - "items": { - "oneOf": [ - { - "$ref": "#/definitions/recording_rule" - }, - { - "$ref": "#/definitions/alerting_rule" - } - ] - } - } - }, - "required": ["name"], - "additionalProperties": false - }, "recording_rule": { "type": "object", "properties": { @@ -90,14 +55,24 @@ }, "expr": { "description": "The PromQL expression to evaluate. Every evaluation cycle this is evaluated at the current time, and the result recorded as a new set of time series with the metric name as given by 'record'.", - "type": "string" + "anyOf": [ + { + "type": "string" + }, + { + "type": "integer" + } + ] }, "labels": { - "description": "Labels to add or overwrite before storing the result.", - "$ref": "#/definitions/labels" + "$ref": "#/definitions/labels", + "description": "Labels to add or overwrite before storing the result." } }, - "required": ["record", "expr"], + "required": [ + "record", + "expr" + ], "additionalProperties": false }, "alerting_rule": { @@ -109,23 +84,92 @@ }, "expr": { "description": "The PromQL expression to evaluate. Every evaluation cycle this is evaluated at the current time, and all resultant time series become pending/firing alerts.", - "type": "string" + "anyOf": [ + { + "type": "string" + }, + { + "type": "integer" + } + ] }, "for": { - "description": "Alerts are considered firing once they have been returned for this long. Alerts which have not yet fired for long enough are considered pending.", - "$ref": "#/definitions/duration" + "$ref": "#/definitions/duration", + "description": "Alerts are considered firing once they have been returned for this long. Alerts which have not yet fired for long enough are considered pending." + }, + "keep_firing_for": { + "$ref": "#/definitions/duration", + "description": "How long an alert will continue firing after the condition that triggered it has cleared." }, "labels": { - "description": "Labels to add or overwrite for each alert.", - "$ref": "#/definitions/labels" + "$ref": "#/definitions/labels", + "description": "Labels to add or overwrite for each alert." }, "annotations": { - "description": "Annotations to add to each alert.", - "$ref": "#/definitions/annotations" + "$ref": "#/definitions/annotations", + "description": "Annotations to add to each alert." } }, - "required": ["alert", "expr"], + "required": [ + "alert", + "expr" + ], "additionalProperties": false } - } + }, + "description": "Prometheus rules file", + "properties": { + "groups": { + "type": [ + "array", + "null" + ], + "items": { + "type": "object", + "properties": { + "name": { + "description": "The name of the group. Must be unique within a file.", + "type": "string" + }, + "interval": { + "$ref": "#/definitions/duration", + "description": "How often rules in the group are evaluated." + }, + "limit": { + "description": "Limit the number of alerts an alerting rule and series a recording rule can produce. 0 is no limit.", + "type": [ + "integer", + "null" + ], + "default": 0 + }, + "rules": { + "type": [ + "array", + "null" + ], + "items": { + "oneOf": [ + { + "$ref": "#/definitions/recording_rule" + }, + { + "$ref": "#/definitions/alerting_rule" + } + ] + } + } + }, + "required": [ + "name" + ], + "additionalProperties": false + } + } + }, + "title": "Prometheus Rules", + "type": [ + "object", + "null" + ] } \ No newline at end of file diff --git a/tools/az-prom-rules-converter/src/steps/to-azure/to-azure-prom-alerting-rule.test.ts b/tools/az-prom-rules-converter/src/steps/to-azure/to-azure-prom-alerting-rule.test.ts index b136da294..fae1bd50e 100644 --- a/tools/az-prom-rules-converter/src/steps/to-azure/to-azure-prom-alerting-rule.test.ts +++ b/tools/az-prom-rules-converter/src/steps/to-azure/to-azure-prom-alerting-rule.test.ts @@ -1,14 +1,14 @@ import { AlertingRule } from '../../types/prometheus-rules'; import toAzurePromAlertingRule from './to-azure-prom-alerting-rule' -import{test, expect} from '@jest/globals'; +import { test, expect } from '@jest/globals'; test('Empty source should add the default properties', () => { - const converted = toAzurePromAlertingRule(({} as AlertingRule), {actionGroupId: 'actionGroupId'}); + const converted = toAzurePromAlertingRule(({} as AlertingRule), { actionGroupId: 'actionGroupId' }); expect(converted.severity).toBe(3); expect(converted.resolveConfiguration).toEqual({ - autoResolve: true, - timeToResolve: "PT10M" + autoResolved: true, + timeToResolve: "PT10M" }); expect(converted.actions[0].actionGroupId).toBe("[parameters('actionGroupId')]"); }); @@ -18,6 +18,6 @@ test('Convert for to ISO 8601', () => { const rule = { for: '1h2m' } - const converted = toAzurePromAlertingRule((rule as AlertingRule), {actionGroupId: 'actionGroupId'}); + const converted = toAzurePromAlertingRule((rule as AlertingRule), { actionGroupId: 'actionGroupId' }); expect(converted.for).toBe("PT1H2M"); -}); \ No newline at end of file +}); diff --git a/tools/az-prom-rules-converter/src/steps/to-azure/to-azure-prom-rule-group-properties.test.ts b/tools/az-prom-rules-converter/src/steps/to-azure/to-azure-prom-rule-group-properties.test.ts index e8ad71db2..07ad2a5a0 100644 --- a/tools/az-prom-rules-converter/src/steps/to-azure/to-azure-prom-rule-group-properties.test.ts +++ b/tools/az-prom-rules-converter/src/steps/to-azure/to-azure-prom-rule-group-properties.test.ts @@ -1,5 +1,5 @@ import toAzurePromAlertingRuleProperties from './to-azure-prom-rule-group-properties' -import{test, expect} from '@jest/globals'; +import { test, expect } from '@jest/globals'; const params = { actionGroupId: 'actionGroupId', @@ -43,9 +43,8 @@ test('Distinguish between alerting and recording rule', () => { expect(converted.rules[1].alert).toBe("alertName"); expect(converted.rules[1].severity).toBe(3); expect(converted.rules[1].resolveConfiguration).toEqual({ - autoResolve: true, - timeToResolve: "PT10M" + autoResolved: true, + timeToResolve: "PT10M" }); expect(converted.rules[1].actions[0].actionGroupId).toBe("[parameters('actionGroupId')]"); }); -