Add a shared context for instrumentation classes for queries optimization
The following discussion from !57333 (closed) should be addressed:
-
@mikolaj_wawrzyniak started a discussion: Question: Even though I love idea of absolutely independent metrics, I wonder if performance wise it is the most optimal choice. What do you think about idea of
context
as a hash, that metrics can write and read from. That way, it may be possible to sharestart
,finish
calculations between family of metrics?issue_minimum_id = context[:issue_minimum_id] ||= ::Issue.minimum(:id) issue_maximum_id = context[:issue_maximum_id] ||= ::Issue.maximum(:id)
Above suggestion is oversimplified, and fully baked solution, most likely should use some measures to avoid key conflicts in context eg:
def context @context[:self.class.to_s.deconstantize] end
Which would make context namespace limited for example
Problem
For multiple metrics we might need to have same information, for example minimum and maximum user id.
We should not run the same query multiple times.
With usage_data.rb
implementation we handled this situation by using memoized values.
https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/utils/usage_data.rb#L217
Proposal
Have a shared context which will be available for all metrics. Here we should compute the min and max values one time and use them later where is needed.
Notes
We might consider running Usage Ping in parallel