diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2016-04-04 14:00:35 +0200 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2016-04-06 14:31:52 +0200 |
commit | 1af6cf28c031cec7813d3fc090476c088de57173 (patch) | |
tree | 0ab61a3b72a49bad7e5880db92d28e3c9071de96 /lib | |
parent | a3cf3d19826ea1e67381592f2d91f7041cccc222 (diff) | |
download | gitlab-ce-1af6cf28c031cec7813d3fc090476c088de57173.tar.gz |
Measure Ruby blocks using Gitlab::Metrics
This allows measuring of timings of arbitrary Ruby blocks, this allows
for more fine grained performance monitoring. Custom values and tags can
also be attached to a block.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/metrics.rb | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/gitlab/metrics.rb b/lib/gitlab/metrics.rb index 88a265c6af2..4a3f47b5a95 100644 --- a/lib/gitlab/metrics.rb +++ b/lib/gitlab/metrics.rb @@ -70,6 +70,32 @@ module Gitlab value.to_s.gsub('=', '\\=') end + # Measures the execution time of a block. + # + # Example: + # + # Gitlab::Metrics.measure(:find_by_username_timings) do + # User.find_by_username(some_username) + # end + # + # series - The name of the series to store the data in. + # values - A Hash containing extra values to add to the metric. + # tags - A Hash containing extra tags to add to the metric. + # + # Returns the value yielded by the supplied block. + def self.measure(series, values = {}, tags = {}) + return yield unless Transaction.current + + start = Time.now.to_f + retval = yield + duration = (Time.now.to_f - start) * 1000.0 + values = values.merge(duration: duration) + + Transaction.current.add_metric(series, values, tags) + + retval + end + # When enabled this should be set before being used as the usual pattern # "@foo ||= bar" is _not_ thread-safe. if enabled? |