Commit a300787f authored by Pawel Chojnacki's avatar Pawel Chojnacki

Use Mutex to guard metrics creation in transaction. Switch action view to...

Use Mutex to guard metrics creation in transaction. Switch action view to threadsafe instance variables
parent 67b3e3d8
...@@ -15,7 +15,7 @@ module Gitlab ...@@ -15,7 +15,7 @@ module Gitlab
private private
def self.metric_view_rendering_duration_seconds def metric_view_rendering_duration_seconds
@metric_view_rendering_duration_seconds ||= Gitlab::Metrics.histogram( @metric_view_rendering_duration_seconds ||= Gitlab::Metrics.histogram(
:gitlab_view_rendering_duration_seconds, :gitlab_view_rendering_duration_seconds,
'View rendering time', 'View rendering time',
...@@ -28,7 +28,7 @@ module Gitlab ...@@ -28,7 +28,7 @@ module Gitlab
values = values_for(event) values = values_for(event)
tags = tags_for(event) tags = tags_for(event)
self.class.metric_view_rendering_duration_seconds.observe( self.metric_view_rendering_duration_seconds.observe(
current_transaction.labels.merge(tags), current_transaction.labels.merge(tags),
event.duration event.duration
) )
......
...@@ -6,6 +6,7 @@ module Gitlab ...@@ -6,6 +6,7 @@ module Gitlab
BASE_LABELS = { controller: nil, action: nil }.freeze BASE_LABELS = { controller: nil, action: nil }.freeze
THREAD_KEY = :_gitlab_metrics_transaction THREAD_KEY = :_gitlab_metrics_transaction
METRICS_MUTEX = Mutex.new
# The series to store events (e.g. Git pushes) in. # The series to store events (e.g. Git pushes) in.
EVENT_SERIES = 'events'.freeze EVENT_SERIES = 'events'.freeze
...@@ -132,44 +133,64 @@ module Gitlab ...@@ -132,44 +133,64 @@ module Gitlab
end end
def self.metric_transaction_duration_seconds def self.metric_transaction_duration_seconds
@metric_transaction_duration_seconds ||= Gitlab::Metrics.histogram( return @metric_transaction_duration_seconds if @metric_transaction_duration_seconds
:gitlab_transaction_duration_seconds,
'Transaction duration', METRICS_MUTEX.synchronize do
BASE_LABELS, @metric_transaction_duration_seconds ||= Gitlab::Metrics.histogram(
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0] :gitlab_transaction_duration_seconds,
) 'Transaction duration',
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
def self.metric_transaction_allocated_memory_bytes def self.metric_transaction_allocated_memory_bytes
@metric_transaction_allocated_memory_bytes ||= Gitlab::Metrics.histogram( return @metric_transaction_allocated_memory_bytes if @metric_transaction_allocated_memory_bytes
:gitlab_transaction_allocated_memory_bytes,
'Transaction allocated memory bytes', METRICS_MUTEX.synchronize do
BASE_LABELS, @metric_transaction_allocated_memory_bytes ||= Gitlab::Metrics.histogram(
[1000, 10000, 20000, 500000, 1000000, 2000000, 5000000, 10000000, 20000000, 100000000] :gitlab_transaction_allocated_memory_bytes,
) 'Transaction allocated memory bytes',
BASE_LABELS,
[1000, 10000, 20000, 500000, 1000000, 2000000, 5000000, 10000000, 20000000, 100000000]
)
end
end end
def self.metric_event_counter(event_name, tags) def self.metric_event_counter(event_name, tags)
@metric_event_counters ||= {} return @metric_event_counters[event_name] if @metric_event_counters&.has_key?(event_name)
@metric_event_counters[event_name] ||= Gitlab::Metrics.counter(
"gitlab_transaction_event_#{event_name}_total".to_sym, METRICS_MUTEX.synchronize do
"Transaction event #{event_name} counter", @metric_event_counters ||= {}
tags.merge(BASE_LABELS) @metric_event_counters[event_name] ||= Gitlab::Metrics.counter(
) "gitlab_transaction_event_#{event_name}_total".to_sym,
"Transaction event #{event_name} counter",
tags.merge(BASE_LABELS)
)
end
end end
def self.metric_transaction_counter(name) def self.metric_transaction_counter(name)
@metric_transaction_counters ||= {} return @metric_transaction_counters[name] if @metric_transaction_counters&.has_key?(name)
@metric_transaction_counters[name] ||= Gitlab::Metrics.counter(
"gitlab_transaction_#{name}_total".to_sym, "Transaction #{name} counter", BASE_LABELS METRICS_MUTEX.synchronize do
) @metric_transaction_counters ||= {}
@metric_transaction_counters[name] ||= Gitlab::Metrics.counter(
"gitlab_transaction_#{name}_total".to_sym, "Transaction #{name} counter", BASE_LABELS
)
end
end end
def self.metric_transaction_gauge(name) def self.metric_transaction_gauge(name)
@metric_transaction_gauges ||= {} return @metric_transaction_gauges[name] if @metric_transaction_gauges&.has_key?(name)
@metric_transaction_gauges[name] ||= Gitlab::Metrics.gauge(
"gitlab_transaction_#{name}".to_sym, "Transaction gauge #{name}", BASE_LABELS, :livesum METRICS_MUTEX.synchronize do
) @metric_transaction_gauges ||= {}
@metric_transaction_gauges[name] ||= Gitlab::Metrics.gauge(
"gitlab_transaction_#{name}".to_sym, "Transaction gauge #{name}", BASE_LABELS, :livesum
)
end
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment