Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 508b19c

Browse files
authored
revert(schemas): use local schema validation for rules API (#18)
* revert(schemas): use local schema validation for rules API Signed-off-by: hayk96 <hayko5999@gmail.com> * Update CHANGELOG.md Signed-off-by: hayk96 <hayko5999@gmail.com> --------- Signed-off-by: hayk96 <hayko5999@gmail.com>
1 parent 7525559 commit 508b19c

File tree

6 files changed

+144
-31
lines changed

6 files changed

+144
-31
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.2.2 / 2024-05-12
4+
5+
* [REVERT] Reverted schema validation mechanism of rules API. Use local schema validation instead of remote which was introduces in [v0.1.2](https://github.com/hayk96/prometheus-api/releases/tag/v0.1.2). #18
6+
37
## 0.2.1 / 2024-05-07
48

59
* [CHANGE] Serve remote JS script through Cloudflare CDN. No API changes. #17

main.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from fastapi.middleware.cors import CORSMiddleware
2-
from src.utils.schemas import rule_schema_status
32
from src.utils.arguments import arg_parser
43
from src.api.v1.api import api_router
54
from src.utils.openapi import openapi
@@ -18,8 +17,7 @@
1817
if not all([settings.check_prom_http_connection(prom_addr),
1918
settings.check_reload_api_status(prom_addr),
2019
settings.check_rules_directory(rule_path),
21-
settings.check_fs_permissions(rule_path),
22-
rule_schema_status]):
20+
settings.check_fs_permissions(rule_path)]):
2321
sys.exit()
2422

2523

src/models/rule.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
from jsonschema import validate, exceptions
12
from src.utils.arguments import arg_parser
2-
from src.utils.schemas import rule_schema
3-
from jsonschema import exceptions
43
from pydantic import BaseModel
54
from typing import Optional
65
import requests
6+
import json
77
import yaml
88
import os
99

@@ -65,8 +65,11 @@ def validate_rule(self) -> tuple[bool, str, str]:
6565
Validates the rule object provided by
6666
the user against the required schema.
6767
"""
68+
schema_file = "src/schemas/rules.json"
69+
with open(schema_file) as f:
70+
schema = json.load(f)
6871
try:
69-
rule_schema.validate(self.data)
72+
validate(instance=self.data, schema=schema)
7073
except exceptions.ValidationError as e:
7174
return False, "error", e.args[0]
7275
return True, "success", "Prometheus rule is valid"

src/schemas/rules.json

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "https://json.schemastore.org/prometheus.rules.json",
4+
"additionalProperties": false,
5+
"definitions": {
6+
"duration": {
7+
"type": ["string", "null"],
8+
"pattern": "^((([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?|0)$",
9+
"minLength": 1
10+
},
11+
"label_name": {
12+
"type": "string",
13+
"pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$"
14+
},
15+
"label_value": {
16+
"type": "string"
17+
},
18+
"labels": {
19+
"type": ["object", "null"],
20+
"patternProperties": {
21+
"^[a-zA-Z_][a-zA-Z0-9_]*$": {
22+
"$ref": "#/definitions/label_value"
23+
}
24+
},
25+
"additionalProperties": false
26+
},
27+
"tmpl_string": {
28+
"description": "A string which is template-expanded before usage.",
29+
"type": "string"
30+
},
31+
"annotations": {
32+
"type": ["object", "null"],
33+
"patternProperties": {
34+
"^[a-zA-Z_][a-zA-Z0-9_]*$": {
35+
"$ref": "#/definitions/tmpl_string"
36+
}
37+
},
38+
"additionalProperties": false
39+
},
40+
"recording_rule": {
41+
"type": "object",
42+
"properties": {
43+
"record": {
44+
"description": "The name of the time series to output to. Must be a valid metric name.",
45+
"type": "string"
46+
},
47+
"expr": {
48+
"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'.",
49+
"type": "string"
50+
},
51+
"labels": {
52+
"$ref": "#/definitions/labels",
53+
"description": "Labels to add or overwrite before storing the result."
54+
}
55+
},
56+
"required": ["record", "expr"],
57+
"additionalProperties": false
58+
},
59+
"alerting_rule": {
60+
"type": "object",
61+
"properties": {
62+
"alert": {
63+
"description": "The name of the alert. Must be a valid metric name.",
64+
"type": "string"
65+
},
66+
"expr": {
67+
"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.",
68+
"type": "string"
69+
},
70+
"for": {
71+
"$ref": "#/definitions/duration",
72+
"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."
73+
},
74+
"keep_firing_for": {
75+
"$ref": "#/definitions/duration",
76+
"description": "How long an alert will continue firing after the condition that triggered it has cleared."
77+
},
78+
"labels": {
79+
"$ref": "#/definitions/labels",
80+
"description": "Labels to add or overwrite for each alert."
81+
},
82+
"annotations": {
83+
"$ref": "#/definitions/annotations",
84+
"description": "Annotations to add to each alert."
85+
}
86+
},
87+
"required": ["alert", "expr"],
88+
"additionalProperties": false
89+
}
90+
},
91+
"description": "Prometheus rules file",
92+
"properties": {
93+
"groups": {
94+
"type": ["array", "null"],
95+
"items": {
96+
"type": "object",
97+
"properties": {
98+
"name": {
99+
"description": "The name of the group. Must be unique within a file.",
100+
"type": "string"
101+
},
102+
"interval": {
103+
"$ref": "#/definitions/duration",
104+
"description": "How often rules in the group are evaluated."
105+
},
106+
"limit": {
107+
"description": "Limit the number of alerts an alerting rule and series a recording rule can produce. 0 is no limit.",
108+
"type": ["integer", "null"],
109+
"default": 0
110+
},
111+
"rules": {
112+
"type": ["array", "null"],
113+
"items": {
114+
"oneOf": [
115+
{
116+
"$ref": "#/definitions/recording_rule"
117+
},
118+
{
119+
"$ref": "#/definitions/alerting_rule"
120+
}
121+
]
122+
}
123+
}
124+
},
125+
"required": ["name"],
126+
"additionalProperties": false
127+
}
128+
}
129+
},
130+
"title": "Prometheus Rules",
131+
"type": ["object", "null"]
132+
}

src/utils/openapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def openapi(app: FastAPI):
1616
"providing additional features and addressing its limitations. "
1717
"Running as a sidecar alongside the Prometheus server enables "
1818
"users to extend the capabilities of the API.",
19-
version="0.2.1",
19+
version="0.2.2",
2020
contact={
2121
"name": "Hayk Davtyan",
2222
"url": "https://hayk96.github.io",

src/utils/schemas.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)