d345591fc8
GitLab Performance Monitoring is now able to track custom events not directly related to application performance. These events include the number of tags pushed, repositories created, builds registered, etc. The use of these events is to get a better overview of how a GitLab instance is used and how that may affect performance. For example, a large number of Git pushes may have a negative impact on the underlying storage engine. Events are stored in the "events" measurement and are not prefixed with "rails_" or "sidekiq_", this makes it easier to query events with the same name triggered from different parts of the application. All events being stored in the same measurement also makes it easier to downsample data. Currently the following events are tracked: * Creating repositories * Removing repositories * Changing the default branch of a repository * Pushing a new tag * Removing an existing tag * Pushing a commit (along with the branch being pushed to) * Pushing a new branch * Removing an existing branch * Importing a repository (along with the URL we're importing) * Forking a repository (along with the source/target path) * CI builds registered (and when no build could be found) * CI builds being updated * Rails and Sidekiq exceptions Fixes gitlab-org/gitlab-ce#13720
158 lines
4.2 KiB
Ruby
158 lines
4.2 KiB
Ruby
module Gitlab
|
|
module Metrics
|
|
extend Gitlab::CurrentSettings
|
|
|
|
RAILS_ROOT = Rails.root.to_s
|
|
METRICS_ROOT = Rails.root.join('lib', 'gitlab', 'metrics').to_s
|
|
PATH_REGEX = /^#{RAILS_ROOT}\/?/
|
|
|
|
def self.settings
|
|
@settings ||= {
|
|
enabled: current_application_settings[:metrics_enabled],
|
|
pool_size: current_application_settings[:metrics_pool_size],
|
|
timeout: current_application_settings[:metrics_timeout],
|
|
method_call_threshold: current_application_settings[:metrics_method_call_threshold],
|
|
host: current_application_settings[:metrics_host],
|
|
port: current_application_settings[:metrics_port],
|
|
sample_interval: current_application_settings[:metrics_sample_interval] || 15,
|
|
packet_size: current_application_settings[:metrics_packet_size] || 1
|
|
}
|
|
end
|
|
|
|
def self.enabled?
|
|
settings[:enabled] || false
|
|
end
|
|
|
|
def self.mri?
|
|
RUBY_ENGINE == 'ruby'
|
|
end
|
|
|
|
def self.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.
|
|
@method_call_threshold ||= settings[:method_call_threshold]
|
|
end
|
|
|
|
def self.pool
|
|
@pool
|
|
end
|
|
|
|
def self.submit_metrics(metrics)
|
|
prepared = prepare_metrics(metrics)
|
|
|
|
pool.with do |connection|
|
|
prepared.each_slice(settings[:packet_size]) do |slice|
|
|
begin
|
|
connection.write_points(slice)
|
|
rescue StandardError
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def self.prepare_metrics(metrics)
|
|
metrics.map do |hash|
|
|
new_hash = hash.symbolize_keys
|
|
|
|
new_hash[:tags].each do |key, value|
|
|
if value.blank?
|
|
new_hash[:tags].delete(key)
|
|
else
|
|
new_hash[:tags][key] = escape_value(value)
|
|
end
|
|
end
|
|
|
|
new_hash
|
|
end
|
|
end
|
|
|
|
def self.escape_value(value)
|
|
value.to_s.gsub('=', '\\=')
|
|
end
|
|
|
|
# Measures the execution time of a block.
|
|
#
|
|
# Example:
|
|
#
|
|
# Gitlab::Metrics.measure(:find_by_username_duration) do
|
|
# User.find_by_username(some_username)
|
|
# end
|
|
#
|
|
# name - The name of the field to store the execution time in.
|
|
#
|
|
# Returns the value yielded by the supplied block.
|
|
def self.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) * 1000.0
|
|
cpu_time = cpu_stop - cpu_start
|
|
|
|
trans.increment("#{name}_real_time", real_time)
|
|
trans.increment("#{name}_cpu_time", cpu_time)
|
|
trans.increment("#{name}_call_count", 1)
|
|
|
|
retval
|
|
end
|
|
|
|
# Adds a tag to the current transaction (if any)
|
|
#
|
|
# name - The name of the tag to add.
|
|
# value - The value of the tag.
|
|
def self.tag_transaction(name, value)
|
|
trans = current_transaction
|
|
|
|
trans.add_tag(name, value) if trans
|
|
end
|
|
|
|
# Sets the action of the current transaction (if any)
|
|
#
|
|
# action - The name of the action.
|
|
def self.action=(action)
|
|
trans = current_transaction
|
|
|
|
trans.action = action if trans
|
|
end
|
|
|
|
# Tracks an event.
|
|
#
|
|
# See `Gitlab::Metrics::Transaction#add_event` for more details.
|
|
def self.add_event(*args)
|
|
trans = current_transaction
|
|
|
|
trans.add_event(*args) if trans
|
|
end
|
|
|
|
# Returns the prefix to use for the name of a series.
|
|
def self.series_prefix
|
|
@series_prefix ||= Sidekiq.server? ? 'sidekiq_' : 'rails_'
|
|
end
|
|
|
|
# When enabled this should be set before being used as the usual pattern
|
|
# "@foo ||= bar" is _not_ thread-safe.
|
|
if enabled?
|
|
@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
|
|
end
|
|
|
|
# Allow access from other metrics related middlewares
|
|
def self.current_transaction
|
|
Transaction.current
|
|
end
|
|
end
|
|
end
|