-
Notifications
You must be signed in to change notification settings - Fork 0
fix(observability): Replace Counter._value.set() with proper .inc() calls #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -74,6 +74,11 @@ def __init__( | |||||
| # Track label cardinality | ||||||
| self._label_combinations: set[tuple[str, ...]] = set() | ||||||
|
|
||||||
| # Track previous counter values for proper incrementing | ||||||
| self._prev_request_totals: dict[tuple[str, str], float] = {} | ||||||
| self._prev_cost_totals: dict[tuple[str, str], float] = {} | ||||||
| self._prev_savings_totals: dict[str, float] = {} | ||||||
|
|
||||||
| # Initialize Prometheus metrics | ||||||
| self._init_metrics() | ||||||
|
|
||||||
|
|
@@ -254,25 +259,40 @@ def update_metrics(self) -> None: | |||||
| ) | ||||||
|
|
||||||
| if self._check_cardinality(labels_success): | ||||||
| # Note: Counter can only increase, so we set to total | ||||||
| self.request_total.labels(primitive_name=name, status="success")._value.set( | ||||||
| throughput_metrics.total_requests | ||||||
| ) | ||||||
| # Increment counter by the difference since last update | ||||||
| key = (name, "success") | ||||||
| current_total = throughput_metrics.total_requests | ||||||
| prev_total = self._prev_request_totals.get(key, 0.0) | ||||||
| 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 | ||||||
|
|
||||||
| # Update cost metrics | ||||||
| for name, cost_metrics in collector._cost_metrics.items(): | ||||||
| for operation, cost in cost_metrics.cost_by_operation.items(): | ||||||
| labels_cost = (name, operation) | ||||||
| if self._check_cardinality(labels_cost): | ||||||
| self.cost_total.labels(primitive_name=name, operation=operation)._value.set( | ||||||
| cost | ||||||
| ) | ||||||
| # Increment counter by the difference since last update | ||||||
| key = (name, operation) | ||||||
| prev_cost = self._prev_cost_totals.get(key, 0.0) | ||||||
| if cost > prev_cost: | ||||||
| self.cost_total.labels(primitive_name=name, operation=operation).inc( | ||||||
| cost - prev_cost | ||||||
| ) | ||||||
| self._prev_cost_totals[key] = cost | ||||||
|
||||||
| self._prev_cost_totals[key] = cost | |
| self._prev_cost_totals[key] = cost |
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
current_totalequalsprev_total, the previous total is not updated. This could cause issues if metrics are reset (e.g.,total_requestsgoes 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.