query-exporter 5.2.0__tar.gz → 5.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (20) hide show
  1. {query_exporter-5.2.0/query_exporter.egg-info → query_exporter-5.2.2}/PKG-INFO +4 -3
  2. {query_exporter-5.2.0 → query_exporter-5.2.2}/pyproject.toml +5 -4
  3. {query_exporter-5.2.0 → query_exporter-5.2.2}/query_exporter/__init__.py +1 -1
  4. {query_exporter-5.2.0 → query_exporter-5.2.2}/query_exporter/config.py +3 -1
  5. {query_exporter-5.2.0 → query_exporter-5.2.2}/query_exporter/db.py +68 -39
  6. {query_exporter-5.2.0 → query_exporter-5.2.2}/query_exporter/executor.py +14 -7
  7. {query_exporter-5.2.0 → query_exporter-5.2.2}/query_exporter/yaml.py +3 -3
  8. {query_exporter-5.2.0 → query_exporter-5.2.2/query_exporter.egg-info}/PKG-INFO +4 -3
  9. {query_exporter-5.2.0 → query_exporter-5.2.2}/LICENSE.txt +0 -0
  10. {query_exporter-5.2.0 → query_exporter-5.2.2}/README.rst +0 -0
  11. {query_exporter-5.2.0 → query_exporter-5.2.2}/query_exporter/builtin_metrics.py +0 -0
  12. {query_exporter-5.2.0 → query_exporter-5.2.2}/query_exporter/main.py +0 -0
  13. {query_exporter-5.2.0 → query_exporter-5.2.2}/query_exporter/py.typed +0 -0
  14. {query_exporter-5.2.0 → query_exporter-5.2.2}/query_exporter/schema.py +0 -0
  15. {query_exporter-5.2.0 → query_exporter-5.2.2}/query_exporter.egg-info/SOURCES.txt +0 -0
  16. {query_exporter-5.2.0 → query_exporter-5.2.2}/query_exporter.egg-info/dependency_links.txt +0 -0
  17. {query_exporter-5.2.0 → query_exporter-5.2.2}/query_exporter.egg-info/entry_points.txt +0 -0
  18. {query_exporter-5.2.0 → query_exporter-5.2.2}/query_exporter.egg-info/requires.txt +0 -0
  19. {query_exporter-5.2.0 → query_exporter-5.2.2}/query_exporter.egg-info/top_level.txt +0 -0
  20. {query_exporter-5.2.0 → query_exporter-5.2.2}/setup.cfg +0 -0
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: query-exporter
3
- Version: 5.2.0
3
+ Version: 5.2.2
4
4
  Summary: Export Prometheus metrics generated from SQL queries
5
- Author-email: Alberto Donato <alberto@albertodonato.net>
6
- Maintainer-email: Alberto Donato <alberto@albertodonato.net>
5
+ Author-email: Alberto Donato <oss@albertodonato.net>
6
+ Maintainer-email: Alberto Donato <oss@albertodonato.net>
7
7
  License-Expression: GPL-3.0-or-later
8
8
  Project-URL: Issue Tracker, https://github.com/albertodonato/query-exporter/issues
9
9
  Project-URL: Release Notes, https://github.com/albertodonato/query-exporter/blob/main/CHANGES.rst
@@ -20,6 +20,7 @@ Classifier: Programming Language :: Python :: 3 :: Only
20
20
  Classifier: Programming Language :: Python :: 3.12
21
21
  Classifier: Programming Language :: Python :: 3.13
22
22
  Classifier: Programming Language :: Python :: 3.14
23
+ Classifier: Programming Language :: Python :: 3.15
23
24
  Classifier: Topic :: Database
24
25
  Classifier: Topic :: System :: Monitoring
25
26
  Classifier: Topic :: Utilities
