Skip to content

Commit

Permalink
revert(schemas): use local schema validation for rules API (#18)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
hayk96 authored May 12, 2024
1 parent 7525559 commit 508b19c
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.2.2 / 2024-05-12

* [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

## 0.2.1 / 2024-05-07

* [CHANGE] Serve remote JS script through Cloudflare CDN. No API changes. #17
Expand Down
4 changes: 1 addition & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from fastapi.middleware.cors import CORSMiddleware
from src.utils.schemas import rule_schema_status
from src.utils.arguments import arg_parser
from src.api.v1.api import api_router
from src.utils.openapi import openapi
Expand All @@ -18,8 +17,7 @@
if not all([settings.check_prom_http_connection(prom_addr),
settings.check_reload_api_status(prom_addr),
settings.check_rules_directory(rule_path),
settings.check_fs_permissions(rule_path),
rule_schema_status]):
settings.check_fs_permissions(rule_path)]):
sys.exit()


Expand Down
9 changes: 6 additions & 3 deletions src/models/rule.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from jsonschema import validate, exceptions
from src.utils.arguments import arg_parser
from src.utils.schemas import rule_schema
from jsonschema import exceptions
from pydantic import BaseModel
from typing import Optional
import requests
import json
import yaml
import os

Expand Down Expand Up @@ -65,8 +65,11 @@ def validate_rule(self) -> tuple[bool, str, str]:
Validates the rule object provided by
the user against the required schema.
"""
schema_file = "src/schemas/rules.json"
with open(schema_file) as f:
schema = json.load(f)
try:
rule_schema.validate(self.data)
validate(instance=self.data, schema=schema)
except exceptions.ValidationError as e:
return False, "error", e.args[0]
return True, "success", "Prometheus rule is valid"
Expand Down
132 changes: 132 additions & 0 deletions src/schemas/rules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://json.schemastore.org/prometheus.rules.json",
"additionalProperties": false,
"definitions": {
"duration": {
"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": {
"type": "string",
"pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$"
},
"label_value": {
"type": "string"
},
"labels": {
"type": ["object", "null"],
"patternProperties": {
"^[a-zA-Z_][a-zA-Z0-9_]*$": {
"$ref": "#/definitions/label_value"
}
},
"additionalProperties": false
},
"tmpl_string": {
"description": "A string which is template-expanded before usage.",
"type": "string"
},
"annotations": {
"type": ["object", "null"],
"patternProperties": {
"^[a-zA-Z_][a-zA-Z0-9_]*$": {
"$ref": "#/definitions/tmpl_string"
}
},
"additionalProperties": false
},
"recording_rule": {
"type": "object",
"properties": {
"record": {
"description": "The name of the time series to output to. Must be a valid metric name.",
"type": "string"
},
"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"
},
"labels": {
"$ref": "#/definitions/labels",
"description": "Labels to add or overwrite before storing the result."
}
},
"required": ["record", "expr"],
"additionalProperties": false
},
"alerting_rule": {
"type": "object",
"properties": {
"alert": {
"description": "The name of the alert. Must be a valid metric name.",
"type": "string"
},
"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"
},
"for": {
"$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": {
"$ref": "#/definitions/labels",
"description": "Labels to add or overwrite for each alert."
},
"annotations": {
"$ref": "#/definitions/annotations",
"description": "Annotations to add to each alert."
}
},
"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"]
}
2 changes: 1 addition & 1 deletion src/utils/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def openapi(app: FastAPI):
"providing additional features and addressing its limitations. "
"Running as a sidecar alongside the Prometheus server enables "
"users to extend the capabilities of the API.",
version="0.2.1",
version="0.2.2",
contact={
"name": "Hayk Davtyan",
"url": "https://hayk96.github.io",
Expand Down
24 changes: 0 additions & 24 deletions src/utils/schemas.py

This file was deleted.

0 comments on commit 508b19c

Please sign in to comment.