Skip to content

Commit

Permalink
Merge branch 'main' into sid/ci/image-build-push
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin5605 authored Oct 9, 2024
2 parents ea7dd90 + 1273cc4 commit 481eee9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
26 changes: 25 additions & 1 deletion pdm.lock

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

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ dependencies = [
"structlog-sentry==2.1.0",
"structlog==24.4.0",
"uvicorn[standard]==0.30.3",
"prometheus-client>=0.21.0",
"prometheus-fastapi-instrumentator>=7.0.0",
]
dynamic = ["version"]

Expand Down
13 changes: 13 additions & 0 deletions src/mainframe/endpoints/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
QueuePackageResponse,
)

from mainframe.metrics import packages_ingested, packages_in_queue, packages_fail, packages_success

router = APIRouter(tags=["package"])
logger: structlog.stdlib.BoundLogger = structlog.get_logger()

Expand Down Expand Up @@ -62,6 +64,8 @@ def submit_results(
log.error(
f"Scan {name}@{version} already in a FINISHED state", error_message=error.detail, tag="already_finished"
)
packages_fail.inc()
packages_in_queue.dec()
raise error

with session, session.begin():
Expand Down Expand Up @@ -104,6 +108,9 @@ def submit_results(
tag="scan_submitted",
)

packages_success.inc()
packages_in_queue.dec()


@router.get(
"/package",
Expand Down Expand Up @@ -226,6 +233,9 @@ def batch_queue_package(

session.add(scan)

packages_ingested.inc(len(packages))
packages_in_queue.inc(len(packages))


@router.post(
"/package",
Expand Down Expand Up @@ -294,4 +304,7 @@ def queue_package(
tag="package_added",
)

packages_ingested.inc()
packages_in_queue.inc()

return QueuePackageResponse(id=str(new_package.scan_id))
4 changes: 4 additions & 0 deletions src/mainframe/endpoints/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
ReportPackageBody,
)

from mainframe.metrics import packages_reported

logger: structlog.stdlib.BoundLogger = structlog.get_logger()


Expand Down Expand Up @@ -251,3 +253,5 @@ def report_package(
},
reported_by=auth.subject,
)

packages_reported.inc()
14 changes: 14 additions & 0 deletions src/mainframe/metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from prometheus_client import Counter, Gauge


packages_ingested = Counter("packages_ingested", "Total number of packages ingested")

packages_in_queue = Gauge(
"packages_in_queue",
"Packages that are currently waiting to be scanned. Includes queued and pending packages.",
)

packages_success = Counter("packages_success", "Number of packages scanned successfully")
packages_fail = Counter("packages_fail", "Number of packages that failed scanning")

packages_reported = Counter("packages_reported", "Number of packages reported")
4 changes: 4 additions & 0 deletions src/mainframe/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from mainframe.models.schemas import ServerMetadata
from mainframe.rules import Rules, fetch_rules

from prometheus_fastapi_instrumentator import Instrumentator

from . import __version__


Expand Down Expand Up @@ -72,6 +74,8 @@ async def lifespan(app_: FastAPI):
version=__version__,
)

Instrumentator().instrument(app).expose(app)

if GIT_SHA in ("development", "testing"):
app.dependency_overrides[validate_token] = validate_token_override

Expand Down

0 comments on commit 481eee9

Please sign in to comment.