@@ -17,10 +17,10 @@ keywords = [
17
17
  license = "GPL-3.0-OR-later"
18
18
  license-files = [ "LICENSE.txt" ]
19
19
  maintainers = [
20
- { name = "Alberto Donato", email = "alberto@albertodonato.net" },
20
+ { name = "Alberto Donato", email = "oss@albertodonato.net" },
21
21
  ]
22
22
  authors = [
23
- { name = "Alberto Donato", email = "alberto@albertodonato.net" },
23
+ { name = "Alberto Donato", email = "oss@albertodonato.net" },
24
24
  ]
25
25
  requires-python = ">=3.12"
26
26
  classifiers = [
@@ -32,6 +32,7 @@ classifiers = [
32
32
  "Programming Language :: Python :: 3.12",
33
33
  "Programming Language :: Python :: 3.13",
34
34
  "Programming Language :: Python :: 3.14",
35
+ "Programming Language :: Python :: 3.15",
35
36
  "Topic :: Database",
36
37
  "Topic :: System :: Monitoring",
37
38
  "Topic :: Utilities",
@@ -107,7 +108,7 @@ dynamic.version = { attr = "query_exporter.__version__" }
107
108
 
108
109
  [tool.ruff]
109
110
  line-length = 79
110
- lint.select = [ "F", "I", "RUF", "UP" ]
111
+ lint.select = [ "ANN", "F", "I", "RUF", "UP" ]
111
112
  lint.isort.combine-as-imports = true
112
113
  lint.isort.force-sort-within-sections = true
113
114
 
@@ -134,8 +135,8 @@ ini_options.asyncio_mode = "auto"
134
135
  [tool.coverage]
135
136
  run.source = [ "query_exporter" ]
136
137
  report.fail_under = 100.0
137
- report.show_missing = true
138
138
  report.skip_covered = true
139
+ report.show_missing = true
139
140
 
140
141
  [tool.tox.env.check]
141
142
  dependency_groups = [ "dev", "tests", "types" ]
@@ -1,3 +1,3 @@
1
1
  """Export Prometheus metrics generated from SQL queries."""
2
2
 
3
- __version__ = "5.2.0"
3
+ __version__ = "5.2.2"
@@ -24,7 +24,9 @@ class ConfigError(Exception):
24
24
  Optionally, a list of dicts can be provided with error details.
25
25
  """
26
26
 
27
- def __init__(self, message: str, details: Sequence[dict[str, str]] = ()):
27
+ def __init__(
28
+ self, message: str, details: Sequence[dict[str, str]] = ()
29
+ ) -> None:
28
30
  super().__init__(message)
29
31
  self.details = details
30
32
 
@@ -2,8 +2,8 @@
2
2
 
3
3
  import asyncio
4
4
  from collections.abc import (
5
+ Generator,
5
6
  Iterable,
6
- Iterator,
7
7
  Sequence,
8
8
  )
9
9
  from concurrent.futures import ThreadPoolExecutor
@@ -34,7 +34,10 @@ from sqlalchemy.engine import (
34
34
  CursorResult,
35
35
  Engine,
36
36
  )
37
- from sqlalchemy.engine.interfaces import DBAPIConnection, DBAPICursor
37
+ from sqlalchemy.engine.interfaces import (
38
+ DBAPIConnection,
39
+ DBAPICursor,
40
+ )
38
41
  from sqlalchemy.exc import (
39
42
  ArgumentError,
40
43
  NoSuchModuleError,
@@ -151,14 +154,13 @@ class QueryResults(NamedTuple):
151
154
  latency: float | None = None
152
155
 
153
156
  @classmethod
154
- def from_result(cls, result: CursorResult[Any]) -> Self:
157
+ def from_result(cls, result: CursorResult[Any], latency: float) -> Self:
155
158
  """Return a QueryResults from results for a query."""
156
159
  timestamp = time()
157
160
  keys: list[str] = []
158
161
  rows: Sequence[Sequence[Any]] = []
159
162
  if result.returns_rows:
160
163
  keys, rows = list(result.keys()), result.all()
161
- latency = result.connection.info.get("query_latency", None)
162
164
  return cls(keys, rows, timestamp=timestamp, latency=latency)
163
165
 
164
166
 
@@ -267,21 +269,35 @@ class QueryExecution:
267
269
 
268
270
 
269
271
  class ConnectionTracker:
270
- """Track the active connection for a query so it can be invalidated on timeout."""
272
+ """Track the connection running a query so it can be dropped on timeout."""
271
273
 
272
274
  def __init__(self) -> None:
273
275
  self._lock = Lock()
274
276
  self._conn: Connection | None = None
275
- self._invalidated = False
276
277
 
277
- def set_conn(self, conn: Connection) -> None:
278
+ @contextmanager
279
+ def track(self, conn: Connection) -> Generator[None]:
280
+ """Track `conn` for the duration of the statement.
281
+
282
+ The connection is untracked before it's returned to the pool, so that a
283
+ late invalidation can't drop a connection another query has since
284
+ checked out.
285
+ """
278
286
  with self._lock:
279
287
  self._conn = conn
280
- self._invalidated = False
288
+ try:
289
+ yield
290
+ finally:
291
+ with self._lock:
292
+ self._conn = None
281
293
 
282
294
  def invalidate(self) -> None:
295
+ """Drop the connection running the statement, if any.
296
+
297
+ This performs I/O and, depending on the driver, can block until the
298
+ statement completes on its own, so it must never run on the event loop.
299
+ """
283
300
  with self._lock:
284
- self._invalidated = True
285
301
  if self._conn is not None:
286
302
  self._conn.invalidate()
287
303
 
@@ -310,9 +326,17 @@ class Database:
310
326
  max_workers=max_workers,
311
327
  thread_name_prefix=f"[Database-{self.name}]",
312
328
  )
329
+ # Timed out queries are dropped from a separate pool: the query
330
+ # executor is by definition saturated by the queries being dropped. At
331
+ # most max_workers queries can be in flight, so the same size is enough.
332
+ self._timeout_executor = ThreadPoolExecutor(
333
+ max_workers=max_workers,
334
+ thread_name_prefix=f"[Database-{self.name}-timeout]",
335
+ )
313
336
 
314
337
  def close(self) -> None:
315
338
  self._executor.shutdown(wait=True, cancel_futures=True)
339
+ self._timeout_executor.shutdown(wait=False, cancel_futures=True)
316
340
  self._engine.dispose()
317
341
 
318
342
  async def execute(self, query_execution: QueryExecution) -> MetricResults:
@@ -358,9 +382,38 @@ class Database:
358
382
  asyncio.shield(future), timeout=timeout
359
383
  )
360
384
  except TimeoutError:
361
- tracker.invalidate()
385
+ self._on_timeout(future, tracker)
362
386
  raise
363
387
 
388
+ def _on_timeout(
389
+ self,
390
+ future: asyncio.Future[QueryResults],
391
+ tracker: ConnectionTracker,
392
+ ) -> None:
393
+ """Drop the connection of a query that timed out.
394
+
395
+ The result is deliberately not awaited: dropping the connection can
396
+ block for as long as the query itself, and the timeout must be reported
397
+ right away.
398
+ """
399
+ self._timeout_executor.submit(self._invalidate_sync, tracker)
400
+ # the shielded future outlives the timeout, consume its outcome so that
401
+ # asyncio doesn't report it as never retrieved
402
+ future.add_done_callback(self._discard_outcome)
403
+
404
+ def _invalidate_sync(self, tracker: ConnectionTracker) -> None:
405
+ try:
406
+ tracker.invalidate()
407
+ except Exception as error:
408
+ self.logger.warning(
409
+ "failed dropping connection for timed out query",
410
+ error=self._error_message(error),
411
+ )
412
+
413
+ def _discard_outcome(self, future: asyncio.Future[QueryResults]) -> None:
414
+ if not future.cancelled():
415
+ future.exception()
416
+
364
417
  def _setup_engine(self) -> Engine:
365
418
  engine = create_db_engine(self.config)
366
419
 
@@ -385,33 +438,8 @@ class Database:
385
438
  def on_close(
386
439
  dbapi_conn: DBAPIConnection, conn: ConnectionPoolEntry
387
440
  ) -> None:
388
- conn.info.clear()
389
441
  self.logger.debug("disconnected")
390
442
 
391
- @event.listens_for(engine, "before_cursor_execute")
392
- def before_cursor_execute(
393
- conn: ConnectionPoolEntry,
394
- cursor: Any,
395
- statement: str,
396
- parameters: Any,
397
- context: Any,
398
- executemany: bool,
399
- ) -> None:
400
- conn.info["query_start_time"] = perf_counter()
401
-
402
- @event.listens_for(engine, "after_cursor_execute")
403
- def after_cursor_execute(
404
- conn: ConnectionPoolEntry,
405
- cursor: Any,
406
- statement: str,
407
- parameters: Any,
408
- context: Any,
409
- executemany: bool,
410
- ) -> None:
411
- conn.info["query_latency"] = perf_counter() - conn.info.pop(
412
- "query_start_time"
413
- )
414
-
415
443
  return engine
416
444
 
417
445
  def _execute_sync(
@@ -421,11 +449,12 @@ class Database:
421
449
  tracker: ConnectionTracker,
422
450
  ) -> QueryResults:
423
451
  try:
424
- with self._engine.begin() as conn:
425
- tracker.set_conn(conn)
452
+ with self._engine.begin() as conn, tracker.track(conn):
426
453
  try:
454
+ start_time = perf_counter()
427
455
  result = conn.execute(statement, parameters)
428
- return QueryResults.from_result(result)
456
+ latency = perf_counter() - start_time
457
+ return QueryResults.from_result(result, latency=latency)
429
458
  except Exception as error:
430
459
  raise self._db_error(error, exc_class=DatabaseQueryError)
431
460
  except DatabaseQueryError:
@@ -467,7 +496,7 @@ class Database:
467
496
  @contextmanager
468
497
  def _dbapi_transaction(
469
498
  self, dbapi_conn: DBAPIConnection
470
- ) -> Iterator[DBAPICursor]:
499
+ ) -> Generator[DBAPICursor]:
471
500
  cursor = dbapi_conn.cursor()
472
501
  try:
473
502
  yield cursor
@@ -5,7 +5,7 @@ from collections import defaultdict
5
5
  from collections.abc import Mapping
6
6
  from datetime import datetime
7
7
  from itertools import chain
8
- from typing import Any, cast
8
+ from typing import SupportsFloat, cast
9
9
 
10
10
  from apscheduler.schedulers.asyncio import AsyncIOScheduler
11
11
  from apscheduler.triggers.cron import CronTrigger
@@ -45,7 +45,7 @@ class MetricsLastSeen:
45
45
 
46
46
  """
47
47
 
48
- def __init__(self, expirations: dict[str, int | None]):
48
+ def __init__(self, expirations: dict[str, int | None]) -> None:
49
49
  self._expirations = expirations
50
50
  self._last_seen: dict[str, dict[tuple[str, ...], float]] = defaultdict(
51
51
  dict
@@ -104,7 +104,7 @@ class QueryExecutor:
104
104
  config: Config,
105
105
  registry: MetricsRegistry,
106
106
  logger: structlog.stdlib.BoundLogger | None = None,
107
- ):
107
+ ) -> None:
108
108
  loop = asyncio.get_event_loop()
109
109
 
110
110
  self._config = config
@@ -276,14 +276,18 @@ class QueryExecutor:
276
276
  self,
277
277
  database: Database,
278
278
  name: str,
279
- value: Any,
279
+ value: SupportsFloat | str | None,
280
280
  labels: Mapping[str, str] | None = None,
281
281
  ) -> None:
282
282
  """Update value for a metric."""
283
+ metric_config = self._config.metrics[name]
283
284
  if value is None:
284
285
  # count queries might return NULL, treat it as zero
285
286
  value = 0.0
286
- metric_config = self._config.metrics[name]
287
+ elif metric_config.type != "enum":
288
+ # coerce numeric-like values (e.g. Decimal, str) to float, since
289
+ # not all prometheus_client metric methods do this internally
290
+ value = float(value)
287
291
  all_labels = {DATABASE_LABEL: database.name}
288
292
  all_labels.update(database.config.labels)
289
293
  if labels:
@@ -316,7 +320,10 @@ class QueryExecutor:
316
320
  return method
317
321
 
318
322
  def _update_metric_value(
319
- self, metric: MetricWrapperBase, method: str, value: Any
323
+ self,
324
+ metric: MetricWrapperBase,
325
+ method: str,
326
+ value: SupportsFloat | str,
320
327
  ) -> None:
321
328
  if metric._type == "counter" and method == "set":
322
329
  # counters can only be incremented, directly set the underlying value
@@ -328,7 +335,7 @@ class QueryExecutor:
328
335
  self,
329
336
  database: Database,
330
337
  name: BuiltinMetric,
331
- value: Any,
338
+ value: SupportsFloat | str | None,
332
339
  labels: Mapping[str, str] | None = None,
333
340
  ) -> None:
