Skip to content

Commit 43a9777

Browse files
committed
Make transaction labels more readable
1 parent 6cd912b commit 43a9777

File tree

3 files changed

+48
-34
lines changed

3 files changed

+48
-34
lines changed

lib/gitlab/metrics/subscribers/active_record.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ def self.metric_sql_duration_seconds
99
@metric_sql_duration_seconds ||= Gitlab::Metrics.histogram(
1010
:gitlab_sql_duration_seconds,
1111
'SQL time',
12-
{ action: nil },
12+
Transaction::BASE_LABELS,
1313
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
1414
)
1515
end
1616

1717
def sql(event)
1818
return unless current_transaction
19-
self.class.metric_sql_duration_seconds.observe({ action: current_transaction.action }, event.duration / 1000.0)
19+
self.class.metric_sql_duration_seconds.observe(current_transaction.labels, event.duration / 1000.0)
2020

2121
current_transaction.increment(:sql_duration, event.duration, false)
2222
current_transaction.increment(:sql_count, 1, false)

lib/gitlab/metrics/subscribers/rails_cache.rb

+16-12
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,25 @@ def self.metric_cache_operation_duration_seconds
1010
@metric_cache_operation_duration_seconds ||= Gitlab::Metrics.histogram(
1111
:gitlab_cache_operation_duration_seconds,
1212
'Cache access time',
13-
{ action: nil, operation: nil },
13+
Transaction::BASE_LABELS.merge({ action: nil }),
1414
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
1515
)
1616
end
1717

1818
def self.metric_cache_read_hit_total
19-
@metric_cache_read_hit_total ||= Gitlab::Metrics.counter(:gitlab_cache_read_hit_total, 'Cache read hit', { action: nil })
19+
@metric_cache_read_hit_total ||= Gitlab::Metrics.counter(
20+
:gitlab_cache_read_hit_total,
21+
'Cache read hit',
22+
Transaction::BASE_LABELS
23+
)
2024
end
2125

2226
def self.metric_cache_read_miss_total
23-
@metric_cache_read_miss_total ||= Gitlab::Metrics.counter(:gitlab_cache_read_miss_total, 'Cache read miss', { action: nil })
27+
@metric_cache_read_miss_total ||= Gitlab::Metrics.counter(
28+
:gitlab_cache_read_miss_total,
29+
'Cache read miss',
30+
Transaction::BASE_LABELS
31+
)
2432
end
2533

2634
def cache_read(event)
@@ -30,10 +38,10 @@ def cache_read(event)
3038
return if event.payload[:super_operation] == :fetch
3139

3240
if event.payload[:hit]
33-
self.class.metric_cache_read_hit_total.increment({ action: action })
41+
self.class.metric_cache_read_hit_total.increment(current_transaction.labels)
3442
current_transaction.increment(:cache_read_hit_count, 1, false)
3543
else
36-
self.class.metric_cache_read_miss_total.increment({ action: action })
44+
self.class.metric_cache_read_miss_total.increment(current_transaction.labels)
3745
current_transaction.increment(:cache_read_miss_count, 1, false)
3846
end
3947
end
@@ -53,21 +61,21 @@ def cache_exist?(event)
5361
def cache_fetch_hit(event)
5462
return unless current_transaction
5563

56-
self.class.metric_cache_read_hit_total.increment({ action: action })
64+
self.class.metric_cache_read_hit_total.increment(current_transaction.labels)
5765
current_transaction.increment(:cache_read_hit_count, 1)
5866
end
5967

6068
def cache_generate(event)
6169
return unless current_transaction
6270

63-
self.class.metric_cache_read_miss_total.increment({ action: action })
71+
self.class.metric_cache_read_miss_total.increment(current_transaction.labels)
6472
current_transaction.increment(:cache_read_miss_count, 1)
6573
end
6674

6775
def observe(key, duration)
6876
return unless current_transaction
6977

70-
self.class.metric_cache_operation_duration_seconds.observe({ operation: key, action: action }, duration / 1000.0)
78+
self.class.metric_cache_operation_duration_seconds.observe(current_transaction.labels.merge({ operation: key }), duration / 1000.0)
7179
current_transaction.increment(:cache_duration, duration, false)
7280
current_transaction.increment(:cache_count, 1, false)
7381
current_transaction.increment("cache_#{key}_duration".to_sym, duration, false)
@@ -76,10 +84,6 @@ def observe(key, duration)
7684

