Skip to content

Commit

Permalink
switch to ruff (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertodonato committed Dec 28, 2023
1 parent 54cad96 commit cb110cb
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 59 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Lint
run: |
tox -e lint
tox run -e lint
check:
runs-on: ubuntu-latest
Expand All @@ -45,7 +45,7 @@ jobs:
- name: Check
run: |
tox -e check
tox run -e check
test:
runs-on: ubuntu-latest
Expand All @@ -54,6 +54,7 @@ jobs:
python-version:
- "3.10"
- "3.11"
- "3.12"
steps:
- name: Repository checkout
uses: actions/checkout@v3
Expand Down
28 changes: 9 additions & 19 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ keywords = [
]
license = {file = "LICENSE.txt"}
maintainers = [
{name = "Alberto Donato", email = "alberto.donato@gmail.com"},
{name = "Alberto Donato", email = "alberto.donato@gmail.com"},
]
authors = [
{name = "Alberto Donato", email = "alberto.donato@gmail.com"},
{name = "Alberto Donato", email = "alberto.donato@gmail.com"},
]
requires-python = ">=3.10"
classifiers = [
Expand Down Expand Up @@ -71,28 +71,18 @@ version = {attr = "query_exporter.__version__"}
[tool.setuptools.packages.find]
include = ["query_exporter*"]


[tool.setuptools.package-data]
query_exporter = ["schemas/*"]
query_exporter = ["py.typed", "schemas/*"]

[tool.black]
[tool.ruff]
line-length = 79

[tool.isort]
combine_as_imports = true
force_grid_wrap = 2
force_sort_within_sections = true
from_first = false
include_trailing_comma = true
multi_line_output = 3
order_by_type = false
profile = "black"
use_parentheses = true
[tool.ruff.lint]
select = ["I", "RUF", "UP"]

[tool.flake8]
ignore = ["E203", "E501", "W503"]
max-line-length = 80
select = ["C", "E", "F", "W", "B", "B950"]
[tool.ruff.lint.isort]
combine-as-imports = true
force-sort-within-sections = true

[tool.pytest.ini_options]
asyncio_mode = "auto"
Expand Down
6 changes: 3 additions & 3 deletions query_exporter/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from pathlib import Path
import re
from typing import (
Any,
IO,
Any,
)
from urllib.parse import (
quote_plus,
Expand All @@ -27,13 +27,13 @@
import yaml

from .db import (
create_db_engine,
DATABASE_LABEL,
DataBaseError,
InvalidQueryParameters,
InvalidQuerySchedule,
Query,
QueryMetric,
create_db_engine,
)

# metric for counting database errors
Expand Down Expand Up @@ -423,7 +423,7 @@ def _get_parameters_sets(parameters: ParametersConfig) -> list[dict[str, Any]]:


def _get_parameters_matrix(
parameters: dict[str, list[dict[str, Any]]]
parameters: dict[str, list[dict[str, Any]]],
) -> list[dict[str, Any]]:
"""Return parameters combinations from a matrix."""
# first, flatten dict like
Expand Down
4 changes: 2 additions & 2 deletions query_exporter/db.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Database wrapper."""

import asyncio
from collections.abc import Iterable
from itertools import chain
import logging
import sys
from time import perf_counter
from traceback import format_tb
from typing import (
Any,
cast,
Iterable,
NamedTuple,
cast,
)

from croniter import croniter
Expand Down
34 changes: 18 additions & 16 deletions query_exporter/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from croniter import croniter
from dateutil.tz import gettz
from prometheus_aioexporter import MetricsRegistry
from prometheus_aioexporter import MetricConfig, MetricsRegistry
from prometheus_client import Counter
from prometheus_client.metrics import MetricWrapperBase
from toolrack.aio import (
Expand All @@ -23,14 +23,14 @@
)

from .config import (
Config,
DB_ERRORS_METRIC_NAME,
QUERIES_METRIC_NAME,
QUERY_LATENCY_METRIC_NAME,
Config,
)
from .db import (
DataBase,
DATABASE_LABEL,
DataBase,
DataBaseConnectError,
DataBaseError,
Query,
Expand Down Expand Up @@ -94,14 +94,6 @@ def expire_series(
class QueryLoop:
"""Run database queries and collect metrics."""

_METRIC_METHODS = {
"counter": "inc",
"gauge": "set",
"histogram": "observe",
"summary": "observe",
"enum": "state",
}

def __init__(
self,
config: Config,
Expand Down Expand Up @@ -250,25 +242,35 @@ def _update_metric(
elif isinstance(value, Decimal):
value = float(value)
metric = self._config.metrics[name]
method = self._METRIC_METHODS[metric.type]
if metric.type == "counter" and not metric.config.get(
"increment", True
):
method = "set"
all_labels = {DATABASE_LABEL: database.config.name}
all_labels.update(database.config.labels)
if labels:
all_labels.update(labels)
labels_string = ",".join(
f'{label}="{value}"' for label, value in sorted(all_labels.items())
)
method = self._get_metric_method(metric)
self._logger.debug(
f'updating metric "{name}" {method} {value} {{{labels_string}}}'
)
metric = self._registry.get_metric(name, labels=all_labels)
self._update_metric_value(metric, method, value)
self._last_seen.update(name, all_labels, self._timestamp())

def _get_metric_method(self, metric: MetricConfig) -> str:
method = {
"counter": "inc",
"gauge": "set",
"histogram": "observe",
"summary": "observe",
"enum": "state",
}[metric.type]
if metric.type == "counter" and not metric.config.get(
"increment", True
):
method = "set"
return method

def _update_metric_value(
self, metric: MetricWrapperBase, method: str, value: Any
) -> None:
Expand Down
8 changes: 4 additions & 4 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import yaml

from query_exporter.config import (
_get_parameters_sets,
_resolve_dsn,
ConfigError,
DB_ERRORS_METRIC_NAME,
GLOBAL_METRICS,
load_config,
QUERIES_METRIC_NAME,
ConfigError,
_get_parameters_sets,
_resolve_dsn,
load_config,
)
from query_exporter.db import QueryMetric

Expand Down
2 changes: 1 addition & 1 deletion tests/db_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from query_exporter.config import DataBaseConfig
from query_exporter.db import (
create_db_engine,
DataBase,
DataBaseConnectError,
DataBaseError,
Expand All @@ -22,6 +21,7 @@
QueryMetric,
QueryResults,
QueryTimeoutExpired,
create_db_engine,
)


Expand Down
2 changes: 1 addition & 1 deletion tests/loop_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ async def test_run_query(self, query_tracker, query_loop, registry):
async def test_run_scheduled_query(
self,
mocker,
event_loop,
advance_time,
query_tracker,
registry,
config_data,
make_query_loop,
):
"""Queries are run and update metrics."""
event_loop = asyncio.get_running_loop()

def croniter(*args):
while True:
Expand Down
16 changes: 5 additions & 11 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,21 @@ commands =

[testenv:format]
deps =
black
isort
pyproject-fmt
ruff
tox-ini-fmt
commands =
isort {[base]lint_files}
black -q {[base]lint_files}
ruff format {[base]lint_files}
ruff check --fix {[base]lint_files}
- pyproject-fmt pyproject.toml
- tox-ini-fmt tox.ini

[testenv:lint]
deps =
black
flake8
flake8-pyproject
isort
pyproject-fmt
ruff
commands =
isort --check-only --diff {[base]lint_files}
black --check {[base]lint_files}
flake8 {[base]lint_files}
ruff check {[base]lint_files}
pyproject-fmt --check pyproject.toml

[testenv:run]
Expand Down

0 comments on commit cb110cb

Please sign in to comment.