2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-16 09:57:33 -04:00
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
2021-10-18 17:12:56 -04:00
|
|
|
# Exclusive transaction-type metrics for web servers (including Web/Api/Git
|
|
|
|
# fleet). One instance of this class is created for each request going
|
|
|
|
# through the Rack metric middleware. Any metrics dispatched with this
|
|
|
|
# instance include metadata such as controller, action, feature category,
|
|
|
|
# etc.
|
2017-10-19 05:24:15 -04:00
|
|
|
class WebTransaction < Transaction
|
2021-10-18 17:12:56 -04:00
|
|
|
THREAD_KEY = :_gitlab_metrics_transaction
|
|
|
|
BASE_LABEL_KEYS = %i(controller action feature_category).freeze
|
|
|
|
|
2019-08-31 15:25:25 -04:00
|
|
|
CONTROLLER_KEY = 'action_controller.instance'
|
|
|
|
ENDPOINT_KEY = 'api.endpoint'
|
2018-06-20 08:33:14 -04:00
|
|
|
ALLOWED_SUFFIXES = Set.new(%w[json js atom rss xml zip])
|
2021-06-15 14:09:57 -04:00
|
|
|
SMALL_BUCKETS = [0.1, 0.25, 0.5, 1.0, 2.5, 5.0].freeze
|
2017-10-16 09:57:33 -04:00
|
|
|
|
2021-10-18 17:12:56 -04:00
|
|
|
class << self
|
|
|
|
def current
|
|
|
|
Thread.current[THREAD_KEY]
|
|
|
|
end
|
|
|
|
|
|
|
|
def prometheus_metric(name, type, &block)
|
|
|
|
fetch_metric(type, name) do
|
|
|
|
# set default metric options
|
|
|
|
docstring "#{name.to_s.humanize} #{type}"
|
|
|
|
|
|
|
|
evaluate(&block)
|
|
|
|
# always filter sensitive labels and merge with base ones
|
|
|
|
label_keys BASE_LABEL_KEYS | (label_keys - ::Gitlab::Metrics::Transaction::FILTERED_LABEL_KEYS)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-17 10:06:52 -04:00
|
|
|
def initialize(env)
|
|
|
|
super()
|
|
|
|
@env = env
|
2017-10-16 09:57:33 -04:00
|
|
|
end
|
|
|
|
|
2021-06-15 14:09:57 -04:00
|
|
|
def run
|
|
|
|
Thread.current[THREAD_KEY] = self
|
|
|
|
|
|
|
|
started_at = System.monotonic_time
|
|
|
|
|
|
|
|
status, _, _ = retval = yield
|
|
|
|
|
|
|
|
finished_at = System.monotonic_time
|
|
|
|
duration = finished_at - started_at
|
|
|
|
record_duration_if_needed(status, duration)
|
|
|
|
|
|
|
|
retval
|
|
|
|
ensure
|
|
|
|
Thread.current[THREAD_KEY] = nil
|
|
|
|
end
|
|
|
|
|
2017-10-16 09:57:33 -04:00
|
|
|
def labels
|
|
|
|
return @labels if @labels
|
|
|
|
|
|
|
|
# memoize transaction labels only source env variables were present
|
|
|
|
@labels = if @env[CONTROLLER_KEY]
|
|
|
|
labels_from_controller || {}
|
|
|
|
elsif @env[ENDPOINT_KEY]
|
|
|
|
labels_from_endpoint || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
@labels || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-06-15 14:09:57 -04:00
|
|
|
def record_duration_if_needed(status, duration)
|
|
|
|
return unless Gitlab::Metrics.record_duration_for_status?(status)
|
|
|
|
|
|
|
|
observe(:gitlab_transaction_duration_seconds, duration) do
|
|
|
|
buckets SMALL_BUCKETS
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-16 09:57:33 -04:00
|
|
|
def labels_from_controller
|
|
|
|
controller = @env[CONTROLLER_KEY]
|
|
|
|
|
|
|
|
action = "#{controller.action_name}"
|
2018-05-16 06:53:54 -04:00
|
|
|
|
|
|
|
# Devise exposes a method called "request_format" that does the below.
|
|
|
|
# However, this method is not available to all controllers (e.g. certain
|
|
|
|
# Doorkeeper controllers). As such we use the underlying code directly.
|
2018-06-20 08:33:14 -04:00
|
|
|
suffix = controller.request.format.try(:ref).to_s
|
2017-10-16 09:57:33 -04:00
|
|
|
|
2018-06-20 08:33:14 -04:00
|
|
|
# Sometimes the request format is set to silly data such as
|
|
|
|
# "application/xrds+xml" or actual URLs. To prevent such values from
|
|
|
|
# increasing the cardinality of our metrics, we limit the number of
|
|
|
|
# possible suffixes.
|
|
|
|
if suffix && ALLOWED_SUFFIXES.include?(suffix)
|
2018-11-16 19:37:17 -05:00
|
|
|
action = "#{action}.#{suffix}"
|
2017-10-16 09:57:33 -04:00
|
|
|
end
|
|
|
|
|
2020-07-07 05:08:57 -04:00
|
|
|
{ controller: controller.class.name, action: action, feature_category: feature_category }
|
2017-10-16 09:57:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def labels_from_endpoint
|
|
|
|
endpoint = @env[ENDPOINT_KEY]
|
|
|
|
|
|
|
|
begin
|
|
|
|
route = endpoint.route
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError
|
2017-10-16 09:57:33 -04:00
|
|
|
# endpoint.route is calling env[Grape::Env::GRAPE_ROUTING_ARGS][:route_info]
|
|
|
|
# but env[Grape::Env::GRAPE_ROUTING_ARGS] is nil in the case of a 405 response
|
|
|
|
# so we're rescuing exceptions and bailing out
|
|
|
|
end
|
|
|
|
|
|
|
|
if route
|
|
|
|
path = endpoint_paths_cache[route.request_method][route.path]
|
2020-07-07 05:08:57 -04:00
|
|
|
|
2020-12-08 10:09:45 -05:00
|
|
|
{ controller: 'Grape', action: "#{route.request_method} #{path}", feature_category: feature_category }
|
2017-10-16 09:57:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def endpoint_paths_cache
|
|
|
|
@endpoint_paths_cache ||= Hash.new do |hash, http_method|
|
|
|
|
hash[http_method] = Hash.new do |inner_hash, raw_path|
|
|
|
|
inner_hash[raw_path] = endpoint_instrumentable_path(raw_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def endpoint_instrumentable_path(raw_path)
|
|
|
|
raw_path.sub('(.:format)', '').sub('/:version', '')
|
|
|
|
end
|
2021-10-07 11:12:00 -04:00
|
|
|
|
|
|
|
def feature_category
|
|
|
|
::Gitlab::ApplicationContext.current_context_attribute(:feature_category) || ::Gitlab::FeatureCategories::FEATURE_CATEGORY_DEFAULT
|
|
|
|
end
|
2017-10-16 09:57:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|