Skip to content

Service Ping instrumentation classes - add customization of timestamp column in database metric

What does this MR do and why?

This commit enhance database instrumentation DSL with timestamp_column keyword which enables users to define custom timestamp column for their metrics. By default timestamp column is created_at.

Resolves #364345 (closed)

Screenshots or screen recordings

These are strongly recommended to assist reviewers and reduce the time to merge your change.

How to set up and validate locally

There is no metric that uses new DSL keyword, so in order to validate MR changes one needs to modify existing one eg:

diff --git a/lib/gitlab/usage/metrics/instrumentations/count_issues_metric.rb b/lib/gitlab/usage/metrics/instrumentations/count_issues_metric.rb
index 07dfba70d92..44ea93b97b2 100644
--- a/lib/gitlab/usage/metrics/instrumentations/count_issues_metric.rb
+++ b/lib/gitlab/usage/metrics/instrumentations/count_issues_metric.rb
@@ -12,7 +12,8 @@ class CountIssuesMetric < DatabaseMetric

           relation { Issue }

-          cache_start_and_finish_as :issue
+          # cache_start_and_finish_as :issue
+          timestamp_column :last_edited_at
         end
       end
     end

Than check how many records matches 28d time constraint

> Issue.where(last_edited_at: 30.days.ago..2.days.ago).count
=> 0

If non then update one of them to have some data

Issue.first.update(last_edited_at: 5.days.ago)

Than finally use metric instrumentation class to collect the same data

Gitlab::Usage::Metrics::Instrumentations::CountIssuesMetric.new(time_frame: '28d').value
=> 1

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Mikołaj Wawrzyniak

Merge request reports