2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-29 18:18:46 -04:00
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
module InfluxDb
|
2018-01-19 12:12:19 -05:00
|
|
|
extend ActiveSupport::Concern
|
2018-01-29 06:33:08 -05:00
|
|
|
include Gitlab::Metrics::Methods
|
2017-05-31 19:46:19 -04:00
|
|
|
|
2018-01-16 10:30:17 -05:00
|
|
|
EXECUTION_MEASUREMENT_BUCKETS = [0.001, 0.01, 0.1, 1].freeze
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
MUTEX = Mutex.new
|
|
|
|
private_constant :MUTEX
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
class_methods do
|
|
|
|
def influx_metrics_enabled?
|
|
|
|
settings[:enabled] || false
|
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
# Prometheus histogram buckets used for arbitrary code measurements
|
|
|
|
|
|
|
|
def settings
|
2018-01-22 10:43:22 -05:00
|
|
|
@settings ||= begin
|
|
|
|
current_settings = Gitlab::CurrentSettings.current_application_settings
|
|
|
|
|
|
|
|
{
|
|
|
|
enabled: current_settings[:metrics_enabled],
|
|
|
|
pool_size: current_settings[:metrics_pool_size],
|
|
|
|
timeout: current_settings[:metrics_timeout],
|
|
|
|
method_call_threshold: current_settings[:metrics_method_call_threshold],
|
|
|
|
host: current_settings[:metrics_host],
|
|
|
|
port: current_settings[:metrics_port],
|
|
|
|
sample_interval: current_settings[:metrics_sample_interval] || 15,
|
|
|
|
packet_size: current_settings[:metrics_packet_size] || 1
|
2018-01-19 12:12:19 -05:00
|
|
|
}
|
2018-01-22 10:43:22 -05:00
|
|
|
end
|
2018-01-19 12:12:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def mri?
|
|
|
|
RUBY_ENGINE == 'ruby'
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_call_threshold
|
|
|
|
# This is memoized since this method is called for every instrumented
|
|
|
|
# method. Loading data from an external cache on every method call slows
|
|
|
|
# things down too much.
|
|
|
|
# in milliseconds
|
|
|
|
@method_call_threshold ||= settings[:method_call_threshold]
|
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
def submit_metrics(metrics)
|
|
|
|
prepared = prepare_metrics(metrics)
|
|
|
|
|
|
|
|
pool&.with do |connection|
|
|
|
|
prepared.each_slice(settings[:packet_size]) do |slice|
|
2019-03-13 09:42:43 -04:00
|
|
|
connection.write_points(slice)
|
|
|
|
rescue StandardError
|
2017-05-29 18:18:46 -04:00
|
|
|
end
|
|
|
|
end
|
2018-01-19 12:12:19 -05:00
|
|
|
rescue Errno::EADDRNOTAVAIL, SocketError => ex
|
|
|
|
Gitlab::EnvironmentLogger.error('Cannot resolve InfluxDB address. GitLab Performance Monitoring will not work.')
|
|
|
|
Gitlab::EnvironmentLogger.error(ex)
|
2017-05-29 18:18:46 -04:00
|
|
|
end
|
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
def prepare_metrics(metrics)
|
|
|
|
metrics.map do |hash|
|
|
|
|
new_hash = hash.symbolize_keys
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
new_hash[:tags].each do |key, value|
|
|
|
|
if value.blank?
|
|
|
|
new_hash[:tags].delete(key)
|
|
|
|
else
|
|
|
|
new_hash[:tags][key] = escape_value(value)
|
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
end
|
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
new_hash
|
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
end
|
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
def escape_value(value)
|
|
|
|
value.to_s.gsub('=', '\\=')
|
2018-01-16 10:30:17 -05:00
|
|
|
end
|
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
# Measures the execution time of a block.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# Gitlab::Metrics.measure(:find_by_username_duration) do
|
2018-10-18 05:06:44 -04:00
|
|
|
# UserFinder.new(some_username).find_by_username
|
2018-01-19 12:12:19 -05:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# name - The name of the field to store the execution time in.
|
|
|
|
#
|
|
|
|
# Returns the value yielded by the supplied block.
|
|
|
|
def measure(name)
|
|
|
|
trans = current_transaction
|
|
|
|
|
|
|
|
return yield unless trans
|
|
|
|
|
|
|
|
real_start = Time.now.to_f
|
|
|
|
cpu_start = System.cpu_time
|
|
|
|
|
|
|
|
retval = yield
|
|
|
|
|
|
|
|
cpu_stop = System.cpu_time
|
|
|
|
real_stop = Time.now.to_f
|
|
|
|
|
|
|
|
real_time = (real_stop - real_start)
|
|
|
|
cpu_time = cpu_stop - cpu_start
|
|
|
|
|
|
|
|
real_duration_seconds = fetch_histogram("gitlab_#{name}_real_duration_seconds".to_sym) do
|
|
|
|
docstring "Measure #{name}"
|
|
|
|
base_labels Transaction::BASE_LABELS
|
|
|
|
buckets EXECUTION_MEASUREMENT_BUCKETS
|
|
|
|
end
|
2018-01-16 10:30:17 -05:00
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
real_duration_seconds.observe(trans.labels, real_time)
|
2018-01-16 10:30:17 -05:00
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
cpu_duration_seconds = fetch_histogram("gitlab_#{name}_cpu_duration_seconds".to_sym) do
|
|
|
|
docstring "Measure #{name}"
|
|
|
|
base_labels Transaction::BASE_LABELS
|
|
|
|
buckets EXECUTION_MEASUREMENT_BUCKETS
|
2018-01-22 06:47:16 -05:00
|
|
|
with_feature "prometheus_metrics_measure_#{name}_cpu_duration"
|
2018-01-19 12:12:19 -05:00
|
|
|
end
|
|
|
|
cpu_duration_seconds.observe(trans.labels, cpu_time)
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
# InfluxDB stores the _real_time and _cpu_time time values as milliseconds
|
|
|
|
trans.increment("#{name}_real_time", real_time.in_milliseconds, false)
|
|
|
|
trans.increment("#{name}_cpu_time", cpu_time.in_milliseconds, false)
|
|
|
|
trans.increment("#{name}_call_count", 1, false)
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
retval
|
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
# Sets the action of the current transaction (if any)
|
|
|
|
#
|
|
|
|
# action - The name of the action.
|
|
|
|
def action=(action)
|
|
|
|
trans = current_transaction
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
trans&.action = action
|
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
# Tracks an event.
|
|
|
|
#
|
|
|
|
# See `Gitlab::Metrics::Transaction#add_event` for more details.
|
|
|
|
def add_event(*args)
|
2019-01-29 12:16:43 -05:00
|
|
|
current_transaction&.add_event(*args)
|
2018-01-19 12:12:19 -05:00
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
# Returns the prefix to use for the name of a series.
|
|
|
|
def series_prefix
|
2019-12-22 04:07:51 -05:00
|
|
|
@series_prefix ||= Gitlab::Runtime.sidekiq? ? 'sidekiq_' : 'rails_'
|
2018-01-19 12:12:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Allow access from other metrics related middlewares
|
|
|
|
def current_transaction
|
|
|
|
Transaction.current
|
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
# When enabled this should be set before being used as the usual pattern
|
|
|
|
# "@foo ||= bar" is _not_ thread-safe.
|
|
|
|
def pool
|
|
|
|
if influx_metrics_enabled?
|
|
|
|
if @pool.nil?
|
|
|
|
MUTEX.synchronize do
|
|
|
|
@pool ||= ConnectionPool.new(size: settings[:pool_size], timeout: settings[:timeout]) do
|
|
|
|
host = settings[:host]
|
|
|
|
port = settings[:port]
|
|
|
|
|
|
|
|
InfluxDB::Client
|
|
|
|
.new(udp: { host: host, port: port })
|
|
|
|
end
|
2017-05-31 19:46:19 -04:00
|
|
|
end
|
|
|
|
end
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2018-01-19 12:12:19 -05:00
|
|
|
@pool
|
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-31 19:46:19 -04:00
|
|
|
end
|