Commit 43a9777e authored by Pawel Chojnacki's avatar Pawel Chojnacki

Make transaction labels more readable

parent 6cd912ba
...@@ -9,14 +9,14 @@ module Gitlab ...@@ -9,14 +9,14 @@ module Gitlab
@metric_sql_duration_seconds ||= Gitlab::Metrics.histogram( @metric_sql_duration_seconds ||= Gitlab::Metrics.histogram(
:gitlab_sql_duration_seconds, :gitlab_sql_duration_seconds,
'SQL time', 'SQL time',
{ action: nil }, Transaction::BASE_LABELS,
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0] [0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
) )
end end
def sql(event) def sql(event)
return unless current_transaction return unless current_transaction
self.class.metric_sql_duration_seconds.observe({ action: current_transaction.action }, event.duration / 1000.0) self.class.metric_sql_duration_seconds.observe(current_transaction.labels, event.duration / 1000.0)
current_transaction.increment(:sql_duration, event.duration, false) current_transaction.increment(:sql_duration, event.duration, false)
current_transaction.increment(:sql_count, 1, false) current_transaction.increment(:sql_count, 1, false)
......
...@@ -10,17 +10,25 @@ module Gitlab ...@@ -10,17 +10,25 @@ module Gitlab
@metric_cache_operation_duration_seconds ||= Gitlab::Metrics.histogram( @metric_cache_operation_duration_seconds ||= Gitlab::Metrics.histogram(
:gitlab_cache_operation_duration_seconds, :gitlab_cache_operation_duration_seconds,
'Cache access time', 'Cache access time',
{ action: nil, operation: nil }, Transaction::BASE_LABELS.merge({ action: nil }),
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0] [0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
) )
end end
def self.metric_cache_read_hit_total def self.metric_cache_read_hit_total
@metric_cache_read_hit_total ||= Gitlab::Metrics.counter(:gitlab_cache_read_hit_total, 'Cache read hit', { action: nil }) @metric_cache_read_hit_total ||= Gitlab::Metrics.counter(
:gitlab_cache_read_hit_total,
'Cache read hit',
Transaction::BASE_LABELS
)
end end
def self.metric_cache_read_miss_total def self.metric_cache_read_miss_total
@metric_cache_read_miss_total ||= Gitlab::Metrics.counter(:gitlab_cache_read_miss_total, 'Cache read miss', { action: nil }) @metric_cache_read_miss_total ||= Gitlab::Metrics.counter(
:gitlab_cache_read_miss_total,
'Cache read miss',
Transaction::BASE_LABELS
)
end end
def cache_read(event) def cache_read(event)
...@@ -30,10 +38,10 @@ module Gitlab ...@@ -30,10 +38,10 @@ module Gitlab
return if event.payload[:super_operation] == :fetch return if event.payload[:super_operation] == :fetch
if event.payload[:hit] if event.payload[:hit]
self.class.metric_cache_read_hit_total.increment({ action: action }) self.class.metric_cache_read_hit_total.increment(current_transaction.labels)
current_transaction.increment(:cache_read_hit_count, 1, false) current_transaction.increment(:cache_read_hit_count, 1, false)
else else
self.class.metric_cache_read_miss_total.increment({ action: action }) self.class.metric_cache_read_miss_total.increment(current_transaction.labels)
current_transaction.increment(:cache_read_miss_count, 1, false) current_transaction.increment(:cache_read_miss_count, 1, false)
end end
end end
...@@ -53,21 +61,21 @@ module Gitlab ...@@ -53,21 +61,21 @@ module Gitlab
def cache_fetch_hit(event) def cache_fetch_hit(event)
return unless current_transaction return unless current_transaction
self.class.metric_cache_read_hit_total.increment({ action: action }) self.class.metric_cache_read_hit_total.increment(current_transaction.labels)
current_transaction.increment(:cache_read_hit_count, 1) current_transaction.increment(:cache_read_hit_count, 1)
end end
def cache_generate(event) def cache_generate(event)
return unless current_transaction return unless current_transaction
self.class.metric_cache_read_miss_total.increment({ action: action }) self.class.metric_cache_read_miss_total.increment(current_transaction.labels)
current_transaction.increment(:cache_read_miss_count, 1) current_transaction.increment(:cache_read_miss_count, 1)
end end
def observe(key, duration) def observe(key, duration)
return unless current_transaction return unless current_transaction
self.class.metric_cache_operation_duration_seconds.observe({ operation: key, action: action }, duration / 1000.0) self.class.metric_cache_operation_duration_seconds.observe(current_transaction.labels.merge({ operation: key }), duration / 1000.0)
current_transaction.increment(:cache_duration, duration, false) current_transaction.increment(:cache_duration, duration, false)
current_transaction.increment(:cache_count, 1, false) current_transaction.increment(:cache_count, 1, false)
current_transaction.increment("cache_#{key}_duration".to_sym, duration, false) current_transaction.increment("cache_#{key}_duration".to_sym, duration, false)
...@@ -76,10 +84,6 @@ module Gitlab ...@@ -76,10 +84,6 @@ module Gitlab
private private
def action
current_transaction&.action
end
def current_transaction def current_transaction
Transaction.current Transaction.current
end end
......
...@@ -2,6 +2,7 @@ module Gitlab ...@@ -2,6 +2,7 @@ module Gitlab
module Metrics module Metrics
# Class for storing metrics information of a single transaction. # Class for storing metrics information of a single transaction.
class Transaction class Transaction
BASE_LABELS = { controller: nil, action: nil }.freeze
CONTROLLER_KEY = 'action_controller.instance'.freeze CONTROLLER_KEY = 'action_controller.instance'.freeze
ENDPOINT_KEY = 'api.endpoint'.freeze ENDPOINT_KEY = 'api.endpoint'.freeze
...@@ -54,11 +55,20 @@ module Gitlab ...@@ -54,11 +55,20 @@ module Gitlab
end end
def action def action
@action ||= if @env[CONTROLLER_KEY] "#{labels[:controller]}##{labels[:action]}" if labels
action_from_controller(@env) || '' end
def labels
return @labels if @labels
# memoize transaction labels only source env variables were present
@labels = if @env[CONTROLLER_KEY]
labels_from_controller(@env) || {}
elsif @env[ENDPOINT_KEY] elsif @env[ENDPOINT_KEY]
action_from_endpoint(@env) || '' labels_from_endpoint(@env) || {}
end end
@labels || {}
end end
def run def run
...@@ -72,8 +82,8 @@ module Gitlab ...@@ -72,8 +82,8 @@ module Gitlab
@memory_after = System.memory_usage @memory_after = System.memory_usage
@finished_at = System.monotonic_time @finished_at = System.monotonic_time
Transaction.metric_transaction_duration_seconds.observe({ action: action }, duration * 1000) Transaction.metric_transaction_duration_seconds.observe(labels, duration * 1000)
Transaction.metric_transaction_allocated_memory_bytes.observe({ action: action }, allocated_memory * 1024.0) Transaction.metric_transaction_allocated_memory_bytes.observe(labels, allocated_memory * 1024.0)
Thread.current[THREAD_KEY] = nil Thread.current[THREAD_KEY] = nil
end end
...@@ -90,7 +100,7 @@ module Gitlab ...@@ -90,7 +100,7 @@ module Gitlab
# event_name - The name of the event (e.g. "git_push"). # event_name - The name of the event (e.g. "git_push").
# tags - A set of tags to attach to the event. # tags - A set of tags to attach to the event.
def add_event(event_name, tags = {}) def add_event(event_name, tags = {})
self.class.metric_event_counter(event_name, tags).increment(tags.merge({ action: action })) self.class.metric_event_counter(event_name, tags).increment(tags.merge(labels))
@metrics << Metric.new(EVENT_SERIES, { count: 1 }, tags, :event) @metrics << Metric.new(EVENT_SERIES, { count: 1 }, tags, :event)
end end
...@@ -104,12 +114,12 @@ module Gitlab ...@@ -104,12 +114,12 @@ module Gitlab
end end
def increment(name, value, use_prometheus = true) def increment(name, value, use_prometheus = true)
self.class.metric_transaction_counter(name).increment({ action: action }, value) if use_prometheus self.class.metric_transaction_counter(name).increment(labels, value) if use_prometheus
@values[name] += value @values[name] += value
end end
def set(name, value, use_prometheus = true) def set(name, value, use_prometheus = true)
self.class.metric_transaction_gauge(name).set({ action: action }, value) if use_prometheus self.class.metric_transaction_gauge(name).set(labels, value) if use_prometheus
@values[name] = value @values[name] = value
end end
...@@ -152,7 +162,7 @@ module Gitlab ...@@ -152,7 +162,7 @@ module Gitlab
@metric_transaction_duration_seconds ||= Gitlab::Metrics.histogram( @metric_transaction_duration_seconds ||= Gitlab::Metrics.histogram(
:gitlab_transaction_duration_seconds, :gitlab_transaction_duration_seconds,
'Transaction duration', 'Transaction duration',
{ action: nil }, BASE_LABELS,
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0] [0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
) )
end end
...@@ -161,7 +171,7 @@ module Gitlab ...@@ -161,7 +171,7 @@ module Gitlab
@metric_transaction_allocated_memory_bytes ||= Gitlab::Metrics.histogram( @metric_transaction_allocated_memory_bytes ||= Gitlab::Metrics.histogram(
:gitlab_transaction_allocated_memory_bytes, :gitlab_transaction_allocated_memory_bytes,
'Transaction allocated memory bytes', 'Transaction allocated memory bytes',
{ action: nil }, BASE_LABELS,
[1000, 10000, 20000, 500000, 1000000, 2000000, 5000000, 10000000, 20000000, 100000000] [1000, 10000, 20000, 500000, 1000000, 2000000, 5000000, 10000000, 20000000, 100000000]
) )
end end
...@@ -171,38 +181,38 @@ module Gitlab ...@@ -171,38 +181,38 @@ module Gitlab
@metric_event_counters[event_name] ||= Gitlab::Metrics.counter( @metric_event_counters[event_name] ||= Gitlab::Metrics.counter(
"gitlab_transaction_event_#{event_name}_total".to_sym, "gitlab_transaction_event_#{event_name}_total".to_sym,
"Transaction event #{event_name} counter", "Transaction event #{event_name} counter",
tags.merge({ action: nil }) tags.merge(BASE_LABELS)
) )
end end
def self.metric_transaction_counter(name) def self.metric_transaction_counter(name)
@metric_transaction_counters ||= {} @metric_transaction_counters ||= {}
@metric_transaction_counters[name] ||= Gitlab::Metrics.counter( @metric_transaction_counters[name] ||= Gitlab::Metrics.counter(
"gitlab_transaction_#{name}_total".to_sym, "Transaction #{name} counter", action: nil "gitlab_transaction_#{name}_total".to_sym, "Transaction #{name} counter", BASE_LABELS
) )
end end
def self.metric_transaction_gauge(name) def self.metric_transaction_gauge(name)
@metric_transaction_gauges ||= {} @metric_transaction_gauges ||= {}
@metric_transaction_gauges[name] ||= Gitlab::Metrics.gauge( @metric_transaction_gauges[name] ||= Gitlab::Metrics.gauge(
"gitlab_transaction_#{name}".to_sym, "Transaction gauge #{name}", { action: nil }, :livesum "gitlab_transaction_#{name}".to_sym, "Transaction gauge #{name}", BASE_LABELS, :livesum
) )
end end
def action_from_controller(env) def labels_from_controller(env)
controller = env[CONTROLLER_KEY] controller = env[CONTROLLER_KEY]
action = "#{controller.class.name}##{controller.action_name}" action = "#{controller.action_name}"
suffix = CONTENT_TYPES[controller.content_type] suffix = CONTENT_TYPES[controller.content_type]
if suffix && suffix != :html if suffix && suffix != :html
action += ".#{suffix}" action += ".#{suffix}"
end end
action { controller: controller.class.name, action: action }
end end
def action_from_endpoint(env) def labels_from_endpoint(env)
endpoint = env[ENDPOINT_KEY] endpoint = env[ENDPOINT_KEY]
begin begin
...@@ -215,7 +225,7 @@ module Gitlab ...@@ -215,7 +225,7 @@ module Gitlab
if route if route
path = endpoint_paths_cache[route.request_method][route.path] path = endpoint_paths_cache[route.request_method][route.path]
"Grape##{route.request_method} #{path}" { controller: 'Grape', action: "#{route.request_method} #{path}" }
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment