diff options
author | Felipe Artur <felipefac@gmail.com> | 2016-04-13 15:42:33 -0300 |
---|---|---|
committer | Felipe Artur <felipefac@gmail.com> | 2016-04-13 15:42:33 -0300 |
commit | 5395e6175e77d3cd8800bb9f41c09d59934b502f (patch) | |
tree | 2234488f40eb4f7aa6920fbe2ab41b1d6f53e14e /lib/gitlab/metrics.rb | |
parent | 39d853f5bd9492fa8dfc8e07ec11070705d3c879 (diff) | |
parent | c0678f2d281242601560e2646cab1aa8a349c4bb (diff) | |
download | gitlab-ce-5395e6175e77d3cd8800bb9f41c09d59934b502f.tar.gz |
Merge branch 'master' into issue_3508issue_3508
Diffstat (limited to 'lib/gitlab/metrics.rb')
-rw-r--r-- | lib/gitlab/metrics.rb | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/lib/gitlab/metrics.rb b/lib/gitlab/metrics.rb index 4a3f47b5a95..484970c5a10 100644 --- a/lib/gitlab/metrics.rb +++ b/lib/gitlab/metrics.rb @@ -74,28 +74,46 @@ module Gitlab # # Example: # - # Gitlab::Metrics.measure(:find_by_username_timings) do + # Gitlab::Metrics.measure(:find_by_username_duration) 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. + # name - The name of the field to store the execution time in. # # Returns the value yielded by the supplied block. - def self.measure(series, values = {}, tags = {}) - return yield unless Transaction.current + def self.measure(name) + trans = current_transaction + + return yield unless trans + + real_start = Time.now.to_f + cpu_start = System.cpu_time - 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) + cpu_stop = System.cpu_time + real_stop = Time.now.to_f + + real_time = (real_stop - real_start) * 1000.0 + cpu_time = cpu_stop - cpu_start + + trans.increment("#{name}_real_time", real_time) + trans.increment("#{name}_cpu_time", cpu_time) + trans.increment("#{name}_call_count", 1) retval end + # Adds a tag to the current transaction (if any) + # + # name - The name of the tag to add. + # value - The value of the tag. + def self.tag_transaction(name, value) + trans = current_transaction + + trans.add_tag(name, value) if trans + end + # When enabled this should be set before being used as the usual pattern # "@foo ||= bar" is _not_ thread-safe. if enabled? @@ -107,5 +125,11 @@ module Gitlab new(udp: { host: host, port: port }) end end + + private + + def self.current_transaction + Transaction.current + end end end |