Skip to content

Commit a8a5c33

Browse files
committed
Transaction needs to be able to describe controller action by itself
1 parent 29a1ad1 commit a8a5c33

File tree

2 files changed

+73
-71
lines changed

2 files changed

+73
-71
lines changed

lib/gitlab/metrics/rack_middleware.rb

+1-50
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@ module Gitlab
22
module Metrics
33
# Rack middleware for tracking Rails and Grape requests.
44
class RackMiddleware
5-
CONTROLLER_KEY = 'action_controller.instance'.freeze
6-
ENDPOINT_KEY = 'api.endpoint'.freeze
7-
CONTENT_TYPES = {
8-
'text/html' => :html,
9-
'text/plain' => :txt,
10-
'application/json' => :json,
11-
'text/js' => :js,
12-
'application/atom+xml' => :atom,
13-
'image/png' => :png,
14-
'image/jpeg' => :jpeg,
15-
'image/gif' => :gif,
16-
'image/svg+xml' => :svg
17-
}.freeze
18-
195
def initialize(app)
206
@app = app
217
end
@@ -42,49 +28,14 @@ def call(env)
4228
end
4329

4430
def transaction_from_env(env)
45-
trans = Transaction.new
31+
trans = Transaction.new(env)
4632

4733
trans.set(:request_uri, filtered_path(env), false)
4834
trans.set(:request_method, env['REQUEST_METHOD'], false)
4935

50-
if env[CONTROLLER_KEY]
51-
tag_controller(trans, env)
52-
elsif env[ENDPOINT_KEY]
53-
tag_endpoint(trans, env)
54-
end
55-
5636
trans
5737
end
5838

59-
def tag_controller(trans, env)
60-
controller = env[CONTROLLER_KEY]
61-
action = "#{controller.class.name}##{controller.action_name}"
62-
suffix = CONTENT_TYPES[controller.content_type]
63-
64-
if suffix && suffix != :html
65-
action += ".#{suffix}"
66-
end
67-
68-
trans.action = action
69-
end
70-
71-
def tag_endpoint(trans, env)
72-
endpoint = env[ENDPOINT_KEY]
73-
74-
begin
75-
route = endpoint.route
76-
rescue
77-
# endpoint.route is calling env[Grape::Env::GRAPE_ROUTING_ARGS][:route_info]
78-
# but env[Grape::Env::GRAPE_ROUTING_ARGS] is nil in the case of a 405 response
79-
# so we're rescuing exceptions and bailing out
80-
end
81-
82-
if route
83-
path = endpoint_paths_cache[route.request_method][route.path]
84-
trans.action = "Grape##{route.request_method} #{path}"
85-
end
86-
end
87-
8839
private
8940

9041
def filtered_path(env)

lib/gitlab/metrics/transaction.rb

+72-21
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,35 @@ module Gitlab
22
module Metrics
33
# Class for storing metrics information of a single transaction.
44
class Transaction
5+
CONTROLLER_KEY = 'action_controller.instance'.freeze
6+
ENDPOINT_KEY = 'api.endpoint'.freeze
7+
8+
CONTENT_TYPES = {
9+
'text/html' => :html,
10+
'text/plain' => :txt,
11+
'application/json' => :json,
12+
'text/js' => :js,
13+
'application/atom+xml' => :atom,
14+
'image/png' => :png,
15+
'image/jpeg' => :jpeg,
16+
'image/gif' => :gif,
17+
'image/svg+xml' => :svg
18+
}.freeze
19+
520
THREAD_KEY = :_gitlab_metrics_transaction
621

722
# The series to store events (e.g. Git pushes) in.
823
EVENT_SERIES = 'events'.freeze
924

1025
attr_reader :tags, :values, :method, :metrics
1126

12-
attr_accessor :action
13-
1427
def self.current
1528
Thread.current[THREAD_KEY]
1629
end
1730

1831
# action - A String describing the action performed, usually the class
1932
# plus method name.
20-
def initialize(action = nil)
33+
def initialize(env)
2134
@metrics = []
2235
@methods = {}
2336

@@ -26,7 +39,7 @@ def initialize(action = nil)
2639

2740
@values = Hash.new(0)
2841
@tags = {}
29-
@action = action
42+
@env = env
3043

3144
@memory_before = 0
3245
@memory_after = 0
@@ -40,22 +53,12 @@ def allocated_memory
4053
@memory_after - @memory_before
4154
end
4255

43-
def self.metric_transaction_duration_seconds
44-
@metric_transaction_duration_seconds ||= Gitlab::Metrics.histogram(
45-
:gitlab_transaction_duration_seconds,
46-
'Transaction duration',
47-
{ action: nil },
48-
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
49-
)
50-
end
51-
52-
def self.metric_transaction_allocated_memory_bytes
53-
@metric_transaction_allocated_memory_bytes ||= Gitlab::Metrics.histogram(
54-
:gitlab_transaction_allocated_memory_bytes,
55-
'Transaction allocated memory bytes',
56-
{ action: nil },
57-
[1000, 10000, 20000, 500000, 1000000, 2000000, 5000000, 10000000, 20000000, 100000000]
58-
)
56+
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
5962
end
6063

6164
def run
@@ -135,7 +138,7 @@ def submit
135138
submit_hashes = submit.map do |metric|
136139
hash = metric.to_hash
137140

138-
hash[:tags][:action] ||= @action if @action && !metric.event?
141+
hash[:tags][:action] ||= action if action && !metric.event?
139142

140143
hash
141144
end
@@ -145,6 +148,24 @@ def submit
145148

146149
private
147150

151+
def self.metric_transaction_duration_seconds
152+
@metric_transaction_duration_seconds ||= Gitlab::Metrics.histogram(
153+
:gitlab_transaction_duration_seconds,
154+
'Transaction duration',
155+
{ action: nil },
156+
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
157+
)
158+
end
159+
160+
def self.metric_transaction_allocated_memory_bytes
161+
@metric_transaction_allocated_memory_bytes ||= Gitlab::Metrics.histogram(
162+
:gitlab_transaction_allocated_memory_bytes,
163+
'Transaction allocated memory bytes',
164+
{ action: nil },
165+
[1000, 10000, 20000, 500000, 1000000, 2000000, 5000000, 10000000, 20000000, 100000000]
166+
)
167+
end
168+
148169
def self.metric_event_counter(event_name, tags)
149170
@metric_event_counters ||= {}
150171
@metric_event_counters[event_name] ||= Gitlab::Metrics.counter(
@@ -167,6 +188,36 @@ def self.metric_transaction_gauge(name)
167188
"gitlab_transaction_#{name}".to_sym, "Transaction gauge #{name}", { action: nil }, :livesum
168189
)
169190
end
191+
192+
def action_from_controller(env)
193+
controller = env[CONTROLLER_KEY]
194+
195+
action = "#{controller.class.name}##{controller.action_name}"
196+
suffix = CONTENT_TYPES[controller.content_type]
197+
198+
if suffix && suffix != :html
199+
action += ".#{suffix}"
200+
end
201+
202+
action
203+
end
204+
205+
def action_from_endpoint(env)
206+
endpoint = env[ENDPOINT_KEY]
207+
208+
begin
209+
route = endpoint.route
210+
rescue
211+
# endpoint.route is calling env[Grape::Env::GRAPE_ROUTING_ARGS][:route_info]
212+
# but env[Grape::Env::GRAPE_ROUTING_ARGS] is nil in the case of a 405 response
213+
# so we're rescuing exceptions and bailing out
214+
end
215+
216+
if route
217+
path = endpoint_paths_cache[route.request_method][route.path]
218+
"Grape##{route.request_method} #{path}"
219+
end
220+
end
170221
end
171222
end
172223
end

0 commit comments

Comments
 (0)