Skip to content

Commit

Permalink
New zocalo.util.rabbitmq.RabbitMQAPI (#140)
Browse files Browse the repository at this point in the history
Provide a (not fully comprehensive) Python wrapper around the RabbitMQ HTTP
API. Can be used to read various metrics useful for monitoring/health checks,
and configuring exchanges, policies, queues, etc.

Adds explicit dependencies on pydantic and requests modules and a test
dependency on requests-mock.

Closes #136
  • Loading branch information
rjgildea authored Nov 3, 2021
1 parent 5f070ee commit d0ea600
Show file tree
Hide file tree
Showing 9 changed files with 1,120 additions and 38 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ repos:
hooks:
- id: mypy
files: 'src/.*\.py$'
additional_dependencies: [types-requests]
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Unreleased
places it back on a queue
* Use ``argparse`` for all command line tools and make use of ``workflows`` transport
argument injection. Minimum ``workflows`` version is now 2.14
* New ``zocalo.util.rabbitmq.RabbitMQAPI()`` providing a thin wrapper around the
RabbitMQ HTTP API

0.10.0 (2021-10-04)
-------------------
Expand Down
3 changes: 3 additions & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
PyYAML==6.0
graypy==2.1.0
marshmallow==3.14.0
pydantic
pytest-cov==3.0.0
pytest-mock
pytest==6.2.5
requests
requests_mock
setuptools==58.3.0
workflows==2.14
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ install_requires =
PyYAML
graypy>=1.0
marshmallow
requests
pydantic
setuptools
workflows>=2.14
packages = find:
Expand Down
22 changes: 9 additions & 13 deletions src/zocalo/cli/dlq_check.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import argparse
import json
import urllib

import workflows.transport

import zocalo.configuration
from zocalo.util.jmxstats import JMXAPI
from zocalo.util.rabbitmq import http_api_request
from zocalo.util.rabbitmq import RabbitMQAPI

#
# zocalo.dlq_check
Expand Down Expand Up @@ -50,16 +48,14 @@ def extract_queue_name(namestring):
def check_dlq_rabbitmq(
zc: zocalo.configuration.Configuration, namespace: str = None
) -> dict:
_api_request = http_api_request(zc, "/queues")
with urllib.request.urlopen(_api_request) as response:
reply = response.read()
queue_info = json.loads(reply)
dlq_info = {}
for q in queue_info:
if q["name"].startswith("dlq."):
if (namespace is None or q["vhost"] == namespace) and int(q["messages"]):
dlq_info[q["name"]] = int(q["messages"])
return dlq_info
rmq = RabbitMQAPI.from_zocalo_configuration(zc)
return {
q.name: q.messages
for q in rmq.queues()
if q.name.startswith("dlq.")
and (namespace is None or q.vhost == namespace)
and q.messages
}


def run() -> None:
Expand Down
10 changes: 3 additions & 7 deletions src/zocalo/cli/dlq_reinject.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import workflows.transport

import zocalo.configuration
from zocalo.util.rabbitmq import http_api_request
from zocalo.util.rabbitmq import RabbitMQAPI


def run() -> None:
Expand Down Expand Up @@ -130,12 +130,8 @@ def run() -> None:
header = dlqmsg["header"]
exchange = header.get("headers", {}).get("x-death", {})[0].get("exchange")
if exchange:
import urllib

_api_request = http_api_request(zc, "/queues")
with urllib.request.urlopen(_api_request) as response:
reply = response.read()
exchange_info = json.loads(reply)
rmqapi = RabbitMQAPI.from_zocalo_configuration(zc)
exchange_info = rmqapi.get("queues").json()
for exch in exchange_info:
if exch["name"] == exchange:
if exch["type"] == "fanout":
Expand Down
Loading

0 comments on commit d0ea600

Please sign in to comment.