Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Comment on lines +266 to +270

Copilot AI Oct 31, 2025

Copy link

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.

# 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

Copilot AI Oct 31, 2025

Copy link

Choose a reason for hiding this comment

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

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.

Suggested change
self._prev_cost_totals[key] = cost
self._prev_cost_totals[key] = cost

Copilot uses AI. Check for mistakes.

labels_savings = (name,)
if self._check_cardinality(labels_savings):
self.savings_total.labels(primitive_name=name)._value.set(
cost_metrics.total_savings
)
# Increment counter by the difference since last update
prev_savings = self._prev_savings_totals.get(name, 0.0)
current_savings = cost_metrics.total_savings
if current_savings > prev_savings:
self.savings_total.labels(primitive_name=name).inc(
current_savings - prev_savings
)
self._prev_savings_totals[name] = current_savings

Copilot AI Oct 31, 2025

Copy link

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.

Suggested change
self._prev_savings_totals[name] = current_savings
self._prev_savings_totals[name] = current_savings

Copilot uses AI. Check for mistakes.

def export(self) -> bytes:
"""
Expand Down
Loading