Skip to content

Commit

Permalink
Merge pull request #5 from lpsinger/black
Browse files Browse the repository at this point in the history
Add black formatter
  • Loading branch information
dakota002 committed Jan 31, 2024
2 parents 8c23ea8 + 8776979 commit b8c5e64
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 88
extend-ignore = E203, E704
30 changes: 30 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.defaultFormatter": "ms-python.black-formatter"
}
25 changes: 17 additions & 8 deletions gcn_monitor/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
18 changes: 9 additions & 9 deletions gcn_monitor/kafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
32 changes: 16 additions & 16 deletions gcn_monitor/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
118 changes: 117 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit b8c5e64

Please sign in to comment.