Skip to content

Commit a300787

Browse files
committed
Use Mutex to guard metrics creation in transaction. Switch action view to threadsafe instance variables
1 parent 67b3e3d commit a300787

File tree

2 files changed

+49
-28
lines changed

2 files changed

+49
-28
lines changed

lib/gitlab/metrics/subscribers/action_view.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def render_template(event)
1515

1616
private
1717

18-
def self.metric_view_rendering_duration_seconds
18+
def metric_view_rendering_duration_seconds
1919
@metric_view_rendering_duration_seconds ||= Gitlab::Metrics.histogram(
2020
:gitlab_view_rendering_duration_seconds,
2121
'View rendering time',
@@ -28,7 +28,7 @@ def track(event)
2828
values = values_for(event)
2929
tags = tags_for(event)
3030

31-
self.class.metric_view_rendering_duration_seconds.observe(
31+
self.metric_view_rendering_duration_seconds.observe(
3232
current_transaction.labels.merge(tags),
3333
event.duration
3434
)

lib/gitlab/metrics/transaction.rb

+47-26
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Transaction
66
BASE_LABELS = { controller: nil, action: nil }.freeze
77

88
THREAD_KEY = :_gitlab_metrics_transaction
9+
METRICS_MUTEX = Mutex.new
910

1011
# The series to store events (e.g. Git pushes) in.
1112
EVENT_SERIES = 'events'.freeze
@@ -132,44 +133,64 @@ def action
132133
end
133134

134135
def self.metric_transaction_duration_seconds
135-
@metric_transaction_duration_seconds ||= Gitlab::Metrics.histogram(
136-
:gitlab_transaction_duration_seconds,
137-
'Transaction duration',
138-
BASE_LABELS,
139-
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
140-
)
136+
return @metric_transaction_duration_seconds if @metric_transaction_duration_seconds
137+
138+
METRICS_MUTEX.synchronize do
139+
@metric_transaction_duration_seconds ||= Gitlab::Metrics.histogram(
140+
:gitlab_transaction_duration_seconds,
141+
'Transaction duration',
142+
BASE_LABELS,
143+
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
144+
)
145+
end
141146
end
142147

143148
def self.metric_transaction_allocated_memory_bytes
144-
@metric_transaction_allocated_memory_bytes ||= Gitlab::Metrics.histogram(
145-
:gitlab_transaction_allocated_memory_bytes,
146-
'Transaction allocated memory bytes',
147-
BASE_LABELS,
148-
[1000, 10000, 20000, 500000, 1000000, 2000000, 5000000, 10000000, 20000000, 100000000]
149-
)
149+
return @metric_transaction_allocated_memory_bytes if @metric_transaction_allocated_memory_bytes
150+
151+
METRICS_MUTEX.synchronize do
152+
@metric_transaction_allocated_memory_bytes ||= Gitlab::Metrics.histogram(
153+
:gitlab_transaction_allocated_memory_bytes,
154+
'Transaction allocated memory bytes',
155+
BASE_LABELS,
156+
[1000, 10000, 20000, 500000, 1000000, 2000000, 5000000, 10000000, 20000000, 100000000]
157+
)
158+
end
150159
end
151160

152161
def self.metric_event_counter(event_name, tags)
153-
@metric_event_counters ||= {}
154-
@metric_event_counters[event_name] ||= Gitlab::Metrics.counter(
155-
"gitlab_transaction_event_#{event_name}_total".to_sym,
156-
"Transaction event #{event_name} counter",
157-
tags.merge(BASE_LABELS)
158-
)
162+
return @metric_event_counters[event_name] if @metric_event_counters&.has_key?(event_name)
163+
164+
METRICS_MUTEX.synchronize do
165+
@metric_event_counters ||= {}
166+
@metric_event_counters[event_name] ||= Gitlab::Metrics.counter(
167+
"gitlab_transaction_event_#{event_name}_total".to_sym,
168+
"Transaction event #{event_name} counter",
169+
tags.merge(BASE_LABELS)
170+
)
171+
end
159172
end
160173

161174
def self.metric_transaction_counter(name)
162-
@metric_transaction_counters ||= {}
163-
@metric_transaction_counters[name] ||= Gitlab::Metrics.counter(
164-
"gitlab_transaction_#{name}_total".to_sym, "Transaction #{name} counter", BASE_LABELS
165-
)
175+
return @metric_transaction_counters[name] if @metric_transaction_counters&.has_key?(name)
176+
177+
METRICS_MUTEX.synchronize do
178+
@metric_transaction_counters ||= {}
179+
@metric_transaction_counters[name] ||= Gitlab::Metrics.counter(
180+
"gitlab_transaction_#{name}_total".to_sym, "Transaction #{name} counter", BASE_LABELS
181+
)
182+
end
166183
end
167184

168185
def self.metric_transaction_gauge(name)
169-
@metric_transaction_gauges ||= {}
170-
@metric_transaction_gauges[name] ||= Gitlab::Metrics.gauge(
171-
"gitlab_transaction_#{name}".to_sym, "Transaction gauge #{name}", BASE_LABELS, :livesum
172-
)
186+
return @metric_transaction_gauges[name] if @metric_transaction_gauges&.has_key?(name)
187+
188+
METRICS_MUTEX.synchronize do
189+
@metric_transaction_gauges ||= {}
190+
@metric_transaction_gauges[name] ||= Gitlab::Metrics.gauge(
191+
"gitlab_transaction_#{name}".to_sym, "Transaction gauge #{name}", BASE_LABELS, :livesum
192+
)
193+
end
173194
end
174195
end
175196
end

0 commit comments

Comments
 (0)