debian-mirror-gitlab/lib/gitlab/metrics/web_transaction.rb

89 lines
2.9 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module Gitlab
module Metrics
class WebTransaction < Transaction
2019-12-04 20:38:33 +05:30
CONTROLLER_KEY = 'action_controller.instance'
ENDPOINT_KEY = 'api.endpoint'
2018-11-08 19:23:39 +05:30
ALLOWED_SUFFIXES = Set.new(%w[json js atom rss xml zip])
2018-03-17 18:26:18 +05:30
def initialize(env)
super()
@env = 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 || {}
elsif @env[ENDPOINT_KEY]
labels_from_endpoint || {}
end
@labels || {}
end
private
def labels_from_controller
controller = @env[CONTROLLER_KEY]
action = "#{controller.action_name}"
2018-10-15 14:42:47 +05:30
2020-07-28 23:09:34 +05:30
# Try to get the feature category, but don't fail when the controller is
# not an ApplicationController.
feature_category = controller.class.try(:feature_category_for_action, action).to_s
2018-10-15 14:42:47 +05:30
# 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-11-08 19:23:39 +05:30
suffix = controller.request.format.try(:ref).to_s
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
# 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)
2019-02-15 15:39:39 +05:30
action = "#{action}.#{suffix}"
2018-03-17 18:26:18 +05:30
end
2020-07-28 23:09:34 +05:30
{ controller: controller.class.name, action: action, feature_category: feature_category }
2018-03-17 18:26:18 +05:30
end
def labels_from_endpoint
endpoint = @env[ENDPOINT_KEY]
begin
route = endpoint.route
rescue
# 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-28 23:09:34 +05:30
# Feature categories will be added for grape endpoints in
# https://gitlab.com/gitlab-com/gl-infra/scalability/-/issues/462
{ controller: 'Grape', action: "#{route.request_method} #{path}", feature_category: '' }
2018-03-17 18:26:18 +05:30
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
end
end
end