Skip to content

Make DB metric expectations resilient to zero durations

What does this MR do and why?

In our MergeRequests::Mergeability::Logger specs we were asserting that both mergeability.expensive_operation.db_main_count.values and mergeability.expensive_operation.db_main_duration_s.values
must always be present. In reality, whenever an operation makes zero SQL calls (or the total duration rounds to 0), our implementation deliberately skips emitting the <.db_main_duration_s> key:

# observe_sql_counters in app/services/merge_requests/mergeability/logger.rb
result = value - start_db_counters.fetch(key, 0)
next if result == 0
observe("mergeability.#{name}.#{key}", result)

This guard makes the spec flaky, because sometimes db_main_duration_s.values is genuinely omitted when its value is zero.

Consideration for Logger Behavior (Future Enhancement): While removing next if result == 0 entirely in the logger would ensure all metric keys are always present, investigation showed this could add approximately 35 extra zero-valued keys per mergeability check, which might be considered excessive log noise.

A potential future enhancement to the logger could involve an allowlist of specific metric keys (like durations corresponding to non-zero counts) that should always be emitted even if their value is zero, to balance consistency with conciseness. For now, the test-side fix is preferred to maintain the current logging behavior.

References

#537926 (closed)

How to set up and validate locally

  1. Check out this branch.
  2. Run the affected spec:
    bundle exec rspec spec/services/merge_requests/mergeability/logger_spec.rb
  3. The tests, particularly those under "when its a query", should now pass consistently.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by 🤖 GitLab Bot 🤖

Merge request reports

Loading