From 8776979d170e1ac842e1b74e4659922e151c2f74 Mon Sep 17 00:00:00 2001 From: Leo Singer Date: Wed, 31 Jan 2024 11:14:21 -0500 Subject: [PATCH] Add black formatter --- .flake8 | 3 + .github/workflows/build.yml | 30 +++++++++ .vscode/settings.json | 3 + gcn_monitor/cli.py | 25 +++++--- gcn_monitor/kafka.py | 18 +++--- gcn_monitor/metrics.py | 32 +++++----- poetry.lock | 118 +++++++++++++++++++++++++++++++++++- pyproject.toml | 1 + 8 files changed, 196 insertions(+), 34 deletions(-) create mode 100644 .flake8 create mode 100644 .vscode/settings.json diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..f295e07 --- /dev/null +++ b/.flake8 @@ -0,0 +1,3 @@ +[flake8] +max-line-length = 88 +extend-ignore = E203, E704 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 59abfb5..e2d4b17 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -54,3 +54,33 @@ jobs: push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + + python: + name: Python tests + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - uses: psf/black@stable + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.10' + + - name: Install Poetry + run: pip install poetry + + - name: Install package + run: poetry install + + - name: Run flake8 linter + run: poetry run flake8 + + # - name: Run unit tests + # run: poetry run pytest . --cov --cov-report=xml + + # - name: Upload coverage to codecov + # uses: codecov/codecov-action@v3 diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f43854b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "editor.defaultFormatter": "ms-python.black-formatter" +} diff --git a/gcn_monitor/cli.py b/gcn_monitor/cli.py index 1ba357a..1da27c1 100644 --- a/gcn_monitor/cli.py +++ b/gcn_monitor/cli.py @@ -21,16 +21,24 @@ def host_port(host_port_str): # Parse netloc like it is done for HTTP URLs. # This ensures that we will get the correct behavior for hostname:port # splitting even for IPv6 addresses. - return urllib.parse.urlparse(f'http://{host_port_str}') + return urllib.parse.urlparse(f"http://{host_port_str}") @click.command() @click.option( - '--prometheus', type=host_port, default=':8000', show_default=True, - help='Hostname and port to listen on for Prometheus metric reporting') + "--prometheus", + type=host_port, + default=":8000", + show_default=True, + help="Hostname and port to listen on for Prometheus metric reporting", +) @click.option( - '--loglevel', type=click.Choice(logging._levelToName.values()), - default='DEBUG', show_default=True, help='Log level') + "--loglevel", + type=click.Choice(logging._levelToName.values()), + default="DEBUG", + show_default=True, + help="Log level", +) def main(prometheus, loglevel): """Monitor connectivity of a Kafka client. @@ -46,8 +54,9 @@ def main(prometheus, loglevel): """ logging.basicConfig(level=loglevel) - prometheus_client.start_http_server(prometheus.port, - prometheus.hostname or '0.0.0.0') - log.info('Prometheus listening on %s', prometheus.netloc) + prometheus_client.start_http_server( + prometheus.port, prometheus.hostname or "0.0.0.0" + ) + log.info("Prometheus listening on %s", prometheus.netloc) kafka.run() diff --git a/gcn_monitor/kafka.py b/gcn_monitor/kafka.py index 3c464cc..ee2cd4d 100644 --- a/gcn_monitor/kafka.py +++ b/gcn_monitor/kafka.py @@ -18,26 +18,26 @@ def stats_cb(data): stats = json.loads(data) - for broker in stats['brokers'].values(): - metrics.broker_state.labels(broker['name']).state(broker['state']) + for broker in stats["brokers"].values(): + metrics.broker_state.labels(broker["name"]).state(broker["state"]) def run(): - log.info('Creating consumer') + log.info("Creating consumer") config = gcn_kafka.config_from_env() - config['stats_cb'] = stats_cb - config['statistics.interval.ms'] = 1e3 + config["stats_cb"] = stats_cb + config["statistics.interval.ms"] = 1e3 consumer = gcn_kafka.Consumer(config) - log.info('Subscribing') + log.info("Subscribing") topics = list(consumer.list_topics().topics.keys()) consumer.subscribe(topics) - log.info('Entering consume loop') + log.info("Entering consume loop") while True: for message in consumer.consume(timeout=1): topic = message.topic() if error := message.error(): - log.error('topic %s: got error %s', topic, error) + log.error("topic %s: got error %s", topic, error) else: - log.info('topic %s: got message', topic) + log.info("topic %s: got message", topic) diff --git a/gcn_monitor/metrics.py b/gcn_monitor/metrics.py index 7060c26..de3fbcf 100644 --- a/gcn_monitor/metrics.py +++ b/gcn_monitor/metrics.py @@ -9,23 +9,23 @@ import prometheus_client broker_state = prometheus_client.Enum( - 'state', - 'Kafka broker state (see https://github.com/confluentinc/librdkafka/blob/master/STATISTICS.md)', + "state", + "Kafka broker state (see https://github.com/confluentinc/librdkafka/blob/master/STATISTICS.md)", states=[ # from https://github.com/confluentinc/librdkafka/blob/v2.2.0/src/rdkafka_broker.c#L83 - 'INIT', - 'DOWN', - 'TRY_CONNECT', - 'CONNECT', - 'SSL_HANDSHAKE', - 'AUTH_LEGACY', - 'UP', - 'UPDATE', - 'APIVERSION_QUERY', - 'AUTH_HANDSHAKE', - 'AUTH_REQ', + "INIT", + "DOWN", + "TRY_CONNECT", + "CONNECT", + "SSL_HANDSHAKE", + "AUTH_LEGACY", + "UP", + "UPDATE", + "APIVERSION_QUERY", + "AUTH_HANDSHAKE", + "AUTH_REQ", ], - namespace='kafka', - subsystem='broker', - labelnames=['name'] + namespace="kafka", + subsystem="broker", + labelnames=["name"], ) diff --git a/poetry.lock b/poetry.lock index 844cfc4..ffe2242 100644 --- a/poetry.lock +++ b/poetry.lock @@ -14,6 +14,52 @@ files = [ [package.dependencies] cryptography = ">=3.2" +[[package]] +name = "black" +version = "24.1.1" +description = "The uncompromising code formatter." +optional = false +python-versions = ">=3.8" +files = [ + {file = "black-24.1.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:2588021038bd5ada078de606f2a804cadd0a3cc6a79cb3e9bb3a8bf581325a4c"}, + {file = "black-24.1.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:1a95915c98d6e32ca43809d46d932e2abc5f1f7d582ffbe65a5b4d1588af7445"}, + {file = "black-24.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2fa6a0e965779c8f2afb286f9ef798df770ba2b6cee063c650b96adec22c056a"}, + {file = "black-24.1.1-cp310-cp310-win_amd64.whl", hash = "sha256:5242ecd9e990aeb995b6d03dc3b2d112d4a78f2083e5a8e86d566340ae80fec4"}, + {file = "black-24.1.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:fc1ec9aa6f4d98d022101e015261c056ddebe3da6a8ccfc2c792cbe0349d48b7"}, + {file = "black-24.1.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:0269dfdea12442022e88043d2910429bed717b2d04523867a85dacce535916b8"}, + {file = "black-24.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b3d64db762eae4a5ce04b6e3dd745dcca0fb9560eb931a5be97472e38652a161"}, + {file = "black-24.1.1-cp311-cp311-win_amd64.whl", hash = "sha256:5d7b06ea8816cbd4becfe5f70accae953c53c0e53aa98730ceccb0395520ee5d"}, + {file = "black-24.1.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:e2c8dfa14677f90d976f68e0c923947ae68fa3961d61ee30976c388adc0b02c8"}, + {file = "black-24.1.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:a21725862d0e855ae05da1dd25e3825ed712eaaccef6b03017fe0853a01aa45e"}, + {file = "black-24.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:07204d078e25327aad9ed2c64790d681238686bce254c910de640c7cc4fc3aa6"}, + {file = "black-24.1.1-cp312-cp312-win_amd64.whl", hash = "sha256:a83fe522d9698d8f9a101b860b1ee154c1d25f8a82ceb807d319f085b2627c5b"}, + {file = "black-24.1.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:08b34e85170d368c37ca7bf81cf67ac863c9d1963b2c1780c39102187ec8dd62"}, + {file = "black-24.1.1-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:7258c27115c1e3b5de9ac6c4f9957e3ee2c02c0b39222a24dc7aa03ba0e986f5"}, + {file = "black-24.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:40657e1b78212d582a0edecafef133cf1dd02e6677f539b669db4746150d38f6"}, + {file = "black-24.1.1-cp38-cp38-win_amd64.whl", hash = "sha256:e298d588744efda02379521a19639ebcd314fba7a49be22136204d7ed1782717"}, + {file = "black-24.1.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:34afe9da5056aa123b8bfda1664bfe6fb4e9c6f311d8e4a6eb089da9a9173bf9"}, + {file = "black-24.1.1-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:854c06fb86fd854140f37fb24dbf10621f5dab9e3b0c29a690ba595e3d543024"}, + {file = "black-24.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:3897ae5a21ca132efa219c029cce5e6bfc9c3d34ed7e892113d199c0b1b444a2"}, + {file = "black-24.1.1-cp39-cp39-win_amd64.whl", hash = "sha256:ecba2a15dfb2d97105be74bbfe5128bc5e9fa8477d8c46766505c1dda5883aac"}, + {file = "black-24.1.1-py3-none-any.whl", hash = "sha256:5cdc2e2195212208fbcae579b931407c1fa9997584f0a415421748aeafff1168"}, + {file = "black-24.1.1.tar.gz", hash = "sha256:48b5760dcbfe5cf97fd4fba23946681f3a81514c6ab8a45b50da67ac8fbc6c7b"}, +] + +[package.dependencies] +click = ">=8.0.0" +mypy-extensions = ">=0.4.3" +packaging = ">=22.0" +pathspec = ">=0.9.0" +platformdirs = ">=2" +tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""} +typing-extensions = {version = ">=4.0.1", markers = "python_version < \"3.11\""} + +[package.extras] +colorama = ["colorama (>=0.4.3)"] +d = ["aiohttp (>=3.7.4)", "aiohttp (>=3.7.4,!=3.9.0)"] +jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"] +uvloop = ["uvloop (>=0.15.2)"] + [[package]] name = "certifi" version = "2023.7.22" @@ -359,6 +405,54 @@ files = [ {file = "mccabe-0.7.0.tar.gz", hash = "sha256:348e0240c33b60bbdf4e523192ef919f28cb2c3d7d5c7794f74009290f236325"}, ] +[[package]] +name = "mypy-extensions" +version = "1.0.0" +description = "Type system extensions for programs checked with the mypy type checker." +optional = false +python-versions = ">=3.5" +files = [ + {file = "mypy_extensions-1.0.0-py3-none-any.whl", hash = "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d"}, + {file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"}, +] + +[[package]] +name = "packaging" +version = "23.2" +description = "Core utilities for Python packages" +optional = false +python-versions = ">=3.7" +files = [ + {file = "packaging-23.2-py3-none-any.whl", hash = "sha256:8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7"}, + {file = "packaging-23.2.tar.gz", hash = "sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5"}, +] + +[[package]] +name = "pathspec" +version = "0.12.1" +description = "Utility library for gitignore style pattern matching of file paths." +optional = false +python-versions = ">=3.8" +files = [ + {file = "pathspec-0.12.1-py3-none-any.whl", hash = "sha256:a0d503e138a4c123b27490a4f7beda6a01c6f288df0e4a8b79c7eb0dc7b4cc08"}, + {file = "pathspec-0.12.1.tar.gz", hash = "sha256:a482d51503a1ab33b1c67a6c3813a26953dbdc71c31dacaef9a838c4e29f5712"}, +] + +[[package]] +name = "platformdirs" +version = "4.2.0" +description = "A small Python package for determining appropriate platform-specific dirs, e.g. a \"user data dir\"." +optional = false +python-versions = ">=3.8" +files = [ + {file = "platformdirs-4.2.0-py3-none-any.whl", hash = "sha256:0614df2a2f37e1a662acbd8e2b25b92ccf8632929bc6d43467e17fe89c75e068"}, + {file = "platformdirs-4.2.0.tar.gz", hash = "sha256:ef0cc731df711022c174543cb70a9b5bd22e5a9337c8624ef2c2ceb8ddad8768"}, +] + +[package.extras] +docs = ["furo (>=2023.9.10)", "proselint (>=0.13)", "sphinx (>=7.2.6)", "sphinx-autodoc-typehints (>=1.25.2)"] +test = ["appdirs (==1.4.4)", "covdefaults (>=2.3)", "pytest (>=7.4.3)", "pytest-cov (>=4.1)", "pytest-mock (>=3.12)"] + [[package]] name = "prometheus-client" version = "0.17.1" @@ -427,6 +521,28 @@ urllib3 = ">=1.21.1,<3" socks = ["PySocks (>=1.5.6,!=1.5.7)"] use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"] +[[package]] +name = "tomli" +version = "2.0.1" +description = "A lil' TOML parser" +optional = false +python-versions = ">=3.7" +files = [ + {file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"}, + {file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"}, +] + +[[package]] +name = "typing-extensions" +version = "4.9.0" +description = "Backported and Experimental Type Hints for Python 3.8+" +optional = false +python-versions = ">=3.8" +files = [ + {file = "typing_extensions-4.9.0-py3-none-any.whl", hash = "sha256:af72aea155e91adfc61c3ae9e0e342dbc0cba726d6cba4b6c72c1f34e47291cd"}, + {file = "typing_extensions-4.9.0.tar.gz", hash = "sha256:23478f88c37f27d76ac8aee6c905017a143b0b1b886c3c9f66bc2fd94f9f5783"}, +] + [[package]] name = "urllib3" version = "2.0.7" @@ -447,4 +563,4 @@ zstd = ["zstandard (>=0.18.0)"] [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "d23667937e9f3075bf960345299b4b7eae046f4eaab8c3260706739a44dc4682" +content-hash = "39a8e00914f7a87309c9483644f065fa6f13a253c5ee05aef5d7e6f723297e87" diff --git a/pyproject.toml b/pyproject.toml index 19d47d1..aac8382 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,6 +13,7 @@ python = "^3.9" prometheus-client = "^0.17.1" [tool.poetry.group.dev.dependencies] +black = "*" flake8 = "^6.1.0" [tool.poetry.scripts]