334
341
  """Update value for a builtin metric."""
@@ -8,7 +8,7 @@ from yaml.scanner import ScannerError
8
8
  __all__ = ["ScannerError"]
9
9
 
10
10
 
11
- def load_yaml(path: Path) -> Any:
11
+ def load_yaml(path: Path) -> Any: # noqa: ANN401
12
12
  """Load a YAML document from a file."""
13
13
 
14
14
  with path.open() as fd:
@@ -28,7 +28,7 @@ def _config_loader(path: Path) -> type[_ConfigLoader]:
28
28
  return ConfigLoaderWithPath
29
29
 
30
30
 
31
- def _tag_env(loader: _ConfigLoader, node: yaml.nodes.ScalarNode) -> Any:
31
+ def _tag_env(loader: _ConfigLoader, node: yaml.nodes.ScalarNode) -> Any: # noqa: ANN401
32
32
  env = loader.construct_scalar(node)
33
33
  value = os.getenv(env)
34
34
  if value is None:
@@ -53,7 +53,7 @@ def _tag_file(loader: _ConfigLoader, node: yaml.nodes.ScalarNode) -> str:
53
53
  return path.read_text().strip()
54
54
 
55
55
 
56
- def _tag_include(loader: _ConfigLoader, node: yaml.nodes.ScalarNode) -> Any:
56
+ def _tag_include(loader: _ConfigLoader, node: yaml.nodes.ScalarNode) -> Any: # noqa: ANN401
57
57
  path = loader.base_path / loader.construct_scalar(node)