7785
private
7886

79-
def action
80-
current_transaction&.action
81-
end
82-
8387
def current_transaction
8488
Transaction.current
8589
end

lib/gitlab/metrics/transaction.rb

+30-20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Gitlab
22
module Metrics
33
# Class for storing metrics information of a single transaction.
44
class Transaction
5+
BASE_LABELS = { controller: nil, action: nil }.freeze
56
CONTROLLER_KEY = 'action_controller.instance'.freeze
67
ENDPOINT_KEY = 'api.endpoint'.freeze
78

@@ -54,11 +55,20 @@ def allocated_memory
5455
end
5556

5657
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 || {}
6272
end
6373

6474
def run
@@ -72,8 +82,8 @@ def run
7282
@memory_after = System.memory_usage
7383
@finished_at = System.monotonic_time
7484

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)
7787

7888
Thread.current[THREAD_KEY] = nil
7989
end
@@ -90,7 +100,7 @@ def add_metric(series, values, tags = {})
90100
# event_name - The name of the event (e.g. "git_push").
91101
# tags - A set of tags to attach to the event.
92102
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))
94104
@metrics << Metric.new(EVENT_SERIES, { count: 1 }, tags, :event)
95105
end
96106

@@ -104,12 +114,12 @@ def method_call_for(name)
104114
end
105115

106116
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
108118
@values[name] += value
109119
end
110120

111121
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
113123
@values[name] = value
114124
end
115125

@@ -152,7 +162,7 @@ def self.metric_transaction_duration_seconds
152162
@metric_transaction_duration_seconds ||= Gitlab::Metrics.histogram(
153163
:gitlab_transaction_duration_seconds,
154164
'Transaction duration',
155-
{ action: nil },
165+
BASE_LABELS,
156166
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
157167
)
158168
end
@@ -161,7 +171,7 @@ def self.metric_transaction_allocated_memory_bytes
161171
@metric_transaction_allocated_memory_bytes ||= Gitlab::Metrics.histogram(
162172
:gitlab_transaction_allocated_memory_bytes,
163173
'Transaction allocated memory bytes',
164-
{ action: nil },
174+
BASE_LABELS,
165175
[1000, 10000, 20000, 500000, 1000000, 2000000, 5000000, 10000000, 20000000, 100000000]
166176
)
167177
end
@@ -171,38 +181,38 @@ def self.metric_event_counter(event_name, tags)
171181
@metric_event_counters[event_name] ||= Gitlab::Metrics.counter(
172182
"gitlab_transaction_event_#{event_name}_total".to_sym,
173183
"Transaction event #{event_name} counter",
174-
tags.merge({ action: nil })
184+
tags.merge(BASE_LABELS)
175185
)
176186
end
177187

178188
def self.metric_transaction_counter(name)
179189
@metric_transaction_counters ||= {}
180190
@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
182192
)
183193
end
184194

185195
def self.metric_transaction_gauge(name)
186196
@metric_transaction_gauges ||= {}
187197
@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
189199
)
190200
end
191201

192-
def action_from_controller(env)
202+
def labels_from_controller(env)
193203
controller = env[CONTROLLER_KEY]
194204

195-
action = "#{controller.class.name}##{controller.action_name}"
205+
action = "#{controller.action_name}"
196206
suffix = CONTENT_TYPES[controller.content_type]
197207

198208
if suffix && suffix != :html
199209
action += ".#{suffix}"
200210
end
201211

202-
action
212+
{ controller: controller.class.name, action: action }
203213
end
204214

205-
def action_from_endpoint(env)
215+
def labels_from_endpoint(env)
206216
endpoint = env[ENDPOINT_KEY]
207217

208218
begin
@@ -215,7 +225,7 @@ def action_from_endpoint(env)
215225

216226
if route
217227
path = endpoint_paths_cache[route.request_method][route.path]
218-
"Grape##{route.request_method} #{path}"
228+
{ controller: 'Grape', action: "#{route.request_method} #{path}" }
219229
end
220230
end
221231
end

0 commit comments

Comments
 (0)