Skip to content

Fix Prometheus Counter anti-pattern: remove _value access - #91

Merged
theinterneti merged 3 commits into
feature/keploy-frameworkfrom
copilot/sub-pr-26-one-more-time
Nov 14, 2025
Merged

theinterneti merged 3 commits into
feature/keploy-frameworkfrom
copilot/sub-pr-26-one-more-time

Conversation

Copilot AI commented Nov 13, 2025

Copy link
Copy Markdown
Contributor

Prometheus Counters were being incorrectly accessed via internal _value attribute to calculate deltas. This violates Prometheus design principles where counters should only be incremented.

Changes

  • Added tracking dictionaries to store last exported values for request, cost, and savings counters
  • Calculate deltas from tracked state instead of accessing Counter._value
  • Ensure counters are only incremented via .inc(), never set

Before

counter = self.request_total.labels(primitive_name=name, status="success")
current_value = getattr(counter, '_value', None)
if current_value is not None:
    increment = throughput_metrics.total_requests - current_value.get()
    if increment > 0:
        counter.inc(increment)

After

counter = self.request_total.labels(primitive_name=name, status="success")
last_value = self._last_request_totals.get(labels_success, 0.0)
increment = throughput_metrics.total_requests - last_value
if increment > 0:
    counter.inc(increment)
    self._last_request_totals[labels_success] = throughput_metrics.total_requests

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Replace direct access to Counter._value with internal tracking of last exported values. This ensures we follow Prometheus best practices by only using Counter.inc() and never accessing internal implementation details.

- Add tracking dictionaries for last exported values
- Calculate deltas from tracked values instead of Counter._value
- Update request_total, cost_total, and savings_total counters properly
- All 57 observability tests pass

Co-authored-by: theinterneti <169108167+theinterneti@users.noreply.github.com>
Copilot AI changed the title [WIP] Update workflow enhancements for observability and API testing Fix Prometheus Counter anti-pattern: remove _value access Nov 13, 2025
Copilot AI requested a review from theinterneti November 13, 2025 00:41
@theinterneti theinterneti added the help wanted Extra attention is needed label Nov 13, 2025
@theinterneti

Copy link
Copy Markdown
Owner

This PR is relevant to the framework-first strategy but has significant merge conflicts that require manual resolution.

Resolved conflicts in prometheus_exporter.py by accepting the cleaner
implementation from feature/keploy-framework that uses singular variable
names (_last_request_total vs _last_request_totals) and more explicit
temporary variables (current_total, last_total).
@theinterneti
theinterneti marked this pull request as ready for review November 14, 2025 01:48
Copilot AI review requested due to automatic review settings November 14, 2025 01:48

Copilot AI 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.

Copilot wasn't able to review any files in this pull request.

@theinterneti

Copy link
Copy Markdown
Owner

Merge conflicts resolved

Merged feature/keploy-framework into this branch and resolved conflicts in prometheus_exporter.py.

Resolution Strategy:

  • Accepted the incoming implementation from feature/keploy-framework
  • Uses cleaner variable naming (_last_request_total vs _last_request_totals)
  • More explicit temporary variables (current_total, last_total) for better readability

The PR is now ready for review and should be mergeable.

@theinterneti
theinterneti merged commit 4a3f72a into feature/keploy-framework Nov 14, 2025
@theinterneti
theinterneti deleted the copilot/sub-pr-26-one-more-time branch November 16, 2025 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

help wanted Extra attention is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants