@@ -6,6 +6,7 @@ class Transaction
6
6
BASE_LABELS = { controller : nil , action : nil } . freeze
7
7
8
8
THREAD_KEY = :_gitlab_metrics_transaction
9
+ METRICS_MUTEX = Mutex . new
9
10
10
11
# The series to store events (e.g. Git pushes) in.
11
12
EVENT_SERIES = 'events' . freeze
@@ -132,44 +133,64 @@ def action
132
133
end
133
134
134
135
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
141
146
end
142
147
143
148
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
150
159
end
151
160
152
161
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
159
172
end
160
173
161
174
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
166
183
end
167
184
168
185
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
173
194
end
174
195
end
175
196
end
0 commit comments