Skip to content

Commit

Permalink
Merge pull request #16 from imrehg/updates
Browse files Browse the repository at this point in the history
Various updates
  • Loading branch information
imrehg authored May 15, 2022
2 parents 1e370a2 + ff3f04c commit a1badc9
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 70 deletions.
17 changes: 6 additions & 11 deletions .github/workflows/python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,24 @@ jobs:
run: git checkout ${{ env.BRANCH }}

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install 'poetry>=1.2.0b1'
python -m pip install 'poetry>=1.2.0b1' 'nox'
poetry plugin add poetry-dynamic-versioning-plugin
poetry install
nox -l
- name: Run linting
run: |
poetry run isort --check src/ tests/
poetry run black --check src/ tests/
poetry run flake8 src/ tests/
poetry run mypy src
poetry run bandit src
nox -s lint
- name: Run unit tests
run: |
poetry run coverage run -m pytest
poetry run coverage report -m
nox -s test-${{ matrix.python-version }}
publish:
name: "Publish to PyPI"
Expand All @@ -63,7 +58,7 @@ jobs:
fetch-depth: 0

- name: Set up Python 3.10
uses: actions/setup-python@v1
uses: actions/setup-python@v3
with:
# Needs to be string for 3.10, otherwise it's wrongly parsed as 3.1
python-version: '3.10'
Expand Down
36 changes: 36 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import nox

source_folders = ["src/"]
project_folders = source_folders + ["tests/"]

# Default sessions
nox.options.sessions = ["lint", "test"]


@nox.session
def format(session):
"""Run code formatting."""
session.run("poetry", "install", external=True)
session.run("isort", *project_folders)
session.run("black", *project_folders)


@nox.session
def lint(session):
"""Run linting checks on the code."""
session.run("poetry", "install", "--with", "test", external=True)
session.run("isort", "--check", *project_folders)
session.run("black", "--check", *project_folders)
session.run("flake8", *project_folders)
session.run("mypy", *source_folders)
session.run("bandit", "-r", *source_folders)


@nox.session(python=["3.10", "3.11"])
def test(session):
"""Run tests on the code."""
session.run("poetry", "install", "--with", "test", external=True)
try:
session.run("coverage", "run", "-m", "pytest")
finally:
session.run("coverage", "report", "-m")
200 changes: 152 additions & 48 deletions poetry.lock

Large diffs are not rendered by default.

33 changes: 26 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ license = "Apache-2.0"
include = [
"LICENSE.txt",
]
classifiers = [
'Development Status :: 3 - Alpha',
'Framework :: Pytest',
'Intended Audience :: Information Technology',
'Intended Audience :: System Administrators',
'Natural Language :: English',
'Operating System :: OS Independent',
'Topic :: System :: Monitoring',
'Topic :: System :: Networking :: Monitoring',
'Typing :: Typed',
]

[tool.poetry.dependencies]
python = "^3.10"
Expand All @@ -20,7 +31,15 @@ dynaconf = "^3.1.8"
types-requests = "^2.27.25"
jsonrpcclient = "^4.0.2"

[tool.poetry.dev-dependencies]
[tool.poetry.group.dev.dependencies]
isort = "*"
black = "*"
nox = "*"

[tool.poetry.group.test]
optional = true

[tool.poetry.group.test.dependencies]
pytest = "*"
pytest-cov = "*"
requests-mock = "*"
Expand All @@ -30,6 +49,12 @@ flake8 = "*"
mypy = "*"
bandit = "*"

[tool.poetry.scripts]
linkhub_exporter = "linkhub_prometheus_exporter.exporter:main"

[tool.poetry.urls]
"Bug Tracker" = "https://github.com/imrehg/linkhub_prometheus_exporter/issues"

[tool.isort]
profile = "black"
src_paths = ["src", "tests"]
Expand All @@ -43,17 +68,11 @@ module = [
]
ignore_missing_imports = true

[tool.poetry.scripts]
linkhub_exporter = "linkhub_prometheus_exporter.exporter:main"

[tool.poetry-dynamic-versioning]
enable = true
vcs = "git"
style = "pep440"

[tool.poetry-dynamic-versioning.substitution]
files = ["*/__init__.py", "*/*/__init__.py"]

[build-system]
requires = ["poetry>=1.2.0b1", "poetry-dynamic-versioning-plugin"]
build-backend = "poetry.masonry.api"
11 changes: 9 additions & 2 deletions src/linkhub_prometheus_exporter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# Will be dynamically filled
__version__ = "0.0.0"
from importlib.metadata import PackageNotFoundError, version

try:
__version__ = version("linkhub_prometheus_exporter")
except PackageNotFoundError:
# This package is not installed.
__version__ = "unknown"
finally:
del PackageNotFoundError, version
2 changes: 1 addition & 1 deletion src/linkhub_prometheus_exporter/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Validator("POLLING_INTERVAL_SECONDS", is_type_of=int, default=5),
Validator("BOX_ADDRESS", is_type_of=str, default="192.168.1.1"),
Validator("EXPORTER_PORT", is_type_of=int, default=9877),
Validator("EXPORTER_ADDRESS", is_type_of=str, default="0.0.0.0"),
Validator("EXPORTER_ADDRESS", is_type_of=str, default="localhost"),
],
)

Expand Down
14 changes: 13 additions & 1 deletion src/linkhub_prometheus_exporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _box_api_request(self, method: str) -> dict[str, Any]:
)
raise RuntimeError(message)
case _:
assert False, "Impossible parsed response received."
raise AssertionError("Impossible parsed response received.")

def _read_network_info(self) -> None:
"""Requesting, parsing, and updating network info metrics."""
Expand Down Expand Up @@ -140,6 +140,13 @@ def fetch_metrics(self) -> None:
def main() -> None:
"""Main entry point for the exporter"""
logging.info("Linkhub Prometheus Exporter, version %s", __version__)
# Add exporter metadata to what's exported
exporter_info = Info("exporter_info", "Exporter information")
exporter_info.info(
{
"version": __version__,
}
)

try:
router_metrics = RouterMetrics(
Expand All @@ -152,6 +159,11 @@ def main() -> None:
logging.error("Missing REQUEST_KEY configuration.")
raise RuntimeError("Missing REQUEST_KEY configuration.") from exc

logging.info(
"Server starting on http://%s:%d",
settings.EXPORTER_ADDRESS,
settings.EXPORTER_PORT,
)
start_http_server(
port=settings.EXPORTER_PORT, addr=settings.EXPORTER_ADDRESS
)
Expand Down

0 comments on commit a1badc9

Please sign in to comment.