58
58
  if not path.is_file():
59
59
  raise ScannerError(
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: query-exporter
3
- Version: 5.2.0
3
+ Version: 5.2.2
4
4
  Summary: Export Prometheus metrics generated from SQL queries
5
- Author-email: Alberto Donato <alberto@albertodonato.net>
6
- Maintainer-email: Alberto Donato <alberto@albertodonato.net>
5
+ Author-email: Alberto Donato <oss@albertodonato.net>
6
+ Maintainer-email: Alberto Donato <oss@albertodonato.net>
7
7
  License-Expression: GPL-3.0-or-later
8
8
  Project-URL: Issue Tracker, https://github.com/albertodonato/query-exporter/issues
9
9
  Project-URL: Release Notes, https://github.com/albertodonato/query-exporter/blob/main/CHANGES.rst
@@ -20,6 +20,7 @@ Classifier: Programming Language :: Python :: 3 :: Only
20
20
  Classifier: Programming Language :: Python :: 3.12
21
21
  Classifier: Programming Language :: Python :: 3.13
22
22
  Classifier: Programming Language :: Python :: 3.14
23
+ Classifier: Programming Language :: Python :: 3.15
23
24
  Classifier: Topic :: Database
24
25
  Classifier: Topic :: System :: Monitoring
25
26
  Classifier: Topic :: Utilities
File without changes