Skip to content

chore: add cache hit/miss instrumentation to pagination total cache#2443

Merged
ctron merged 1 commit into
guacsec:mainfrom
ctron:chore/pagination-cache-instrumentation
Jul 3, 2026
Merged

chore: add cache hit/miss instrumentation to pagination total cache#2443
ctron merged 1 commit into
guacsec:mainfrom
ctron:chore/pagination-cache-instrumentation

Conversation

@ctron

@ctron ctron commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add #[instrument] to PaginationCache::cached_total with the cache key and a cache_hit field
  • Defaults to cache_hit = true; the init closure sets it to false when actually called (cache miss)
  • Helps diagnose whether the pagination total cache is working as expected

Test plan

  • Verify cache_hit field appears in traces for paginated list endpoints
  • Confirm first request shows cache_hit=false, subsequent identical requests show cache_hit=true

🤖 Generated with Claude Code

Summary by Sourcery

Enhancements:

  • Add tracing instrumentation to pagination total cache lookups, including a cache_hit field that distinguishes between cache hits and misses.

@sourcery-ai

sourcery-ai Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds tracing instrumentation to PaginationCache::cached_total to record cache hits vs misses for pagination total caching, wiring the cache key into the span and toggling a cache_hit field based on whether the cache initialization closure runs.

Sequence diagram for cache hit/miss instrumentation in PaginationCache::cached_total

sequenceDiagram
    actor Caller
    participant PaginationCache
    participant Cache
    participant tracing

    Caller->>PaginationCache: cached_total(key, compute)
    PaginationCache->>tracing: span with field cache_hit = true
    PaginationCache->>Cache: try_get_with(key, compute)
    alt cache miss (init closure runs)
        Cache->>tracing: Span::current().record(cache_hit, false)
        Cache->>PaginationCache: compute().await result
    else cache hit (value already cached)
        Cache-->>PaginationCache: cached result (cache_hit remains true)
    end
    PaginationCache-->>Caller: total_count
Loading

File-Level Changes

Change Details Files
Instrument pagination total cache lookups with a tracing span that records cache hits versus misses.
  • Import tracing::instrument for use with function-level instrumentation.
  • Add #[instrument(skip(self, compute), fields(cache_hit = true))] to PaginationCache::cached_total so calls are traced with cache_key and a default cache_hit=true field without logging self or the compute closure.
  • Inside the cache miss initialization closure, increment the misses counter and record cache_hit=false on the current span before invoking compute().
common/src/db/pagination_cache.rs

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

@ctron ctron requested review from helio-frota and rh-jfuller July 2, 2026 15:15
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ctron ctron force-pushed the chore/pagination-cache-instrumentation branch from 5b4b7e7 to ce4c8bb Compare July 2, 2026 15:16

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

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="common/src/db/pagination_cache.rs" line_range="72" />
<code_context>
         self.cache
             .try_get_with(key, async {
                 misses.add(1, &[]);
+                tracing::Span::current().record("cache_hit", false);
                 compute().await
             })
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Be cautious relying on `Span::current()` inside the `try_get_with` async closure.

Because `try_get_with` controls how the async closure is scheduled, it may run on a different task where the `#[instrument]` span is no longer current, making this `record` call a no-op or updating the wrong span. If you need reliable instrumentation, capture the span at the start of `cached_total` (e.g. `let span = tracing::Span::current();`) and call `span.record("cache_hit", false);` inside the closure instead of using `Span::current()` there.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

self.cache
.try_get_with(key, async {
misses.add(1, &[]);
tracing::Span::current().record("cache_hit", false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion (bug_risk): Be cautious relying on Span::current() inside the try_get_with async closure.

Because try_get_with controls how the async closure is scheduled, it may run on a different task where the #[instrument] span is no longer current, making this record call a no-op or updating the wrong span. If you need reliable instrumentation, capture the span at the start of cached_total (e.g. let span = tracing::Span::current();) and call span.record("cache_hit", false); inside the closure instead of using Span::current() there.

@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.41%. Comparing base (551117a) to head (ce4c8bb).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2443      +/-   ##
==========================================
+ Coverage   71.39%   71.41%   +0.01%     
==========================================
  Files         453      453              
  Lines       27254    27251       -3     
  Branches    27254    27251       -3     
==========================================
+ Hits        19459    19460       +1     
+ Misses       6666     6657       -9     
- Partials     1129     1134       +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ctron ctron added this pull request to the merge queue Jul 3, 2026
Merged via the queue into guacsec:main with commit cf5466c Jul 3, 2026
9 checks passed
@ctron ctron deleted the chore/pagination-cache-instrumentation branch July 3, 2026 09:28
@github-project-automation github-project-automation Bot moved this to Done in Trustify Jul 3, 2026
@ctron ctron added the backport release/0.5.z Backport (0.5.z) label Jul 3, 2026
@ctron

ctron commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

/backport

@trustify-ci-bot

Copy link
Copy Markdown

Successfully created backport PR for release/0.5.z:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport release/0.5.z Backport (0.5.z)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants