move metrics for ActiveRecord, RailsCache and queue duration to instance variables

This commit is contained in:
Pawel Chojnacki 2017-10-24 10:09:13 +03:00
parent 735365a367
commit 67b3e3d84a
3 changed files with 40 additions and 38 deletions

View file

@ -5,18 +5,9 @@ module Gitlab
class ActiveRecord < ActiveSupport::Subscriber
attach_to :active_record
def self.metric_sql_duration_seconds
@metric_sql_duration_seconds ||= Gitlab::Metrics.histogram(
:gitlab_sql_duration_seconds,
'SQL time',
Transaction::BASE_LABELS,
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
)
end
def sql(event)
return unless current_transaction
self.class.metric_sql_duration_seconds.observe(current_transaction.labels, event.duration / 1000.0)
metric_sql_duration_seconds.observe(current_transaction.labels, event.duration / 1000.0)
current_transaction.increment(:sql_duration, event.duration, false)
current_transaction.increment(:sql_count, 1, false)
@ -27,6 +18,15 @@ module Gitlab
def current_transaction
Transaction.current
end
def metric_sql_duration_seconds
@metric_sql_duration_seconds ||= Gitlab::Metrics.histogram(
:gitlab_sql_duration_seconds,
'SQL time',
Transaction::BASE_LABELS,
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
)
end
end
end
end

View file

@ -6,23 +6,6 @@ module Gitlab
class RailsCache < ActiveSupport::Subscriber
attach_to :active_support
def self.metric_cache_operation_duration_seconds
@metric_cache_operation_duration_seconds ||= Gitlab::Metrics.histogram(
:gitlab_cache_operation_duration_seconds,
'Cache access time',
Transaction::BASE_LABELS.merge({ action: nil }),
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
)
end
def self.metric_cache_misses_total
@metric_cache_misses_total ||= Gitlab::Metrics.counter(
:gitlab_cache_misses_total,
'Cache read miss',
Transaction::BASE_LABELS
)
end
def cache_read(event)
observe(:read, event.duration)
@ -32,7 +15,7 @@ module Gitlab
if event.payload[:hit]
current_transaction.increment(:cache_read_hit_count, 1, false)
else
self.class.metric_cache_misses_total.increment(current_transaction.labels)
self.metric_cache_misses_total.increment(current_transaction.labels)
current_transaction.increment(:cache_read_miss_count, 1, false)
end
end
@ -58,14 +41,14 @@ module Gitlab
def cache_generate(event)
return unless current_transaction
self.class.metric_cache_misses_total.increment(current_transaction.labels)
self.metric_cache_misses_total.increment(current_transaction.labels)
current_transaction.increment(:cache_read_miss_count, 1)
end
def observe(key, duration)
return unless current_transaction
self.class.metric_cache_operation_duration_seconds.observe(current_transaction.labels.merge({ operation: key }), duration / 1000.0)
self.metric_cache_operation_duration_seconds.observe(current_transaction.labels.merge({ operation: key }), duration / 1000.0)
current_transaction.increment(:cache_duration, duration, false)
current_transaction.increment(:cache_count, 1, false)
current_transaction.increment("cache_#{key}_duration".to_sym, duration, false)
@ -77,6 +60,23 @@ module Gitlab
def current_transaction
Transaction.current
end
def metric_cache_operation_duration_seconds
@metric_cache_operation_duration_seconds ||= Gitlab::Metrics.histogram(
:gitlab_cache_operation_duration_seconds,
'Cache access time',
Transaction::BASE_LABELS.merge({ action: nil }),
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
)
end
def metric_cache_misses_total
@metric_cache_misses_total ||= Gitlab::Metrics.counter(
:gitlab_cache_misses_total,
'Cache read miss',
Transaction::BASE_LABELS
)
end
end
end
end

View file

@ -5,13 +5,6 @@
module Gitlab
module Middleware
class RailsQueueDuration
def self.metric_rails_queue_duration_seconds
@metric_rails_queue_duration_seconds ||= Gitlab::Metrics.histogram(
:gitlab_rails_queue_duration_seconds,
Gitlab::Metrics::Transaction::BASE_LABELS
)
end
def initialize(app)
@app = app
end
@ -23,11 +16,20 @@ module Gitlab
# Time in milliseconds since gitlab-workhorse started the request
duration = Time.now.to_f * 1_000 - proxy_start.to_f / 1_000_000
trans.set(:rails_queue_duration, duration)
self.class.metric_rails_queue_duration_seconds.observe(trans.labels, duration / 1_000)
metric_rails_queue_duration_seconds.observe(trans.labels, duration / 1_000)
end
@app.call(env)
end
private
def metric_rails_queue_duration_seconds
@metric_rails_queue_duration_seconds ||= Gitlab::Metrics.histogram(
:gitlab_rails_queue_duration_seconds,
Gitlab::Metrics::Transaction::BASE_LABELS
)
end
end
end
end