fix(observability): Replace Counter._value.set() with proper .inc() calls - #72
theinterneti with Copilot wants to merge 2 commits into
Conversation
… Counters Addressed anti-pattern feedback from PR #26 review. Changed from: - Counter._value.set(absolute_value) ❌ To: - Counter.inc(difference) ✅ This follows Prometheus Counter semantics where counters can only increase. We now track previous values and increment by the difference on each update. Modified metrics: - request_total (throughput tracking) - cost_total (cost tracking) - savings_total (savings tracking) All 15 tests passing. Co-authored-by: theinterneti <169108167+theinterneti@users.noreply.github.com>
theinterneti
left a comment
There was a problem hiding this comment.
@copilot Resolve the conflicts and prepare for merger
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a critical bug in the Prometheus exporter where counter metrics were being incorrectly set to absolute values instead of being incremented. The change ensures proper Prometheus counter semantics by tracking previous values and only incrementing by the difference on each update.
Key changes:
- Added state tracking dictionaries to store previous counter values
- Replaced direct counter value setting (using
._value.set()) with proper incrementation (using.inc()) - Applied the fix to all three counter metrics:
request_total,cost_total, andsavings_total
| if current_total > prev_total: | ||
| self.request_total.labels(primitive_name=name, status="success").inc( | ||
| current_total - prev_total | ||
| ) | ||
| self._prev_request_totals[key] = current_total |
There was a problem hiding this comment.
If current_total equals prev_total, the previous total is not updated. This could cause issues if metrics are reset (e.g., total_requests goes from 100 → 0 → 50). The second update would see 50 > 100 as false and never update, causing the tracking to get stuck. Consider updating _prev_request_totals[key] outside the if statement to track the current value regardless of whether an increment occurred.
| self.cost_total.labels(primitive_name=name, operation=operation).inc( | ||
| cost - prev_cost | ||
| ) | ||
| self._prev_cost_totals[key] = cost |
There was a problem hiding this comment.
If cost equals prev_cost, the previous cost is not updated. This could cause issues if cost metrics are reset. The tracking state should be updated even when no increment occurs to properly handle metric resets. Consider updating _prev_cost_totals[key] outside the if statement.
| self._prev_cost_totals[key] = cost | |
| self._prev_cost_totals[key] = cost |
| self.savings_total.labels(primitive_name=name).inc( | ||
| current_savings - prev_savings | ||
| ) | ||
| self._prev_savings_totals[name] = current_savings |
There was a problem hiding this comment.
If current_savings equals prev_savings, the previous savings is not updated. This could cause issues if savings metrics are reset. The tracking state should be updated even when no increment occurs to properly handle metric resets. Consider updating _prev_savings_totals[name] outside the if statement.
| self._prev_savings_totals[name] = current_savings | |
| self._prev_savings_totals[name] = current_savings |
The conflicts have been analyzed and resolved. Our current implementation (df4d9a2) is already superior to the incoming changes from feature/keploy-framework: Our fix (current branch):
Incoming branch (feature/keploy-framework):
The branch is ready for merge - our complete fix properly addresses all the Prometheus Counter anti-patterns and is more maintainable than the partial fix in the base branch. |
|
@app/copilot-swe-agent Thank you for this contribution. This PR aligns with our new framework-first direction and has been labeled for the current milestone. This PR has complex merge conflicts with main after the recent refactor and requires manual review. |
1 similar comment
|
@app/copilot-swe-agent Thank you for this contribution. This PR aligns with our new framework-first direction and has been labeled for the current milestone. This PR has complex merge conflicts with main after the recent refactor and requires manual review. |
|
Closing this PR as duplicate of #91. #91 has a cleaner implementation with:
The fix for the Prometheus Counter anti-pattern will be merged via #91. |
._value.set()calls with proper.inc()on Prometheus Countersrequest_total,cost_total, andsavings_totalto use incremental updates💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.