Skip to content

feat(serve): add lightweight Prometheus metrics endpoint - #48

Merged
rkinas merged 3 commits into
rkinas:mainfrom
marcinbrzezanski:main
Nov 2, 2026
Merged

rkinas merged 3 commits into
rkinas:mainfrom
marcinbrzezanski:main

Conversation

@marcinbrzezanski

@marcinbrzezanski marcinbrzezanski commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem

basal-serve exposes current load through /health, but has no native Prometheus metrics endpoint.

Change

  • Add GET /metrics using Prometheus text format, without additional dependencies.
  • Expose build information, running/queued requests, and state-cache statistics.
  • Collect metrics only during scrapes, with no per-request instrumentation or GPU synchronization.
  • Use non-blocking cache snapshots to avoid interfering with inference.

Validation

  • Extended the existing HTTP test to check running/queued metrics.
  • Ran before/after benchmarks on Windows with 1 and 4 concurrent clients, with and without scraping.
  • Observed approximately 0.4–1.1% lower throughput across runs, including those without scraping. No clear additional scrape-related overhead was observed.
  • No inference errors during benchmarking.

Summary by Sourcery

Add lightweight Prometheus monitoring for server load, build details, and state-cache performance.

New Features:

  • Add a Prometheus-compatible /metrics endpoint exposing build, request-load, and state-cache metrics.

Enhancements:

  • Collect metrics on demand without per-request instrumentation or GPU synchronization, while retaining the last available cache snapshot when the cache is busy.
  • Support non-blocking state-cache statistics snapshots.

Documentation:

  • Document the Prometheus endpoint, exported metrics, scrape configuration, and collection behavior.

Tests:

  • Extend HTTP tests to validate running and queued request metrics during request lifecycle changes.

@sourcery-ai

sourcery-ai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

The PR introduces a lightweight, dependency-free /metrics endpoint that collects request-load and state-cache data only when scraped, using nonblocking cache snapshots to minimize inference interference; documentation and HTTP coverage describe and validate the behavior.

Sequence diagram for Prometheus metrics scraping

sequenceDiagram
    participant Prometheus
    participant Metrics as /metrics
    participant Server
    participant Cache as StateCache

    Prometheus->>Metrics: GET /metrics
    Metrics->>Server: load()
    Server-->>Metrics: running and queued
    Metrics->>Cache: stats(blocking=False)
    alt snapshot available
        Cache-->>Metrics: cache statistics
        Metrics->>Metrics: update cached snapshot
    else cache lock busy
        Cache-->>Metrics: None
        Metrics->>Metrics: reuse last snapshot
    end
    Metrics-->>Prometheus: Prometheus text response
Loading

File-Level Changes

Change Details Files
Adds a dependency-free Prometheus text endpoint with scrape-time operational and build metrics.
  • Registers GET /metrics and returns Prometheus-compatible HELP, TYPE, and metric samples.
  • Exports build identity, running and queued request gauges, and state-cache gauges/counters.
  • Escapes metric label values and sets the Prometheus content type.
basal/server.py
docs/SERVING.md
Makes state-cache statistics safe for nonblocking metric collection.
  • Adds an optional nonblocking lock acquisition to cache.stats().
  • Reuses the last successful snapshot when the cache is busy and records its timestamp.
  • Keeps cache statistics CPU-only without per-request instrumentation or GPU synchronization.
basal/engine.py
basal/server.py
Validates request load metrics through the existing HTTP integration test.
  • Scrapes /metrics while one request is running and another is queued.
  • Checks the running and queued metric values in the response.
tests/test_http_status.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="tests/test_http_status.py" line_range="127" />
<code_context>
         await until(lambda: srv.in_flight == 2)
         health.append((await c.get("/health")).json())
+        metrics = await c.get("/metrics")
+        assert "basal_requests_running 1\n" in metrics.text and "basal_requests_queued 1\n" in metrics.text
         engine.go.set()
         codes = [(await t).status_code for t in (first, second)]
</code_context>
<issue_to_address>
**Hard-coded metrics pass the test**

When the `/metrics` endpoint always emits `basal_requests_running 1` and `basal_requests_queued 1`, the assertion in `test_health_counts_running_and_queued_requests` checks only the `/metrics` response while the expected values are exactly 1 and 1, so an endpoint that always returns those constants passes and broken load reporting goes undetected.

Check the metrics at multiple load states, including idle and running-only, and assert each response matches the current load.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread tests/test_http_status.py Resolved
@rkinas

rkinas commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Thank you, Marcin! Your metrics work is in basal 2.5. While this PR was open, the 2.5 branch got its own GET /metrics (router and multi-GPU serving), so we kept that endpoint as the base and merged in what yours had and it lacked: basal_build_info, the state cache's prefix hits and evictions, and your non-blocking read. You are credited in WHATS_NEW. The PR will close when 2.5 is published on main.

@rkinas
rkinas merged commit b3dc41a into rkinas:main Nov 2, 2026
1 check passed
rkinas added a commit that referenced this pull request Nov 2, 2026
rkinas added a commit that referenced this pull request Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants