Skip to content

feat(flags): push coverage stats for the flag definitions cache - #96662

Merged
trunk-io[bot] merged 1 commit into
masterfrom
feat/flags-definitions-cache-stats
Sep 11, 2026
Merged

trunk-io[bot] merged 1 commit into
masterfrom
feat/flags-definitions-cache-stats

Conversation

@JamesPatrickGill

Copy link
Copy Markdown
Member

Problem

Nobody can see how much of the flag definitions cache is populated without running a management command and reading its stdout.

  • The coverage series does not exist. posthog_hypercache_coverage_percent{cache_name="flag_definitions"} returns nothing, while cache_name="flags" returns a series.
  • The hourly definitions refresh task never calls get_cache_stats. The sibling flags task does.
  • The flag definitions cache cutover needs this number to check that a backfill reached every team.

Changes

  • Grafana can now chart coverage, entry count, and expiry-tracked count for the flag definitions cache, refreshed hourly.
  • The task calls the generic get_cache_stats with FLAG_DEFINITIONS_HYPERCACHE_MANAGEMENT_CONFIG, which already carries cache_name="flag_definitions".
  • The scan follows the cache's configured Redis, so it measures the dedicated cluster where that alias is set.
  • The completion log gains the four fields the flags task already logs. That part is mechanical.
  • Cost: one hourly SCAN plus a sampled MEMORY USAGE pass. The flags cache runs the identical pass on the same cluster 20 minutes earlier, at :15 against this task's :35.

Note

The coverage percent divides cached entries by every team, and only teams that have had a flag are cached. The number therefore reads low. The arithmetic is right and the panel is easy to misread. Presentation is a separate change.

How did you test this code?

  • Extended test_refreshes_cache in products/feature_flags/backend/test/test_flag_definitions_cache_tasks.py to assert the task calls get_cache_stats with the definitions config.
  • The regression it catches: passing the flags config instead. That labels the series flags and double-counts the flags cache under a name that looks right.
  • No existing test covered the flags task's equivalent push, so there was no closer test to extend.
  • Not checked: the series itself. Confirm after deploy by querying posthog_hypercache_coverage_percent{cache_name="flag_definitions"}.

Automatic notifications

  • Publish to changelog?

Docs update

None. No user-facing behavior, API, or config changes.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

  • Claude Code (Opus 5) wrote the change from a written brief and a subplan.
  • Skills invoked: /writing-tests, /writing-code-comments, /writing-pr-descriptions, review-code --fix, simplify.
  • review-code --fix and simplify ran nine agents and applied no fixes. Two suggestions were declined: a zero-arg get_cache_stats() wrapper in local_evaluation.py, which the spec ruled out, and an import alias rename, where the current spelling matches three other cache modules.
  • Follow-up, not introduced here: get_cache_stats swallows exceptions and returns an error dict, so all five callers log "Completed" on a failed scan and drop the error string.
  • No duplicate: gh pr list --state open --search "flag definitions cache stats coverage" found nothing covering this.
  • Patch coverage: the extended test covers both changed production lines.
  • Public artifact: the work drew on an internal plan. Nothing from it reached the diff or this description.

The hourly definitions refresh task never called get_cache_stats, so
posthog_hypercache_coverage_percent{cache_name="flag_definitions"} did not
exist and the backfill-coverage question could only be answered from a
management command's stdout. Call the generic with the definitions config,
mirroring the flags task.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@JamesPatrickGill JamesPatrickGill self-assigned this Sep 8, 2026
@trunk-io

trunk-io Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

😎 Merged successfully - details.

@posthog

posthog Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

🦔 PostHog Review reviewed this pull request

Nothing worth raising this time, so here's a calming picture instead:

A panda relaxing and waving

@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

🤖 CI report

⚠️ Trunk lane — backend Python lane

This PR is assigned to the backend Python lane. It runs backend Python tests and may merge in parallel with PRs in other lanes.

@posthog posthog Bot removed the reviewhog label Sep 8, 2026
@JamesPatrickGill
JamesPatrickGill marked this pull request as ready for review September 8, 2026 14:39
@pr-assigner-resolver-posthog
pr-assigner-resolver-posthog Bot requested a review from a team September 8, 2026 14:40
@posthog-project-board-bot posthog-project-board-bot Bot moved this to In Review in Feature Flags Sep 8, 2026
@greptile-apps

greptile-apps Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
### Issue 1
products/feature_flags/backend/tasks.py:495
**Comment narrates the code**

This comment only restates that the next call scans and pushes cache statistics. The repository requires comments to explain non-obvious rationale rather than narrate the code, so this requirement must be satisfied before merging.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "feat(flags): push coverage stats for the..." | Re-trigger Greptile

successful_gauge.set(counts.successful)
failed_gauge.set(counts.failed)

# Scan after refresh for metrics (pushes to Pushgateway via get_cache_stats)

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.

P2 Comment narrates the code

This comment only restates that the next call scans and pushes cache statistics. The repository requires comments to explain non-obvious rationale rather than narrate the code, so this requirement must be satisfied before merging.

Context Used: AGENTS.MD (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: products/feature_flags/backend/tasks.py
Line: 495

Comment:
**Comment narrates the code**

This comment only restates that the next call scans and pushes cache statistics. The repository requires comments to explain non-obvious rationale rather than narrate the code, so this requirement must be satisfied before merging.

**Context Used:** AGENTS.MD ([source](https://github.com/posthog/posthog/blob/master/AGENTS.MD))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@trunk-io

trunk-io Bot commented Sep 8, 2026

Copy link
Copy Markdown

Static Badge   Static Badge   Static Badge

View Full Report ↗︎ ⋅ Docs

@haacked haacked 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.

LGTM!

@github-project-automation github-project-automation Bot moved this from In Review to Approved in Feature Flags Sep 8, 2026
@JamesPatrickGill

Copy link
Copy Markdown
Member Author

/trunk merge

@trunk-io
trunk-io Bot merged commit d7340e4 into master Sep 11, 2026
392 of 399 checks passed
@trunk-io
trunk-io Bot deleted the feat/flags-definitions-cache-stats branch September 11, 2026 13:35
@github-project-automation github-project-automation Bot moved this from Approved to Done in Feature Flags Sep 11, 2026
@deployment-status-posthog

deployment-status-posthog Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

Deploy status

Environment Status Deployed At Workflow
dev ✅ Deployed 2026-09-11 14:03 UTC Run
prod-us ✅ Deployed 2026-09-11 14:16 UTC Run
prod-eu ✅ Deployed 2026-09-11 14:20 UTC Run

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

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants