@@ -2,6 +2,7 @@ module Gitlab
2
2
module Metrics
3
3
# Class for storing metrics information of a single transaction.
4
4
class Transaction
5
+ BASE_LABELS = { controller : nil , action : nil } . freeze
5
6
CONTROLLER_KEY = 'action_controller.instance' . freeze
6
7
ENDPOINT_KEY = 'api.endpoint' . freeze
7
8
@@ -54,11 +55,20 @@ def allocated_memory
54
55
end
55
56
56
57
def action
57
- @action ||= if @env [ CONTROLLER_KEY ]
58
- action_from_controller ( @env ) || ''
59
- elsif @env [ ENDPOINT_KEY ]
60
- action_from_endpoint ( @env ) || ''
61
- end
58
+ "#{ labels [ :controller ] } ##{ labels [ :action ] } " if labels
59
+ end
60
+
61
+ def labels
62
+ return @labels if @labels
63
+
64
+ # memoize transaction labels only source env variables were present
65
+ @labels = if @env [ CONTROLLER_KEY ]
66
+ labels_from_controller ( @env ) || { }
67
+ elsif @env [ ENDPOINT_KEY ]
68
+ labels_from_endpoint ( @env ) || { }
69
+ end
70
+
71
+ @labels || { }
62
72
end
63
73
64
74
def run
@@ -72,8 +82,8 @@ def run
72
82
@memory_after = System . memory_usage
73
83
@finished_at = System . monotonic_time
74
84
75
- Transaction . metric_transaction_duration_seconds . observe ( { action : action } , duration * 1000 )
76
- Transaction . metric_transaction_allocated_memory_bytes . observe ( { action : action } , allocated_memory * 1024.0 )
85
+ Transaction . metric_transaction_duration_seconds . observe ( labels , duration * 1000 )
86
+ Transaction . metric_transaction_allocated_memory_bytes . observe ( labels , allocated_memory * 1024.0 )
77
87
78
88
Thread . current [ THREAD_KEY ] = nil
79
89
end
@@ -90,7 +100,7 @@ def add_metric(series, values, tags = {})
90
100
# event_name - The name of the event (e.g. "git_push").
91
101
# tags - A set of tags to attach to the event.
92
102
def add_event ( event_name , tags = { } )
93
- self . class . metric_event_counter ( event_name , tags ) . increment ( tags . merge ( { action : action } ) )
103
+ self . class . metric_event_counter ( event_name , tags ) . increment ( tags . merge ( labels ) )
94
104
@metrics << Metric . new ( EVENT_SERIES , { count : 1 } , tags , :event )
95
105
end
96
106
@@ -104,12 +114,12 @@ def method_call_for(name)
104
114
end
105
115
106
116
def increment ( name , value , use_prometheus = true )
107
- self . class . metric_transaction_counter ( name ) . increment ( { action : action } , value ) if use_prometheus
117
+ self . class . metric_transaction_counter ( name ) . increment ( labels , value ) if use_prometheus
108
118
@values [ name ] += value
109
119
end
110
120
111
121
def set ( name , value , use_prometheus = true )
112
- self . class . metric_transaction_gauge ( name ) . set ( { action : action } , value ) if use_prometheus
122
+ self . class . metric_transaction_gauge ( name ) . set ( labels , value ) if use_prometheus
113
123
@values [ name ] = value
114
124
end
115
125
@@ -152,7 +162,7 @@ def self.metric_transaction_duration_seconds
152
162
@metric_transaction_duration_seconds ||= Gitlab ::Metrics . histogram (
153
163
:gitlab_transaction_duration_seconds ,
154
164
'Transaction duration' ,
155
- { action : nil } ,
165
+ BASE_LABELS ,
156
166
[ 0.001 , 0.002 , 0.005 , 0.01 , 0.02 , 0.05 , 0.1 , 0.500 , 2.0 , 10.0 ]
157
167
)
158
168
end
@@ -161,7 +171,7 @@ def self.metric_transaction_allocated_memory_bytes
161
171
@metric_transaction_allocated_memory_bytes ||= Gitlab ::Metrics . histogram (
162
172
:gitlab_transaction_allocated_memory_bytes ,
163
173
'Transaction allocated memory bytes' ,
164
- { action : nil } ,
174
+ BASE_LABELS ,
165
175
[ 1000 , 10000 , 20000 , 500000 , 1000000 , 2000000 , 5000000 , 10000000 , 20000000 , 100000000 ]
166
176
)
167
177
end
@@ -171,38 +181,38 @@ def self.metric_event_counter(event_name, tags)
171
181
@metric_event_counters [ event_name ] ||= Gitlab ::Metrics . counter (
172
182
"gitlab_transaction_event_#{ event_name } _total" . to_sym ,
173
183
"Transaction event #{ event_name } counter" ,
174
- tags . merge ( { action : nil } )
184
+ tags . merge ( BASE_LABELS )
175
185
)
176
186
end
177
187
178
188
def self . metric_transaction_counter ( name )
179
189
@metric_transaction_counters ||= { }
180
190
@metric_transaction_counters [ name ] ||= Gitlab ::Metrics . counter (
181
- "gitlab_transaction_#{ name } _total" . to_sym , "Transaction #{ name } counter" , action : nil
191
+ "gitlab_transaction_#{ name } _total" . to_sym , "Transaction #{ name } counter" , BASE_LABELS
182
192
)
183
193
end
184
194
185
195
def self . metric_transaction_gauge ( name )
186
196
@metric_transaction_gauges ||= { }
187
197
@metric_transaction_gauges [ name ] ||= Gitlab ::Metrics . gauge (
188
- "gitlab_transaction_#{ name } " . to_sym , "Transaction gauge #{ name } " , { action : nil } , :livesum
198
+ "gitlab_transaction_#{ name } " . to_sym , "Transaction gauge #{ name } " , BASE_LABELS , :livesum
189
199
)
190
200
end
191
201
192
- def action_from_controller ( env )
202
+ def labels_from_controller ( env )
193
203
controller = env [ CONTROLLER_KEY ]
194
204
195
- action = "#{ controller . class . name } # #{ controller . action_name } "
205
+ action = "#{ controller . action_name } "
196
206
suffix = CONTENT_TYPES [ controller . content_type ]
197
207
198
208
if suffix && suffix != :html
199
209
action += ".#{ suffix } "
200
210
end
201
211
202
- action
212
+ { controller : controller . class . name , action : action }
203
213
end
204
214
205
- def action_from_endpoint ( env )
215
+ def labels_from_endpoint ( env )
206
216
endpoint = env [ ENDPOINT_KEY ]
207
217
208
218
begin
@@ -215,7 +225,7 @@ def action_from_endpoint(env)
215
225
216
226
if route
217
227
path = endpoint_paths_cache [ route . request_method ] [ route . path ]
218
- " Grape# # {route . request_method } #{ path } "
228
+ { controller : ' Grape' , action : " # {route . request_method } #{ path } " }
219
229
end
220
230
end
221
231
end
0 commit comments