Skip to content

Commit

Permalink
Add workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-v-k committed Nov 1, 2024
1 parent 016b3b9 commit 38c9170
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 1 deletion.
29 changes: 29 additions & 0 deletions .github/workflows/python-linters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Linters

on:
push:
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8.16']

steps:
- uses: actions/checkout@v3
- name: Set up PDM
uses: pdm-project/setup-pdm@v3
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: pdm sync -dG lint

- name: Run lint
run: pdm run ruff check

- name: Run mypy
run: pdm run mypy .
28 changes: 28 additions & 0 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Tests

on:
push:
pull_request:
branches: [ master ]

jobs:
test:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13']

steps:
- uses: actions/checkout@v3
- name: Set up PDM
uses: pdm-project/setup-pdm@v3
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: pdm sync -dG pytest

- name: Run Tests
run: pdm run pytest

55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,58 @@
Base definitions for metrics collection via prometheus client library.
Intended to be used in Huntflow fastapi-based services: ready-to use collectors to measure HTTP requests and responses.
Also provides universal decorator to observe timings of custom methods/functions.

# How to use

## How to collect metrics for FastAPI requests

```python
from contextlib import asynccontextmanager

from fastAPI import FastAPI

from huntflow_base_metrics import start_metrics, stop_metrics, add_middleware


# Service name (in most cases should be provided in `FACILITY_NAME` environment variable)
FACILITY_NAME = "my-service-name"
# Service instance name (should provided in `FACILITY_ID` environment variable)
FACILITY_ID = "qwerty"


@asynccontextmanager
async def lifespan(app: FastAPI):
await onstartup(app)
yield
await onshutdown(app)


async def onstartup(app: FastAPI):
# do some startup actions
pass

async def onshutdown(app: FastAPI):
# do some shutdown actions
stop_metrics()


def create_app()
app = FastAPI(lifespan=lifespan)

start_metrics(
FACILITY_NAME,
FACILITY_ID,
# Optional, only needed if metrics are collected from files.
# Also, it's mandatory if write_to_file is True
out_file_path=f"/app/metrics/{FACILITY_NAME}-{FACILITY_ID}.prom",
enabled=True,
write_to_file=True,
# interval in seconds to dump metrics to a file
file_update_interval=15,
)
add_middleware(app)
return app

```

# TODO: another use-cases and development notes
14 changes: 13 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@ dependencies = [
requires-python = ">=3.8"
readme = "README.md"
license = {text = "MIT"}
classifiers = [
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
]

[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"

[tool.pdm]
distribution = false
distribution = true

[tool.pdm.dev-dependencies]
lint = [
Expand Down
22 changes: 22 additions & 0 deletions src/huntflow_base_metrics/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from .base_metrics import (
apply_labels,
register_method_observe_histogram,
register_method_observe_gauge,
register_metric,
start_metrics,
stop_metrics,
)
from .fastapi_metrics import add_middleware
from .metrics_export import export_to_http_response


__all__ = [
"apply_labels",
"register_method_observe_histogram",
"register_method_observe_gauge",
"register_metric",
"start_metrics",
"stop_metrics",
"add_middleware",
"export_to_http_response",
]

0 comments on commit 38c9170

Please sign in to comment.